From 765c9eb85c80129791062e0884b0baef674f358c Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 9 Oct 2025 15:42:45 +0200 Subject: [PATCH] Add PagerConfig This is an object that is owned by Gui, is accessible through GuiCommon.State(), and also passed down to GitCommand, where it is mostly needed. Right now it simply wraps access to the Git.Paging config, which isn't very exciting, but we'll extend it in the next commit to handle a slice of pagers (and maintain the currently selected pager index), and doing this refactoring up front allows us to make that change without having to touch clients. --- pkg/commands/git.go | 6 +++- pkg/commands/git_commands/commit.go | 6 ++-- pkg/commands/git_commands/common.go | 30 +++++++++++-------- pkg/commands/git_commands/config.go | 12 -------- pkg/commands/git_commands/deps_test.go | 4 +++ pkg/commands/git_commands/diff.go | 6 ++-- pkg/commands/git_commands/stash.go | 6 ++-- pkg/commands/git_commands/working_tree.go | 12 ++++---- pkg/config/pager_config.go | 36 +++++++++++++++++++++++ pkg/gui/gui.go | 15 ++++++++-- pkg/gui/pty.go | 6 ++-- pkg/gui/types/common.go | 1 + 12 files changed, 93 insertions(+), 47 deletions(-) create mode 100644 pkg/config/pager_config.go diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 4aba9a7a8..90514cbd6 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -12,6 +12,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/common" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -59,6 +60,7 @@ func NewGitCommand( version *git_commands.GitVersion, osCommand *oscommands.OSCommand, gitConfig git_config.IGitConfig, + pagerConfig *config.PagerConfig, ) (*GitCommand, error) { repoPaths, err := git_commands.GetRepoPaths(osCommand.Cmd, version) if err != nil { @@ -88,6 +90,7 @@ func NewGitCommand( gitConfig, repoPaths, repository, + pagerConfig, ), nil } @@ -98,6 +101,7 @@ func NewGitCommandAux( gitConfig git_config.IGitConfig, repoPaths *git_commands.RepoPaths, repo *gogit.Repository, + pagerConfig *config.PagerConfig, ) *GitCommand { cmd := NewGitCmdObjBuilder(cmn.Log, osCommand.Cmd) @@ -108,7 +112,7 @@ func NewGitCommandAux( // common ones are: cmn, osCommand, dotGitDir, configCommands configCommands := git_commands.NewConfigCommands(cmn, gitConfig, repo) - gitCommon := git_commands.NewGitCommon(cmn, version, cmd, osCommand, repoPaths, repo, configCommands) + gitCommon := git_commands.NewGitCommon(cmn, version, cmd, osCommand, repoPaths, repo, configCommands, pagerConfig) fileLoader := git_commands.NewFileLoader(gitCommon, cmd, configCommands) statusCommands := git_commands.NewStatusCommands(gitCommon) diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 084a6c8d4..89063be82 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -256,14 +256,14 @@ func (self *CommitCommands) AmendHeadCmdObj() *oscommands.CmdObj { func (self *CommitCommands) ShowCmdObj(hash string, filterPaths []string) *oscommands.CmdObj { contextSize := self.UserConfig().Git.DiffContextSize - extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand - useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig + extDiffCmd := self.pagerConfig.GetExternalDiffCommand() + useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() cmdArgs := NewGitCmd("show"). Config("diff.noprefix=false"). ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd). ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff"). Arg("--submodule"). - Arg("--color="+self.UserConfig().Git.Paging.ColorArg). + Arg("--color="+self.pagerConfig.GetColorArg()). Arg(fmt.Sprintf("--unified=%d", contextSize)). Arg("--stat"). Arg("--decorate"). diff --git a/pkg/commands/git_commands/common.go b/pkg/commands/git_commands/common.go index b9537165c..28dd78e8b 100644 --- a/pkg/commands/git_commands/common.go +++ b/pkg/commands/git_commands/common.go @@ -4,16 +4,18 @@ import ( gogit "github.com/jesseduffield/go-git/v5" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" + "github.com/jesseduffield/lazygit/pkg/config" ) type GitCommon struct { *common.Common - version *GitVersion - cmd oscommands.ICmdObjBuilder - os *oscommands.OSCommand - repoPaths *RepoPaths - repo *gogit.Repository - config *ConfigCommands + version *GitVersion + cmd oscommands.ICmdObjBuilder + os *oscommands.OSCommand + repoPaths *RepoPaths + repo *gogit.Repository + config *ConfigCommands + pagerConfig *config.PagerConfig } func NewGitCommon( @@ -24,14 +26,16 @@ func NewGitCommon( repoPaths *RepoPaths, repo *gogit.Repository, config *ConfigCommands, + pagerConfig *config.PagerConfig, ) *GitCommon { return &GitCommon{ - Common: cmn, - version: version, - cmd: cmd, - os: osCommand, - repoPaths: repoPaths, - repo: repo, - config: config, + Common: cmn, + version: version, + cmd: cmd, + os: osCommand, + repoPaths: repoPaths, + repo: repo, + config: config, + pagerConfig: pagerConfig, } } diff --git a/pkg/commands/git_commands/config.go b/pkg/commands/git_commands/config.go index 40cf041b3..fa6570356 100644 --- a/pkg/commands/git_commands/config.go +++ b/pkg/commands/git_commands/config.go @@ -1,13 +1,10 @@ package git_commands import ( - "strconv" - gogit "github.com/jesseduffield/go-git/v5" "github.com/jesseduffield/go-git/v5/config" "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/common" - "github.com/jesseduffield/lazygit/pkg/utils" ) type ConfigCommands struct { @@ -29,15 +26,6 @@ func NewConfigCommands( } } -func (self *ConfigCommands) GetPager(width int) string { - templateValues := map[string]string{ - "columnWidth": strconv.Itoa(width/2 - 6), - } - - pagerTemplate := string(self.UserConfig().Git.Paging.Pager) - return utils.ResolvePlaceholderString(pagerTemplate, templateValues) -} - type GpgConfigKey string const ( diff --git a/pkg/commands/git_commands/deps_test.go b/pkg/commands/git_commands/deps_test.go index a8fff5edd..3c4bd5a14 100644 --- a/pkg/commands/git_commands/deps_test.go +++ b/pkg/commands/git_commands/deps_test.go @@ -61,6 +61,10 @@ func buildGitCommon(deps commonDeps) *GitCommon { gitCommon.Common.SetUserConfig(config.GetDefaultConfig()) } + gitCommon.pagerConfig = config.NewPagerConfig(func() *config.UserConfig { + return gitCommon.Common.UserConfig() + }) + gitCommon.version = deps.gitVersion if gitCommon.version == nil { gitCommon.version = &GitVersion{2, 0, 0, ""} diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index 76b677884..4feeb4d7f 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -19,9 +19,9 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands { // This is for generating diffs to be shown in the UI (e.g. rendering a range // diff to the main view). It uses a custom pager if one is configured. func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj { - extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand + extDiffCmd := self.pagerConfig.GetExternalDiffCommand() useExtDiff := extDiffCmd != "" - useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig + useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() ignoreWhitespace := self.UserConfig().Git.IgnoreWhitespaceInDiffView return self.cmd.New( @@ -30,7 +30,7 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj { ConfigIf(useExtDiff, "diff.external="+extDiffCmd). ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff"). Arg("--submodule"). - Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)). + Arg(fmt.Sprintf("--color=%s", self.pagerConfig.GetColorArg())). ArgIf(ignoreWhitespace, "--ignore-all-space"). Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)). Arg(diffArgs...). diff --git a/pkg/commands/git_commands/stash.go b/pkg/commands/git_commands/stash.go index f6b69ae7d..0e5eb299d 100644 --- a/pkg/commands/git_commands/stash.go +++ b/pkg/commands/git_commands/stash.go @@ -81,8 +81,8 @@ func (self *StashCommands) Hash(index int) (string, error) { } func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj { - extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand - useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig + extDiffCmd := self.pagerConfig.GetExternalDiffCommand() + useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() // "-u" is the same as "--include-untracked", but the latter fails in older git versions for some reason cmdArgs := NewGitCmd("stash").Arg("show"). @@ -91,7 +91,7 @@ func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj { Arg("-u"). ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd). ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff"). - Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)). + Arg(fmt.Sprintf("--color=%s", self.pagerConfig.GetColorArg())). Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)). ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)). diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index ffa1d99b6..1cba0d511 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -258,7 +258,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiff(file *models.File, plain bool, } func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain bool, cached bool) *oscommands.CmdObj { - colorArg := self.UserConfig().Git.Paging.ColorArg + colorArg := self.pagerConfig.GetColorArg() if plain { colorArg = "never" } @@ -266,9 +266,9 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain contextSize := self.UserConfig().Git.DiffContextSize prevPath := node.GetPreviousPath() noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile() - extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand + extDiffCmd := self.pagerConfig.GetExternalDiffCommand() useExtDiff := extDiffCmd != "" && !plain - useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig && !plain + useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() && !plain cmdArgs := NewGitCmd("diff"). ConfigIf(useExtDiff, "diff.external="+extDiffCmd). @@ -299,14 +299,14 @@ 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.CmdObj { contextSize := self.UserConfig().Git.DiffContextSize - colorArg := self.UserConfig().Git.Paging.ColorArg + colorArg := self.pagerConfig.GetColorArg() if plain { colorArg = "never" } - extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand + extDiffCmd := self.pagerConfig.GetExternalDiffCommand() useExtDiff := extDiffCmd != "" && !plain - useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig && !plain + useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() && !plain cmdArgs := NewGitCmd("diff"). Config("diff.noprefix=false"). diff --git a/pkg/config/pager_config.go b/pkg/config/pager_config.go new file mode 100644 index 000000000..6d7d39739 --- /dev/null +++ b/pkg/config/pager_config.go @@ -0,0 +1,36 @@ +package config + +import ( + "strconv" + + "github.com/jesseduffield/lazygit/pkg/utils" +) + +type PagerConfig struct { + getUserConfig func() *UserConfig +} + +func NewPagerConfig(getUserConfig func() *UserConfig) *PagerConfig { + return &PagerConfig{getUserConfig: getUserConfig} +} + +func (self *PagerConfig) GetPagerCommand(width int) string { + templateValues := map[string]string{ + "columnWidth": strconv.Itoa(width/2 - 6), + } + + pagerTemplate := string(self.getUserConfig().Git.Paging.Pager) + return utils.ResolvePlaceholderString(pagerTemplate, templateValues) +} + +func (self *PagerConfig) GetColorArg() string { + return self.getUserConfig().Git.Paging.ColorArg +} + +func (self *PagerConfig) GetExternalDiffCommand() string { + return self.getUserConfig().Git.Paging.ExternalDiffCommand +} + +func (self *PagerConfig) GetUseExternalDiffGitConfig() bool { + return self.getUserConfig().Git.Paging.UseExternalDiffGitConfig +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index c9252e03d..2727df3c4 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -69,6 +69,8 @@ type Gui struct { // this is the state of the GUI for the current repo State *GuiRepoState + pagerConfig *config.PagerConfig + CustomCommandsClient *custom_commands.Client // this is a mapping of repos to gui states, so that we can restore the original @@ -169,6 +171,10 @@ func (self *StateAccessor) GetRepoState() types.IRepoStateAccessor { return self.gui.State } +func (self *StateAccessor) GetPagerConfig() *config.PagerConfig { + return self.gui.pagerConfig +} + func (self *StateAccessor) GetIsRefreshingFiles() bool { return self.gui.IsRefreshingFiles } @@ -307,6 +313,7 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context gui.gitVersion, gui.os, git_config.NewStdCachedGitConfig(gui.Log), + gui.pagerConfig, ) if err != nil { return err @@ -653,7 +660,7 @@ func (gui *Gui) Contexts() *context.ContextTree { // NewGui builds a new gui handler func NewGui( cmn *common.Common, - config config.AppConfigurer, + configurer config.AppConfigurer, gitVersion *git_commands.GitVersion, updater *updates.Updater, showRecentRepos bool, @@ -663,7 +670,7 @@ func NewGui( gui := &Gui{ Common: cmn, gitVersion: gitVersion, - Config: config, + Config: configurer, Updater: updater, statusManager: status.NewStatusManager(), viewBufferManagerMap: map[string]*tasks.ViewBufferManager{}, @@ -713,7 +720,7 @@ func NewGui( credentialsHelper.PromptUserForCredential, ) - osCommand := oscommands.NewOSCommand(cmn, config, oscommands.GetPlatform(), guiIO) + osCommand := oscommands.NewOSCommand(cmn, configurer, oscommands.GetPlatform(), guiIO) gui.os = osCommand @@ -724,6 +731,8 @@ func NewGui( gui.BackgroundRoutineMgr = &BackgroundRoutineMgr{gui: gui} gui.stateAccessor = &StateAccessor{gui: gui} + gui.pagerConfig = config.NewPagerConfig(func() *config.UserConfig { return gui.UserConfig() }) + return gui, nil } diff --git a/pkg/gui/pty.go b/pkg/gui/pty.go index 4253438bb..688240dff 100644 --- a/pkg/gui/pty.go +++ b/pkg/gui/pty.go @@ -45,8 +45,8 @@ func (gui *Gui) onResize() error { // command. func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error { width := view.InnerWidth() - pager := gui.git.Config.GetPager(width) - externalDiffCommand := gui.Config.GetUserConfig().Git.Paging.ExternalDiffCommand + pager := gui.stateAccessor.GetPagerConfig().GetPagerCommand(width) + externalDiffCommand := gui.stateAccessor.GetPagerConfig().GetExternalDiffCommand() if pager == "" && externalDiffCommand == "" { // if we're not using a custom pager we don't need to use a pty @@ -58,7 +58,7 @@ func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error // Need to get the width and the pager again because the layout might have // changed the size of the view width = view.InnerWidth() - pager = gui.git.Config.GetPager(width) + pager := gui.stateAccessor.GetPagerConfig().GetPagerCommand(width) cmdStr := strings.Join(cmd.Args, " ") diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 6f7087d8e..905a9b65d 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -356,6 +356,7 @@ type HasUrn interface { type IStateAccessor interface { GetRepoPathStack() *utils.StringStack GetRepoState() IRepoStateAccessor + GetPagerConfig() *config.PagerConfig // tells us whether we're currently updating lazygit GetUpdating() bool SetUpdating(bool)