1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-10-19 08:09:21 +03:00

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.
This commit is contained in:
Stefan Haller
2025-10-09 15:42:45 +02:00
parent ed05470732
commit 765c9eb85c
12 changed files with 93 additions and 47 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@@ -59,6 +60,7 @@ func NewGitCommand(
version *git_commands.GitVersion, version *git_commands.GitVersion,
osCommand *oscommands.OSCommand, osCommand *oscommands.OSCommand,
gitConfig git_config.IGitConfig, gitConfig git_config.IGitConfig,
pagerConfig *config.PagerConfig,
) (*GitCommand, error) { ) (*GitCommand, error) {
repoPaths, err := git_commands.GetRepoPaths(osCommand.Cmd, version) repoPaths, err := git_commands.GetRepoPaths(osCommand.Cmd, version)
if err != nil { if err != nil {
@@ -88,6 +90,7 @@ func NewGitCommand(
gitConfig, gitConfig,
repoPaths, repoPaths,
repository, repository,
pagerConfig,
), nil ), nil
} }
@@ -98,6 +101,7 @@ func NewGitCommandAux(
gitConfig git_config.IGitConfig, gitConfig git_config.IGitConfig,
repoPaths *git_commands.RepoPaths, repoPaths *git_commands.RepoPaths,
repo *gogit.Repository, repo *gogit.Repository,
pagerConfig *config.PagerConfig,
) *GitCommand { ) *GitCommand {
cmd := NewGitCmdObjBuilder(cmn.Log, osCommand.Cmd) cmd := NewGitCmdObjBuilder(cmn.Log, osCommand.Cmd)
@@ -108,7 +112,7 @@ func NewGitCommandAux(
// common ones are: cmn, osCommand, dotGitDir, configCommands // common ones are: cmn, osCommand, dotGitDir, configCommands
configCommands := git_commands.NewConfigCommands(cmn, gitConfig, repo) 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) fileLoader := git_commands.NewFileLoader(gitCommon, cmd, configCommands)
statusCommands := git_commands.NewStatusCommands(gitCommon) statusCommands := git_commands.NewStatusCommands(gitCommon)

View File

@@ -256,14 +256,14 @@ func (self *CommitCommands) AmendHeadCmdObj() *oscommands.CmdObj {
func (self *CommitCommands) ShowCmdObj(hash string, filterPaths []string) *oscommands.CmdObj { func (self *CommitCommands) ShowCmdObj(hash string, filterPaths []string) *oscommands.CmdObj {
contextSize := self.UserConfig().Git.DiffContextSize contextSize := self.UserConfig().Git.DiffContextSize
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig()
cmdArgs := NewGitCmd("show"). cmdArgs := NewGitCmd("show").
Config("diff.noprefix=false"). Config("diff.noprefix=false").
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd). ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff"). ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
Arg("--submodule"). Arg("--submodule").
Arg("--color="+self.UserConfig().Git.Paging.ColorArg). Arg("--color="+self.pagerConfig.GetColorArg()).
Arg(fmt.Sprintf("--unified=%d", contextSize)). Arg(fmt.Sprintf("--unified=%d", contextSize)).
Arg("--stat"). Arg("--stat").
Arg("--decorate"). Arg("--decorate").

View File

@@ -4,16 +4,18 @@ import (
gogit "github.com/jesseduffield/go-git/v5" gogit "github.com/jesseduffield/go-git/v5"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
) )
type GitCommon struct { type GitCommon struct {
*common.Common *common.Common
version *GitVersion version *GitVersion
cmd oscommands.ICmdObjBuilder cmd oscommands.ICmdObjBuilder
os *oscommands.OSCommand os *oscommands.OSCommand
repoPaths *RepoPaths repoPaths *RepoPaths
repo *gogit.Repository repo *gogit.Repository
config *ConfigCommands config *ConfigCommands
pagerConfig *config.PagerConfig
} }
func NewGitCommon( func NewGitCommon(
@@ -24,14 +26,16 @@ func NewGitCommon(
repoPaths *RepoPaths, repoPaths *RepoPaths,
repo *gogit.Repository, repo *gogit.Repository,
config *ConfigCommands, config *ConfigCommands,
pagerConfig *config.PagerConfig,
) *GitCommon { ) *GitCommon {
return &GitCommon{ return &GitCommon{
Common: cmn, Common: cmn,
version: version, version: version,
cmd: cmd, cmd: cmd,
os: osCommand, os: osCommand,
repoPaths: repoPaths, repoPaths: repoPaths,
repo: repo, repo: repo,
config: config, config: config,
pagerConfig: pagerConfig,
} }
} }

View File

@@ -1,13 +1,10 @@
package git_commands package git_commands
import ( import (
"strconv"
gogit "github.com/jesseduffield/go-git/v5" gogit "github.com/jesseduffield/go-git/v5"
"github.com/jesseduffield/go-git/v5/config" "github.com/jesseduffield/go-git/v5/config"
"github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
type ConfigCommands struct { 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 type GpgConfigKey string
const ( const (

View File

@@ -61,6 +61,10 @@ func buildGitCommon(deps commonDeps) *GitCommon {
gitCommon.Common.SetUserConfig(config.GetDefaultConfig()) gitCommon.Common.SetUserConfig(config.GetDefaultConfig())
} }
gitCommon.pagerConfig = config.NewPagerConfig(func() *config.UserConfig {
return gitCommon.Common.UserConfig()
})
gitCommon.version = deps.gitVersion gitCommon.version = deps.gitVersion
if gitCommon.version == nil { if gitCommon.version == nil {
gitCommon.version = &GitVersion{2, 0, 0, ""} gitCommon.version = &GitVersion{2, 0, 0, ""}

View File

@@ -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 // 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. // diff to the main view). It uses a custom pager if one is configured.
func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj { func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj {
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
useExtDiff := extDiffCmd != "" useExtDiff := extDiffCmd != ""
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig()
ignoreWhitespace := self.UserConfig().Git.IgnoreWhitespaceInDiffView ignoreWhitespace := self.UserConfig().Git.IgnoreWhitespaceInDiffView
return self.cmd.New( return self.cmd.New(
@@ -30,7 +30,7 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj {
ConfigIf(useExtDiff, "diff.external="+extDiffCmd). ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff"). ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
Arg("--submodule"). 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"). ArgIf(ignoreWhitespace, "--ignore-all-space").
Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)). Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)).
Arg(diffArgs...). Arg(diffArgs...).

View File

@@ -81,8 +81,8 @@ func (self *StashCommands) Hash(index int) (string, error) {
} }
func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj { func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj {
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig()
// "-u" is the same as "--include-untracked", but the latter fails in older git versions for some reason // "-u" is the same as "--include-untracked", but the latter fails in older git versions for some reason
cmdArgs := NewGitCmd("stash").Arg("show"). cmdArgs := NewGitCmd("stash").Arg("show").
@@ -91,7 +91,7 @@ func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj {
Arg("-u"). Arg("-u").
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd). ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff"). 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)). Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)).
ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space").
Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)). Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)).

View File

@@ -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 { 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 { if plain {
colorArg = "never" colorArg = "never"
} }
@@ -266,9 +266,9 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
contextSize := self.UserConfig().Git.DiffContextSize contextSize := self.UserConfig().Git.DiffContextSize
prevPath := node.GetPreviousPath() prevPath := node.GetPreviousPath()
noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile() noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile()
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
useExtDiff := extDiffCmd != "" && !plain useExtDiff := extDiffCmd != "" && !plain
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig && !plain useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() && !plain
cmdArgs := NewGitCmd("diff"). cmdArgs := NewGitCmd("diff").
ConfigIf(useExtDiff, "diff.external="+extDiffCmd). 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 { func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) *oscommands.CmdObj {
contextSize := self.UserConfig().Git.DiffContextSize contextSize := self.UserConfig().Git.DiffContextSize
colorArg := self.UserConfig().Git.Paging.ColorArg colorArg := self.pagerConfig.GetColorArg()
if plain { if plain {
colorArg = "never" colorArg = "never"
} }
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
useExtDiff := extDiffCmd != "" && !plain useExtDiff := extDiffCmd != "" && !plain
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig && !plain useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() && !plain
cmdArgs := NewGitCmd("diff"). cmdArgs := NewGitCmd("diff").
Config("diff.noprefix=false"). Config("diff.noprefix=false").

View File

@@ -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
}

View File

@@ -69,6 +69,8 @@ type Gui struct {
// this is the state of the GUI for the current repo // this is the state of the GUI for the current repo
State *GuiRepoState State *GuiRepoState
pagerConfig *config.PagerConfig
CustomCommandsClient *custom_commands.Client CustomCommandsClient *custom_commands.Client
// this is a mapping of repos to gui states, so that we can restore the original // 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 return self.gui.State
} }
func (self *StateAccessor) GetPagerConfig() *config.PagerConfig {
return self.gui.pagerConfig
}
func (self *StateAccessor) GetIsRefreshingFiles() bool { func (self *StateAccessor) GetIsRefreshingFiles() bool {
return self.gui.IsRefreshingFiles return self.gui.IsRefreshingFiles
} }
@@ -307,6 +313,7 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context
gui.gitVersion, gui.gitVersion,
gui.os, gui.os,
git_config.NewStdCachedGitConfig(gui.Log), git_config.NewStdCachedGitConfig(gui.Log),
gui.pagerConfig,
) )
if err != nil { if err != nil {
return err return err
@@ -653,7 +660,7 @@ func (gui *Gui) Contexts() *context.ContextTree {
// NewGui builds a new gui handler // NewGui builds a new gui handler
func NewGui( func NewGui(
cmn *common.Common, cmn *common.Common,
config config.AppConfigurer, configurer config.AppConfigurer,
gitVersion *git_commands.GitVersion, gitVersion *git_commands.GitVersion,
updater *updates.Updater, updater *updates.Updater,
showRecentRepos bool, showRecentRepos bool,
@@ -663,7 +670,7 @@ func NewGui(
gui := &Gui{ gui := &Gui{
Common: cmn, Common: cmn,
gitVersion: gitVersion, gitVersion: gitVersion,
Config: config, Config: configurer,
Updater: updater, Updater: updater,
statusManager: status.NewStatusManager(), statusManager: status.NewStatusManager(),
viewBufferManagerMap: map[string]*tasks.ViewBufferManager{}, viewBufferManagerMap: map[string]*tasks.ViewBufferManager{},
@@ -713,7 +720,7 @@ func NewGui(
credentialsHelper.PromptUserForCredential, credentialsHelper.PromptUserForCredential,
) )
osCommand := oscommands.NewOSCommand(cmn, config, oscommands.GetPlatform(), guiIO) osCommand := oscommands.NewOSCommand(cmn, configurer, oscommands.GetPlatform(), guiIO)
gui.os = osCommand gui.os = osCommand
@@ -724,6 +731,8 @@ func NewGui(
gui.BackgroundRoutineMgr = &BackgroundRoutineMgr{gui: gui} gui.BackgroundRoutineMgr = &BackgroundRoutineMgr{gui: gui}
gui.stateAccessor = &StateAccessor{gui: gui} gui.stateAccessor = &StateAccessor{gui: gui}
gui.pagerConfig = config.NewPagerConfig(func() *config.UserConfig { return gui.UserConfig() })
return gui, nil return gui, nil
} }

View File

@@ -45,8 +45,8 @@ func (gui *Gui) onResize() error {
// command. // command.
func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error { func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error {
width := view.InnerWidth() width := view.InnerWidth()
pager := gui.git.Config.GetPager(width) pager := gui.stateAccessor.GetPagerConfig().GetPagerCommand(width)
externalDiffCommand := gui.Config.GetUserConfig().Git.Paging.ExternalDiffCommand externalDiffCommand := gui.stateAccessor.GetPagerConfig().GetExternalDiffCommand()
if pager == "" && externalDiffCommand == "" { if pager == "" && externalDiffCommand == "" {
// if we're not using a custom pager we don't need to use a pty // 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 // Need to get the width and the pager again because the layout might have
// changed the size of the view // changed the size of the view
width = view.InnerWidth() width = view.InnerWidth()
pager = gui.git.Config.GetPager(width) pager := gui.stateAccessor.GetPagerConfig().GetPagerCommand(width)
cmdStr := strings.Join(cmd.Args, " ") cmdStr := strings.Join(cmd.Args, " ")

View File

@@ -356,6 +356,7 @@ type HasUrn interface {
type IStateAccessor interface { type IStateAccessor interface {
GetRepoPathStack() *utils.StringStack GetRepoPathStack() *utils.StringStack
GetRepoState() IRepoStateAccessor GetRepoState() IRepoStateAccessor
GetPagerConfig() *config.PagerConfig
// tells us whether we're currently updating lazygit // tells us whether we're currently updating lazygit
GetUpdating() bool GetUpdating() bool
SetUpdating(bool) SetUpdating(bool)