Skip to content

Commit

Permalink
♻️ Refactor setting saves and compatibility
Browse files Browse the repository at this point in the history
Refactor the set saves which loads the save when an amend is requested.
Setting compatibility mode was moved into a method to reduce code in
configuration.

Validation was simplified as the logic was confusing.
  • Loading branch information
mikelorant committed Feb 18, 2023
1 parent a209820 commit cbc8836
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
11 changes: 11 additions & 0 deletions internal/ui/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func (m *Model) backupModel() savedState {
return save
}

func (m *Model) setSaves() {
m.amend = m.state.Options.Amend

switch m.amend {
case true:
m.currentSave = defaultAmendSave(m.state)
case false:
m.previousSave = defaultAmendSave(m.state)
}
}

func (m *Model) setSave() bool {
save := m.snapshotToSave()

Expand Down
34 changes: 15 additions & 19 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,9 @@ func (m *Model) Configure(state *commit.State) {

m.models.info.Date = m.Date.Format(dateTimeFormat)

m.amend = state.Options.Amend

switch m.amend {
case true:
m.currentSave = defaultAmendSave(state)
m.previousSave = savedState{}
case false:
m.currentSave = savedState{}
m.previousSave = defaultAmendSave(state)
}

m.setSaves()
m.restoreModel(m.currentSave)

switch m.state.Config.View.Compatibility {
case config.CompatibilityTtyd:
os.Setenv("LIPGLOSS_TERMINAL", "ttyd")
case config.CompatibilityKitty:
os.Setenv("LIPGLOSS_TERMINAL", "kitty")
}
m.setCompatibility()

if m.state.Snapshot.Restore && m.setSave() {
m.resetCursor()
Expand Down Expand Up @@ -456,10 +440,22 @@ func (m Model) commit(q quit) Model {
}

func (m Model) validate() bool {
return (m.state.Repository.Worktree.IsStaged() || m.amend) && m.models.header.Summary() != ""
staged := m.state.Repository.Worktree.IsStaged()
summary := m.models.header.Summary()

return (staged || m.amend) && summary != ""
}

func (m *Model) resetCursor() {
m.models.header.CursorStartSummary()
m.models.body.CursorStart()
}

func (m *Model) setCompatibility() {
switch m.state.Config.View.Compatibility {
case config.CompatibilityTtyd:
os.Setenv("LIPGLOSS_TERMINAL", "ttyd")
case config.CompatibilityKitty:
os.Setenv("LIPGLOSS_TERMINAL", "kitty")
}
}

0 comments on commit cbc8836

Please sign in to comment.