From 7774fe0ab37bb07832ef7349a3f7c080f9d908e1 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 28 Aug 2023 13:09:55 +0200 Subject: [PATCH] Move diff context size from UserConfig to AppState --- docs/Config.md | 1 - pkg/commands/git_commands/commit.go | 2 +- pkg/commands/git_commands/commit_test.go | 2 +- pkg/commands/git_commands/stash.go | 2 +- pkg/commands/git_commands/stash_test.go | 2 +- pkg/commands/git_commands/working_tree.go | 4 ++-- pkg/commands/git_commands/working_tree_test.go | 4 ++-- pkg/config/app_config.go | 2 ++ pkg/config/user_config.go | 6 ++---- pkg/gui/controllers/context_lines_controller.go | 10 +++++++--- pkg/i18n/english.go | 2 ++ 11 files changed, 21 insertions(+), 16 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 6546572c9..f23e6ccf0 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -117,7 +117,6 @@ git: overrideGpg: false # prevents lazygit from spawning a separate process when using GPG disableForcePushing: false parseEmoji: false - diffContextSize: 3 # how many lines of context are shown around a change in diffs os: copyToClipboardCmd: '' # See 'Custom Command for Copying to Clipboard' section editPreset: '' # see 'Configuring File Editing' section diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index b56c87e74..2f8446b1a 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -197,7 +197,7 @@ func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj { } func (self *CommitCommands) ShowCmdObj(sha string, filterPath string) oscommands.ICmdObj { - contextSize := self.UserConfig.Git.DiffContextSize + contextSize := self.AppState.DiffContextSize extDiffCmd := self.UserConfig.Git.Paging.ExternalDiffCommand cmdArgs := NewGitCmd("show"). diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index ace0c97de..a6f887a12 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -237,10 +237,10 @@ func TestCommitShowCmdObj(t *testing.T) { s := s t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() - userConfig.Git.DiffContextSize = s.contextSize userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd appState := &config.AppState{} appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace + appState.DiffContextSize = s.contextSize runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expected, "", nil) instance := buildCommitCommands(commonDeps{userConfig: userConfig, appState: appState, runner: runner}) diff --git a/pkg/commands/git_commands/stash.go b/pkg/commands/git_commands/stash.go index 12907a6ae..fad73a472 100644 --- a/pkg/commands/git_commands/stash.go +++ b/pkg/commands/git_commands/stash.go @@ -85,7 +85,7 @@ func (self *StashCommands) ShowStashEntryCmdObj(index int) oscommands.ICmdObj { Arg("-p"). Arg("--stat"). Arg(fmt.Sprintf("--color=%s", self.UserConfig.Git.Paging.ColorArg)). - Arg(fmt.Sprintf("--unified=%d", self.UserConfig.Git.DiffContextSize)). + Arg(fmt.Sprintf("--unified=%d", self.AppState.DiffContextSize)). ArgIf(self.AppState.IgnoreWhitespaceInDiffView, "--ignore-all-space"). Arg(fmt.Sprintf("stash@{%d}", index)). ToArgv() diff --git a/pkg/commands/git_commands/stash_test.go b/pkg/commands/git_commands/stash_test.go index 651430ab8..846c493ee 100644 --- a/pkg/commands/git_commands/stash_test.go +++ b/pkg/commands/git_commands/stash_test.go @@ -134,9 +134,9 @@ func TestStashStashEntryCmdObj(t *testing.T) { s := s t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() - userConfig.Git.DiffContextSize = s.contextSize appState := &config.AppState{} appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace + appState.DiffContextSize = s.contextSize instance := buildStashCommands(commonDeps{userConfig: userConfig, appState: appState}) cmdStr := instance.ShowStashEntryCmdObj(s.index).Args() diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 29e259f8f..7921cd26f 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -240,7 +240,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain colorArg = "never" } - contextSize := self.UserConfig.Git.DiffContextSize + contextSize := self.AppState.DiffContextSize prevPath := node.GetPreviousPath() noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile() extDiffCmd := self.UserConfig.Git.Paging.ExternalDiffCommand @@ -271,7 +271,7 @@ func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bo } func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) oscommands.ICmdObj { - contextSize := self.UserConfig.Git.DiffContextSize + contextSize := self.AppState.DiffContextSize colorArg := self.UserConfig.Git.Paging.ColorArg if plain { diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go index 8137a96af..cc46d4994 100644 --- a/pkg/commands/git_commands/working_tree_test.go +++ b/pkg/commands/git_commands/working_tree_test.go @@ -309,9 +309,9 @@ func TestWorkingTreeDiff(t *testing.T) { s := s t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() - userConfig.Git.DiffContextSize = s.contextSize appState := &config.AppState{} appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace + appState.DiffContextSize = s.contextSize instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig, appState: appState}) result := instance.WorktreeFileDiff(s.file, s.plain, s.cached) @@ -375,9 +375,9 @@ func TestWorkingTreeShowFileDiff(t *testing.T) { s := s t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() - userConfig.Git.DiffContextSize = s.contextSize appState := &config.AppState{} appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace + appState.DiffContextSize = s.contextSize instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig, appState: appState}) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index ea26dd872..54d41b695 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -322,6 +322,7 @@ type AppState struct { CustomCommandsHistory []string HideCommandLog bool IgnoreWhitespaceInDiffView bool + DiffContextSize int } func getDefaultAppState() *AppState { @@ -329,6 +330,7 @@ func getDefaultAppState() *AppState { LastUpdateCheck: 0, RecentRepos: []string{}, StartupPopupVersion: 0, + DiffContextSize: 3, } } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 0acfcfb9b..4df6b5676 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -95,9 +95,8 @@ type GitConfig struct { DisableForcePushing bool `yaml:"disableForcePushing"` CommitPrefixes map[string]CommitPrefixConfig `yaml:"commitPrefixes"` // this should really be under 'gui', not 'git' - ParseEmoji bool `yaml:"parseEmoji"` - Log LogConfig `yaml:"log"` - DiffContextSize int `yaml:"diffContextSize"` + ParseEmoji bool `yaml:"parseEmoji"` + Log LogConfig `yaml:"log"` } type PagingConfig struct { @@ -497,7 +496,6 @@ func GetDefaultConfig() *UserConfig { DisableForcePushing: false, CommitPrefixes: map[string]CommitPrefixConfig(nil), ParseEmoji: false, - DiffContextSize: 3, }, Refresher: RefresherConfig{ RefreshInterval: 10, diff --git a/pkg/gui/controllers/context_lines_controller.go b/pkg/gui/controllers/context_lines_controller.go index 5ec2d3167..d3ff7688d 100644 --- a/pkg/gui/controllers/context_lines_controller.go +++ b/pkg/gui/controllers/context_lines_controller.go @@ -2,6 +2,7 @@ package controllers import ( "errors" + "fmt" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -65,7 +66,7 @@ func (self *ContextLinesController) Increase() error { return self.c.Error(err) } - self.c.UserConfig.Git.DiffContextSize = self.c.UserConfig.Git.DiffContextSize + 1 + self.c.AppState.DiffContextSize++ return self.applyChange() } @@ -73,14 +74,14 @@ func (self *ContextLinesController) Increase() error { } func (self *ContextLinesController) Decrease() error { - old_size := self.c.UserConfig.Git.DiffContextSize + old_size := self.c.AppState.DiffContextSize if self.isShowingDiff() && old_size > 1 { if err := self.checkCanChangeContext(); err != nil { return self.c.Error(err) } - self.c.UserConfig.Git.DiffContextSize = old_size - 1 + self.c.AppState.DiffContextSize = old_size - 1 return self.applyChange() } @@ -88,6 +89,9 @@ func (self *ContextLinesController) Decrease() error { } func (self *ContextLinesController) applyChange() error { + self.c.Toast(fmt.Sprintf(self.c.Tr.DiffContextSizeChanged, self.c.AppState.DiffContextSize)) + self.c.SaveAppStateAndLogError() + currentContext := self.c.CurrentStaticContext() switch currentContext.GetKey() { // we make an exception for our staging and patch building contexts because they actually need to refresh their state afterwards. diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 3dbe405ff..5c7f99b55 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -512,6 +512,7 @@ type TranslationSet struct { IgnoreWhitespaceNotSupportedHere string IncreaseContextInDiffView string DecreaseContextInDiffView string + DiffContextSizeChanged string CreatePullRequestOptions string DefaultBranch string SelectBranch string @@ -1293,6 +1294,7 @@ func EnglishTranslationSet() TranslationSet { IgnoreWhitespaceNotSupportedHere: "Ignoring whitespace is not supported in this view", IncreaseContextInDiffView: "Increase the size of the context shown around changes in the diff view", DecreaseContextInDiffView: "Decrease the size of the context shown around changes in the diff view", + DiffContextSizeChanged: "Changed diff context size to %d", CreatePullRequestOptions: "Create pull request options", DefaultBranch: "Default branch", SelectBranch: "Select branch",