Skip to content

Commit 765c9eb

Browse files
committed
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.
1 parent ed05470 commit 765c9eb

File tree

12 files changed

+93
-47
lines changed

12 files changed

+93
-47
lines changed

pkg/commands/git.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
1313
"github.com/jesseduffield/lazygit/pkg/commands/patch"
1414
"github.com/jesseduffield/lazygit/pkg/common"
15+
"github.com/jesseduffield/lazygit/pkg/config"
1516
"github.com/jesseduffield/lazygit/pkg/utils"
1617
)
1718

@@ -59,6 +60,7 @@ func NewGitCommand(
5960
version *git_commands.GitVersion,
6061
osCommand *oscommands.OSCommand,
6162
gitConfig git_config.IGitConfig,
63+
pagerConfig *config.PagerConfig,
6264
) (*GitCommand, error) {
6365
repoPaths, err := git_commands.GetRepoPaths(osCommand.Cmd, version)
6466
if err != nil {
@@ -88,6 +90,7 @@ func NewGitCommand(
8890
gitConfig,
8991
repoPaths,
9092
repository,
93+
pagerConfig,
9194
), nil
9295
}
9396

@@ -98,6 +101,7 @@ func NewGitCommandAux(
98101
gitConfig git_config.IGitConfig,
99102
repoPaths *git_commands.RepoPaths,
100103
repo *gogit.Repository,
104+
pagerConfig *config.PagerConfig,
101105
) *GitCommand {
102106
cmd := NewGitCmdObjBuilder(cmn.Log, osCommand.Cmd)
103107

@@ -108,7 +112,7 @@ func NewGitCommandAux(
108112
// common ones are: cmn, osCommand, dotGitDir, configCommands
109113
configCommands := git_commands.NewConfigCommands(cmn, gitConfig, repo)
110114

111-
gitCommon := git_commands.NewGitCommon(cmn, version, cmd, osCommand, repoPaths, repo, configCommands)
115+
gitCommon := git_commands.NewGitCommon(cmn, version, cmd, osCommand, repoPaths, repo, configCommands, pagerConfig)
112116

113117
fileLoader := git_commands.NewFileLoader(gitCommon, cmd, configCommands)
114118
statusCommands := git_commands.NewStatusCommands(gitCommon)

pkg/commands/git_commands/commit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ func (self *CommitCommands) AmendHeadCmdObj() *oscommands.CmdObj {
256256
func (self *CommitCommands) ShowCmdObj(hash string, filterPaths []string) *oscommands.CmdObj {
257257
contextSize := self.UserConfig().Git.DiffContextSize
258258

259-
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
260-
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
259+
extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
260+
useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig()
261261
cmdArgs := NewGitCmd("show").
262262
Config("diff.noprefix=false").
263263
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
264264
ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
265265
Arg("--submodule").
266-
Arg("--color="+self.UserConfig().Git.Paging.ColorArg).
266+
Arg("--color="+self.pagerConfig.GetColorArg()).
267267
Arg(fmt.Sprintf("--unified=%d", contextSize)).
268268
Arg("--stat").
269269
Arg("--decorate").

pkg/commands/git_commands/common.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import (
44
gogit "github.com/jesseduffield/go-git/v5"
55
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
66
"github.com/jesseduffield/lazygit/pkg/common"
7+
"github.com/jesseduffield/lazygit/pkg/config"
78
)
89

910
type GitCommon struct {
1011
*common.Common
11-
version *GitVersion
12-
cmd oscommands.ICmdObjBuilder
13-
os *oscommands.OSCommand
14-
repoPaths *RepoPaths
15-
repo *gogit.Repository
16-
config *ConfigCommands
12+
version *GitVersion
13+
cmd oscommands.ICmdObjBuilder
14+
os *oscommands.OSCommand
15+
repoPaths *RepoPaths
16+
repo *gogit.Repository
17+
config *ConfigCommands
18+
pagerConfig *config.PagerConfig
1719
}
1820

1921
func NewGitCommon(
@@ -24,14 +26,16 @@ func NewGitCommon(
2426
repoPaths *RepoPaths,
2527
repo *gogit.Repository,
2628
config *ConfigCommands,
29+
pagerConfig *config.PagerConfig,
2730
) *GitCommon {
2831
return &GitCommon{
29-
Common: cmn,
30-
version: version,
31-
cmd: cmd,
32-
os: osCommand,
33-
repoPaths: repoPaths,
34-
repo: repo,
35-
config: config,
32+
Common: cmn,
33+
version: version,
34+
cmd: cmd,
35+
os: osCommand,
36+
repoPaths: repoPaths,
37+
repo: repo,
38+
config: config,
39+
pagerConfig: pagerConfig,
3640
}
3741
}

pkg/commands/git_commands/config.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package git_commands
22

33
import (
4-
"strconv"
5-
64
gogit "github.com/jesseduffield/go-git/v5"
75
"github.com/jesseduffield/go-git/v5/config"
86
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
97
"github.com/jesseduffield/lazygit/pkg/common"
10-
"github.com/jesseduffield/lazygit/pkg/utils"
118
)
129

1310
type ConfigCommands struct {
@@ -29,15 +26,6 @@ func NewConfigCommands(
2926
}
3027
}
3128

32-
func (self *ConfigCommands) GetPager(width int) string {
33-
templateValues := map[string]string{
34-
"columnWidth": strconv.Itoa(width/2 - 6),
35-
}
36-
37-
pagerTemplate := string(self.UserConfig().Git.Paging.Pager)
38-
return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
39-
}
40-
4129
type GpgConfigKey string
4230

4331
const (

pkg/commands/git_commands/deps_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func buildGitCommon(deps commonDeps) *GitCommon {
6161
gitCommon.Common.SetUserConfig(config.GetDefaultConfig())
6262
}
6363

64+
gitCommon.pagerConfig = config.NewPagerConfig(func() *config.UserConfig {
65+
return gitCommon.Common.UserConfig()
66+
})
67+
6468
gitCommon.version = deps.gitVersion
6569
if gitCommon.version == nil {
6670
gitCommon.version = &GitVersion{2, 0, 0, ""}

pkg/commands/git_commands/diff.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands {
1919
// This is for generating diffs to be shown in the UI (e.g. rendering a range
2020
// diff to the main view). It uses a custom pager if one is configured.
2121
func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj {
22-
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
22+
extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
2323
useExtDiff := extDiffCmd != ""
24-
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
24+
useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig()
2525
ignoreWhitespace := self.UserConfig().Git.IgnoreWhitespaceInDiffView
2626

2727
return self.cmd.New(
@@ -30,7 +30,7 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj {
3030
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
3131
ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
3232
Arg("--submodule").
33-
Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)).
33+
Arg(fmt.Sprintf("--color=%s", self.pagerConfig.GetColorArg())).
3434
ArgIf(ignoreWhitespace, "--ignore-all-space").
3535
Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)).
3636
Arg(diffArgs...).

pkg/commands/git_commands/stash.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (self *StashCommands) Hash(index int) (string, error) {
8181
}
8282

8383
func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj {
84-
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
85-
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
84+
extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
85+
useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig()
8686

8787
// "-u" is the same as "--include-untracked", but the latter fails in older git versions for some reason
8888
cmdArgs := NewGitCmd("stash").Arg("show").
@@ -91,7 +91,7 @@ func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj {
9191
Arg("-u").
9292
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
9393
ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
94-
Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)).
94+
Arg(fmt.Sprintf("--color=%s", self.pagerConfig.GetColorArg())).
9595
Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)).
9696
ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space").
9797
Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)).

pkg/commands/git_commands/working_tree.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ func (self *WorkingTreeCommands) WorktreeFileDiff(file *models.File, plain bool,
258258
}
259259

260260
func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain bool, cached bool) *oscommands.CmdObj {
261-
colorArg := self.UserConfig().Git.Paging.ColorArg
261+
colorArg := self.pagerConfig.GetColorArg()
262262
if plain {
263263
colorArg = "never"
264264
}
265265

266266
contextSize := self.UserConfig().Git.DiffContextSize
267267
prevPath := node.GetPreviousPath()
268268
noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile()
269-
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
269+
extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
270270
useExtDiff := extDiffCmd != "" && !plain
271-
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig && !plain
271+
useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() && !plain
272272

273273
cmdArgs := NewGitCmd("diff").
274274
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
@@ -299,14 +299,14 @@ func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bo
299299
func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) *oscommands.CmdObj {
300300
contextSize := self.UserConfig().Git.DiffContextSize
301301

302-
colorArg := self.UserConfig().Git.Paging.ColorArg
302+
colorArg := self.pagerConfig.GetColorArg()
303303
if plain {
304304
colorArg = "never"
305305
}
306306

307-
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
307+
extDiffCmd := self.pagerConfig.GetExternalDiffCommand()
308308
useExtDiff := extDiffCmd != "" && !plain
309-
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig && !plain
309+
useExtDiffGitConfig := self.pagerConfig.GetUseExternalDiffGitConfig() && !plain
310310

311311
cmdArgs := NewGitCmd("diff").
312312
Config("diff.noprefix=false").

pkg/config/pager_config.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/jesseduffield/lazygit/pkg/utils"
7+
)
8+
9+
type PagerConfig struct {
10+
getUserConfig func() *UserConfig
11+
}
12+
13+
func NewPagerConfig(getUserConfig func() *UserConfig) *PagerConfig {
14+
return &PagerConfig{getUserConfig: getUserConfig}
15+
}
16+
17+
func (self *PagerConfig) GetPagerCommand(width int) string {
18+
templateValues := map[string]string{
19+
"columnWidth": strconv.Itoa(width/2 - 6),
20+
}
21+
22+
pagerTemplate := string(self.getUserConfig().Git.Paging.Pager)
23+
return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
24+
}
25+
26+
func (self *PagerConfig) GetColorArg() string {
27+
return self.getUserConfig().Git.Paging.ColorArg
28+
}
29+
30+
func (self *PagerConfig) GetExternalDiffCommand() string {
31+
return self.getUserConfig().Git.Paging.ExternalDiffCommand
32+
}
33+
34+
func (self *PagerConfig) GetUseExternalDiffGitConfig() bool {
35+
return self.getUserConfig().Git.Paging.UseExternalDiffGitConfig
36+
}

pkg/gui/gui.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type Gui struct {
6969
// this is the state of the GUI for the current repo
7070
State *GuiRepoState
7171

72+
pagerConfig *config.PagerConfig
73+
7274
CustomCommandsClient *custom_commands.Client
7375

7476
// 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 {
169171
return self.gui.State
170172
}
171173

174+
func (self *StateAccessor) GetPagerConfig() *config.PagerConfig {
175+
return self.gui.pagerConfig
176+
}
177+
172178
func (self *StateAccessor) GetIsRefreshingFiles() bool {
173179
return self.gui.IsRefreshingFiles
174180
}
@@ -307,6 +313,7 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context
307313
gui.gitVersion,
308314
gui.os,
309315
git_config.NewStdCachedGitConfig(gui.Log),
316+
gui.pagerConfig,
310317
)
311318
if err != nil {
312319
return err
@@ -653,7 +660,7 @@ func (gui *Gui) Contexts() *context.ContextTree {
653660
// NewGui builds a new gui handler
654661
func NewGui(
655662
cmn *common.Common,
656-
config config.AppConfigurer,
663+
configurer config.AppConfigurer,
657664
gitVersion *git_commands.GitVersion,
658665
updater *updates.Updater,
659666
showRecentRepos bool,
@@ -663,7 +670,7 @@ func NewGui(
663670
gui := &Gui{
664671
Common: cmn,
665672
gitVersion: gitVersion,
666-
Config: config,
673+
Config: configurer,
667674
Updater: updater,
668675
statusManager: status.NewStatusManager(),
669676
viewBufferManagerMap: map[string]*tasks.ViewBufferManager{},
@@ -713,7 +720,7 @@ func NewGui(
713720
credentialsHelper.PromptUserForCredential,
714721
)
715722

716-
osCommand := oscommands.NewOSCommand(cmn, config, oscommands.GetPlatform(), guiIO)
723+
osCommand := oscommands.NewOSCommand(cmn, configurer, oscommands.GetPlatform(), guiIO)
717724

718725
gui.os = osCommand
719726

@@ -724,6 +731,8 @@ func NewGui(
724731
gui.BackgroundRoutineMgr = &BackgroundRoutineMgr{gui: gui}
725732
gui.stateAccessor = &StateAccessor{gui: gui}
726733

734+
gui.pagerConfig = config.NewPagerConfig(func() *config.UserConfig { return gui.UserConfig() })
735+
727736
return gui, nil
728737
}
729738

0 commit comments

Comments
 (0)