From be667682f09652a32e0c5c9bb12c20caa6571349 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 26 Jul 2023 11:29:43 +0200 Subject: [PATCH 1/2] Move IgnoreWhitespaceInDiffView to AppState, and persist it when it changes --- pkg/config/app_config.go | 5 +++-- pkg/gui/controllers/commits_files_controller.go | 2 +- pkg/gui/controllers/files_controller.go | 4 ++-- pkg/gui/controllers/helpers/diff_helper.go | 4 ++-- pkg/gui/controllers/local_commits_controller.go | 2 +- pkg/gui/controllers/reflog_commits_controller.go | 2 +- pkg/gui/controllers/stash_controller.go | 2 +- pkg/gui/controllers/sub_commits_controller.go | 2 +- pkg/gui/controllers/submodules_controller.go | 2 +- pkg/gui/controllers/toggle_whitespace_action.go | 5 ++++- pkg/gui/gui.go | 11 ----------- pkg/gui/types/common.go | 2 -- 12 files changed, 17 insertions(+), 26 deletions(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 0d00cb780..a9872ea1e 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -318,8 +318,9 @@ type AppState struct { StartupPopupVersion int // these are for custom commands typed in directly, not for custom commands in the lazygit config - CustomCommandsHistory []string - HideCommandLog bool + CustomCommandsHistory []string + HideCommandLog bool + IgnoreWhitespaceInDiffView bool } func getDefaultAppState() *AppState { diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index a1cd6a9ca..c82a051f4 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -114,7 +114,7 @@ func (self *CommitFilesController) GetOnRenderToMain() func() error { from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName()) cmdObj := self.c.Git().WorkingTree.ShowFileDiffCmdObj( - from, to, reverse, node.GetPath(), false, self.c.State().GetIgnoreWhitespaceInDiffView(), + from, to, reverse, node.GetPath(), false, self.c.GetAppState().IgnoreWhitespaceInDiffView, ) task := types.NewRunPtyTask(cmdObj.GetCmd()) diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 1b5d44080..b557c636f 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -201,7 +201,7 @@ func (self *FilesController) GetOnRenderToMain() func() error { split := self.c.UserConfig.Gui.SplitDiff == "always" || (node.GetHasUnstagedChanges() && node.GetHasStagedChanges()) mainShowsStaged := !split && node.GetHasStagedChanges() - cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(node, false, mainShowsStaged, self.c.State().GetIgnoreWhitespaceInDiffView()) + cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(node, false, mainShowsStaged, self.c.GetAppState().IgnoreWhitespaceInDiffView) title := self.c.Tr.UnstagedChanges if mainShowsStaged { title = self.c.Tr.StagedChanges @@ -216,7 +216,7 @@ func (self *FilesController) GetOnRenderToMain() func() error { } if split { - cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(node, false, true, self.c.State().GetIgnoreWhitespaceInDiffView()) + cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(node, false, true, self.c.GetAppState().IgnoreWhitespaceInDiffView) title := self.c.Tr.StagedChanges if mainShowsStaged { diff --git a/pkg/gui/controllers/helpers/diff_helper.go b/pkg/gui/controllers/helpers/diff_helper.go index 3cf4f5735..bf4b33052 100644 --- a/pkg/gui/controllers/helpers/diff_helper.go +++ b/pkg/gui/controllers/helpers/diff_helper.go @@ -29,7 +29,7 @@ func (self *DiffHelper) DiffArgs() []string { output = append(output, "-R") } - if self.c.State().GetIgnoreWhitespaceInDiffView() { + if self.c.GetAppState().IgnoreWhitespaceInDiffView { output = append(output, "--ignore-all-space") } @@ -113,7 +113,7 @@ func (self *DiffHelper) WithDiffModeCheck(f func() error) error { } func (self *DiffHelper) IgnoringWhitespaceSubTitle() string { - if self.c.State().GetIgnoreWhitespaceInDiffView() { + if self.c.GetAppState().IgnoreWhitespaceInDiffView { return self.c.Tr.IgnoreWhitespaceDiffViewSubTitle } diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 49f664a29..696e4e717 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -177,7 +177,7 @@ func (self *LocalCommitsController) GetOnRenderToMain() func() error { "ref": commit.Name, })) } else { - cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.State().GetIgnoreWhitespaceInDiffView()) + cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.GetAppState().IgnoreWhitespaceInDiffView) task = types.NewRunPtyTask(cmdObj.GetCmd()) } diff --git a/pkg/gui/controllers/reflog_commits_controller.go b/pkg/gui/controllers/reflog_commits_controller.go index 735e0434f..512390a6b 100644 --- a/pkg/gui/controllers/reflog_commits_controller.go +++ b/pkg/gui/controllers/reflog_commits_controller.go @@ -37,7 +37,7 @@ func (self *ReflogCommitsController) GetOnRenderToMain() func() error { if commit == nil { task = types.NewRenderStringTask("No reflog history") } else { - cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.State().GetIgnoreWhitespaceInDiffView()) + cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.GetAppState().IgnoreWhitespaceInDiffView) task = types.NewRunPtyTask(cmdObj.GetCmd()) } diff --git a/pkg/gui/controllers/stash_controller.go b/pkg/gui/controllers/stash_controller.go index 1d8a0bbb0..1b8377f27 100644 --- a/pkg/gui/controllers/stash_controller.go +++ b/pkg/gui/controllers/stash_controller.go @@ -66,7 +66,7 @@ func (self *StashController) GetOnRenderToMain() func() error { task = types.NewRunPtyTask( self.c.Git().Stash.ShowStashEntryCmdObj( stashEntry.Index, - self.c.State().GetIgnoreWhitespaceInDiffView(), + self.c.GetAppState().IgnoreWhitespaceInDiffView, ).GetCmd(), ) } diff --git a/pkg/gui/controllers/sub_commits_controller.go b/pkg/gui/controllers/sub_commits_controller.go index 0855e6689..c11bba089 100644 --- a/pkg/gui/controllers/sub_commits_controller.go +++ b/pkg/gui/controllers/sub_commits_controller.go @@ -38,7 +38,7 @@ func (self *SubCommitsController) GetOnRenderToMain() func() error { if commit == nil { task = types.NewRenderStringTask("No commits") } else { - cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.State().GetIgnoreWhitespaceInDiffView()) + cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Sha, self.c.Modes().Filtering.GetPath(), self.c.GetAppState().IgnoreWhitespaceInDiffView) task = types.NewRunPtyTask(cmdObj.GetCmd()) } diff --git a/pkg/gui/controllers/submodules_controller.go b/pkg/gui/controllers/submodules_controller.go index ebf597d3b..06a26af2e 100644 --- a/pkg/gui/controllers/submodules_controller.go +++ b/pkg/gui/controllers/submodules_controller.go @@ -102,7 +102,7 @@ func (self *SubmodulesController) GetOnRenderToMain() func() error { if file == nil { task = types.NewRenderStringTask(prefix) } else { - cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(file, false, !file.HasUnstagedChanges && file.HasStagedChanges, self.c.State().GetIgnoreWhitespaceInDiffView()) + cmdObj := self.c.Git().WorkingTree.WorktreeFileDiffCmdObj(file, false, !file.HasUnstagedChanges && file.HasStagedChanges, self.c.GetAppState().IgnoreWhitespaceInDiffView) task = types.NewRunCommandTaskWithPrefix(cmdObj.GetCmd(), prefix) } } diff --git a/pkg/gui/controllers/toggle_whitespace_action.go b/pkg/gui/controllers/toggle_whitespace_action.go index f501338f4..5e9c1bf1b 100644 --- a/pkg/gui/controllers/toggle_whitespace_action.go +++ b/pkg/gui/controllers/toggle_whitespace_action.go @@ -23,7 +23,10 @@ func (self *ToggleWhitespaceAction) Call() error { return self.c.ErrorMsg(self.c.Tr.IgnoreWhitespaceNotSupportedHere) } - self.c.State().SetIgnoreWhitespaceInDiffView(!self.c.State().GetIgnoreWhitespaceInDiffView()) + self.c.GetAppState().IgnoreWhitespaceInDiffView = !self.c.GetAppState().IgnoreWhitespaceInDiffView + if err := self.c.SaveAppState(); err != nil { + self.c.Log.Errorf("error when saving app state: %v", err) + } return self.c.CurrentSideContext().HandleFocus(types.OnFocusOpts{}) } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 4da5d5e96..49a894941 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -105,9 +105,6 @@ type Gui struct { IsNewRepo bool - // flag as to whether or not the diff view should ignore whitespace - IgnoreWhitespaceInDiffView bool - IsRefreshingFiles bool // we use this to decide whether we'll return to the original directory that @@ -144,14 +141,6 @@ type StateAccessor struct { var _ types.IStateAccessor = new(StateAccessor) -func (self *StateAccessor) GetIgnoreWhitespaceInDiffView() bool { - return self.gui.IgnoreWhitespaceInDiffView -} - -func (self *StateAccessor) SetIgnoreWhitespaceInDiffView(value bool) { - self.gui.IgnoreWhitespaceInDiffView = value -} - func (self *StateAccessor) GetRepoPathStack() *utils.StringStack { return self.gui.RepoPathStack } diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 6480f1fcc..b8cc92ff2 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -243,8 +243,6 @@ type Mutexes struct { } type IStateAccessor interface { - GetIgnoreWhitespaceInDiffView() bool - SetIgnoreWhitespaceInDiffView(value bool) GetRepoPathStack() *utils.StringStack GetRepoState() IRepoStateAccessor // tells us whether we're currently updating lazygit From 668d29736c20a22396eaa36f70279cfd23f71c39 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 28 Jul 2023 14:31:14 +0200 Subject: [PATCH 2/2] Log error when saving app state fails after showing/hiding command log --- pkg/gui/extras_panel.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/gui/extras_panel.go b/pkg/gui/extras_panel.go index 4d607f631..5381823b9 100644 --- a/pkg/gui/extras_panel.go +++ b/pkg/gui/extras_panel.go @@ -24,7 +24,9 @@ func (gui *Gui) handleCreateExtrasMenuPanel() error { show := !gui.c.State().GetShowExtrasWindow() gui.c.State().SetShowExtrasWindow(show) gui.c.GetAppState().HideCommandLog = !show - _ = gui.c.SaveAppState() + if err := gui.c.SaveAppState(); err != nil { + gui.c.Log.Errorf("error when saving app state: %v", err) + } return nil }, },