Skip to content

Commit 7925683

Browse files
committed
Replace paging config with an array of pagers
1 parent 5b2227a commit 7925683

File tree

9 files changed

+191
-78
lines changed

9 files changed

+191
-78
lines changed

docs/Config.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -319,26 +319,30 @@ gui:
319319

320320
# Config relating to git
321321
git:
322+
# Array of pagers. Each entry has the following format:
323+
#
324+
# # Value of the --color arg in the git diff command. Some pagers want
325+
# # this to be set to 'always' and some want it set to 'never'
326+
# colorArg: "always"
327+
#
328+
# # e.g.
329+
# # diff-so-fancy
330+
# # delta --dark --paging=never
331+
# # ydiff -p cat -s --wrap --width={{columnWidth}}
332+
# pager: ""
333+
#
334+
# # e.g. 'difft --color=always'
335+
# externalDiffCommand: ""
336+
#
337+
# # If true, Lazygit will use git's `diff.external` config for paging.
338+
# # The advantage over `externalDiffCommand` is that this can be
339+
# # configured per file type in .gitattributes; see
340+
# # https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.
341+
# useExternalDiffGitConfig: false
342+
#
322343
# See https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_Pagers.md
323-
paging:
324-
# Value of the --color arg in the git diff command. Some pagers want this to be
325-
# set to 'always' and some want it set to 'never'
326-
colorArg: always
327-
328-
# e.g.
329-
# diff-so-fancy
330-
# delta --dark --paging=never
331-
# ydiff -p cat -s --wrap --width={{columnWidth}}
332-
pager: ""
333-
334-
# e.g. 'difft --color=always'
335-
externalDiffCommand: ""
336-
337-
# If true, Lazygit will use git's `diff.external` config for paging. The
338-
# advantage over `externalDiffCommand` is that this can be configured per file
339-
# type in .gitattributes; see
340-
# https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.
341-
useExternalDiffGitConfig: false
344+
# for more information.
345+
pagers: []
342346

343347
# Config relating to committing
344348
commit:
@@ -638,6 +642,7 @@ keybinding:
638642
prevTab: '['
639643
nextScreenMode: +
640644
prevScreenMode: _
645+
cyclePagers: '|'
641646
undo: z
642647
redo: Z
643648
filteringMenu: <c-s>

docs/Custom_Pagers.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ Lazygit supports custom pagers, [configured](/docs/Config.md) in the config.yml
44

55
Support does not extend to Windows users, because we're making use of a package which doesn't have Windows support. However, see [below](#emulating-custom-pagers-on-windows) for a workaround.
66

7-
## Default:
7+
Multiple pagers are supported; you can cycle through them with the `|` key. This can be useful if you usually prefer a particular pager, but want to use a different one for certain kinds of diffs.
8+
9+
Pagers are configured with the `pagers` array in the git section; here's an example for a multi-pager setup:
810

911
```yaml
1012
git:
11-
paging:
12-
colorArg: always
13+
pagers:
14+
- pager: delta --dark --paging=never
15+
- pager: ydiff -p cat -s --wrap --width={{columnWidth}}
16+
colorArg: never
17+
- externalDiffCommand: difft --color=always
1318
```
1419
15-
the `colorArg` key is for whether you want the `--color=always` arg in your `git diff` command. Some pagers want it set to `always`, others want it set to `never`.
20+
The `colorArg` key is for whether you want the `--color=always` arg in your `git diff` command. Some pagers want it set to `always`, others want it set to `never`. The default is `always`, since that's what most pagers need.
1621

1722
## Delta:
1823

1924
```yaml
2025
git:
21-
paging:
22-
colorArg: always
23-
pager: delta --dark --paging=never
26+
pagers:
27+
- pager: delta --dark --paging=never
2428
```
2529

2630
![](https://i.imgur.com/QJpQkF3.png)
@@ -31,9 +35,8 @@ A cool feature of delta is --hyperlinks, which renders clickable links for the l
3135

3236
```yaml
3337
git:
34-
paging:
35-
colorArg: always
36-
pager: diff-so-fancy
38+
pagers:
39+
- pager: diff-so-fancy
3740
```
3841

3942
![](https://i.imgur.com/rjH1TpT.png)
@@ -44,9 +47,9 @@ git:
4447
gui:
4548
sidePanelWidth: 0.2 # gives you more space to show things side-by-side
4649
git:
47-
paging:
48-
colorArg: never
49-
pager: ydiff -p cat -s --wrap --width={{columnWidth}}
50+
pagers:
51+
- colorArg: never
52+
pager: ydiff -p cat -s --wrap --width={{columnWidth}}
5053
```
5154

5255
![](https://i.imgur.com/vaa8z0H.png)
@@ -61,8 +64,8 @@ These can be used in lazygit by using the `externalDiffCommand` config; in the c
6164

6265
```yaml
6366
git:
64-
paging:
65-
externalDiffCommand: difft --color=always
67+
pagers:
68+
- externalDiffCommand: difft --color=always
6669
```
6770

6871
The `colorArg` and `pager` options are not used in this case.
@@ -71,16 +74,16 @@ You can add whatever extra arguments you prefer for your difftool; for instance
7174

7275
```yaml
7376
git:
74-
paging:
75-
externalDiffCommand: difft --color=always --display=inline --syntax-highlight=off
77+
pagers:
78+
- externalDiffCommand: difft --color=always --display=inline --syntax-highlight=off
7679
```
7780

7881
Instead of setting this command in lazygit's `externalDiffCommand` config, you can also tell lazygit to use the external diff command that is configured in git itself (`diff.external`), by using
7982

8083
```yaml
8184
git:
82-
paging:
83-
useExternalDiffGitConfig: true
85+
pagers:
86+
- useExternalDiffGitConfig: true
8487
```
8588

8689
This can be useful if you also want to use it for diffs on the command line, and it also has the advantage that you can configure it per file type in `.gitattributes`; see https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.
@@ -106,8 +109,8 @@ In your lazygit config, use
106109

107110
```yml
108111
git:
109-
paging:
110-
externalDiffCommand: "C:/wherever/lazygit-pager.ps1"
112+
pagers:
113+
- externalDiffCommand: "C:/wherever/lazygit-pager.ps1"
111114
```
112115

113116
The main limitation of this approach compared to a "real" pager is that renames are not displayed correctly; they are shown as if they were modifications of the old file. (This affects only the hunk headers; the diff itself is always correct.)

pkg/commands/git_commands/commit_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ func TestCommitShowCmdObj(t *testing.T) {
255255
contextSize uint64
256256
similarityThreshold int
257257
ignoreWhitespace bool
258-
extDiffCmd string
259-
useExtDiffGitConfig bool
258+
pagerConfig *config.PagingConfig
260259
expected []string
261260
}
262261

@@ -267,7 +266,7 @@ func TestCommitShowCmdObj(t *testing.T) {
267266
contextSize: 3,
268267
similarityThreshold: 50,
269268
ignoreWhitespace: false,
270-
extDiffCmd: "",
269+
pagerConfig: nil,
271270
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--"},
272271
},
273272
{
@@ -276,7 +275,7 @@ func TestCommitShowCmdObj(t *testing.T) {
276275
contextSize: 3,
277276
similarityThreshold: 50,
278277
ignoreWhitespace: false,
279-
extDiffCmd: "",
278+
pagerConfig: nil,
280279
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--", "file.txt"},
281280
},
282281
{
@@ -285,7 +284,7 @@ func TestCommitShowCmdObj(t *testing.T) {
285284
contextSize: 77,
286285
similarityThreshold: 50,
287286
ignoreWhitespace: false,
288-
extDiffCmd: "",
287+
pagerConfig: nil,
289288
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--"},
290289
},
291290
{
@@ -294,7 +293,7 @@ func TestCommitShowCmdObj(t *testing.T) {
294293
contextSize: 3,
295294
similarityThreshold: 33,
296295
ignoreWhitespace: false,
297-
extDiffCmd: "",
296+
pagerConfig: nil,
298297
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=33%", "--"},
299298
},
300299
{
@@ -303,7 +302,7 @@ func TestCommitShowCmdObj(t *testing.T) {
303302
contextSize: 77,
304303
similarityThreshold: 50,
305304
ignoreWhitespace: true,
306-
extDiffCmd: "",
305+
pagerConfig: nil,
307306
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890", "--ignore-all-space", "--find-renames=50%", "--"},
308307
},
309308
{
@@ -312,7 +311,7 @@ func TestCommitShowCmdObj(t *testing.T) {
312311
contextSize: 3,
313312
similarityThreshold: 50,
314313
ignoreWhitespace: false,
315-
extDiffCmd: "difft --color=always",
314+
pagerConfig: &config.PagingConfig{ExternalDiffCommand: "difft --color=always"},
316315
expected: []string{"-C", "/path/to/worktree", "-c", "diff.external=difft --color=always", "-c", "diff.noprefix=false", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--"},
317316
},
318317
{
@@ -321,19 +320,20 @@ func TestCommitShowCmdObj(t *testing.T) {
321320
contextSize: 3,
322321
similarityThreshold: 50,
323322
ignoreWhitespace: false,
324-
useExtDiffGitConfig: true,
323+
pagerConfig: &config.PagingConfig{UseExternalDiffGitConfig: true},
325324
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--"},
326325
},
327326
}
328327

329328
for _, s := range scenarios {
330329
t.Run(s.testName, func(t *testing.T) {
331330
userConfig := config.GetDefaultConfig()
332-
userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd
331+
if s.pagerConfig != nil {
332+
userConfig.Git.Pagers = []config.PagingConfig{*s.pagerConfig}
333+
}
333334
userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace
334335
userConfig.Git.DiffContextSize = s.contextSize
335336
userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold
336-
userConfig.Git.Paging.UseExternalDiffGitConfig = s.useExtDiffGitConfig
337337

338338
runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expected, "", nil)
339339
repoPaths := RepoPaths{

pkg/commands/git_commands/stash_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
103103
contextSize uint64
104104
similarityThreshold int
105105
ignoreWhitespace bool
106-
extDiffCmd string
107-
useExtDiffGitConfig bool
106+
pagerConfig *config.PagingConfig
108107
expected []string
109108
}
110109

@@ -139,7 +138,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
139138
contextSize: 3,
140139
similarityThreshold: 50,
141140
ignoreWhitespace: false,
142-
extDiffCmd: "difft --color=always",
141+
pagerConfig: &config.PagingConfig{ExternalDiffCommand: "difft --color=always"},
143142
expected: []string{"git", "-C", "/path/to/worktree", "-c", "diff.external=difft --color=always", "stash", "show", "-p", "--stat", "-u", "--ext-diff", "--color=always", "--unified=3", "--find-renames=50%", "refs/stash@{5}"},
144143
},
145144
{
@@ -148,7 +147,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
148147
contextSize: 3,
149148
similarityThreshold: 50,
150149
ignoreWhitespace: false,
151-
useExtDiffGitConfig: true,
150+
pagerConfig: &config.PagingConfig{UseExternalDiffGitConfig: true},
152151
expected: []string{"git", "-C", "/path/to/worktree", "stash", "show", "-p", "--stat", "-u", "--ext-diff", "--color=always", "--unified=3", "--find-renames=50%", "refs/stash@{5}"},
153152
},
154153
{
@@ -167,8 +166,9 @@ func TestStashStashEntryCmdObj(t *testing.T) {
167166
userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace
168167
userConfig.Git.DiffContextSize = s.contextSize
169168
userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold
170-
userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd
171-
userConfig.Git.Paging.UseExternalDiffGitConfig = s.useExtDiffGitConfig
169+
if s.pagerConfig != nil {
170+
userConfig.Git.Pagers = []config.PagingConfig{*s.pagerConfig}
171+
}
172172
repoPaths := RepoPaths{
173173
worktreePath: "/path/to/worktree",
174174
}

pkg/config/pager_config.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,75 @@ import (
88

99
type PagerConfig struct {
1010
getUserConfig func() *UserConfig
11+
pagerIndex int
1112
}
1213

1314
func NewPagerConfig(getUserConfig func() *UserConfig) *PagerConfig {
1415
return &PagerConfig{getUserConfig: getUserConfig}
1516
}
1617

18+
func (self *PagerConfig) currentPagerConfig() *PagingConfig {
19+
pagers := self.getUserConfig().Git.Pagers
20+
if len(pagers) == 0 {
21+
return nil
22+
}
23+
24+
// Guard against the pager index being out of range, which can happen if the user
25+
// has removed pagers from their config file while lazygit is running.
26+
if self.pagerIndex >= len(pagers) {
27+
self.pagerIndex = 0
28+
}
29+
30+
return &pagers[self.pagerIndex]
31+
}
32+
1733
func (self *PagerConfig) GetPagerCommand(width int) string {
34+
currentPagerConfig := self.currentPagerConfig()
35+
if currentPagerConfig == nil {
36+
return ""
37+
}
38+
1839
templateValues := map[string]string{
1940
"columnWidth": strconv.Itoa(width/2 - 6),
2041
}
2142

22-
pagerTemplate := string(self.getUserConfig().Git.Paging.Pager)
43+
pagerTemplate := string(currentPagerConfig.Pager)
2344
return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
2445
}
2546

2647
func (self *PagerConfig) GetColorArg() string {
27-
return self.getUserConfig().Git.Paging.ColorArg
48+
currentPagerConfig := self.currentPagerConfig()
49+
if currentPagerConfig == nil {
50+
return "always"
51+
}
52+
53+
colorArg := currentPagerConfig.ColorArg
54+
if colorArg == "" {
55+
return "always"
56+
}
57+
return colorArg
2858
}
2959

3060
func (self *PagerConfig) GetExternalDiffCommand() string {
31-
return self.getUserConfig().Git.Paging.ExternalDiffCommand
61+
currentPagerConfig := self.currentPagerConfig()
62+
if currentPagerConfig == nil {
63+
return ""
64+
}
65+
return currentPagerConfig.ExternalDiffCommand
3266
}
3367

3468
func (self *PagerConfig) GetUseExternalDiffGitConfig() bool {
35-
return self.getUserConfig().Git.Paging.UseExternalDiffGitConfig
69+
currentPagerConfig := self.currentPagerConfig()
70+
if currentPagerConfig == nil {
71+
return false
72+
}
73+
return currentPagerConfig.UseExternalDiffGitConfig
74+
}
75+
76+
func (self *PagerConfig) CyclePagers() {
77+
self.pagerIndex = (self.pagerIndex + 1) % len(self.getUserConfig().Git.Pagers)
78+
}
79+
80+
func (self *PagerConfig) CurrentPagerIndex() (int, int) {
81+
return self.pagerIndex, len(self.getUserConfig().Git.Pagers)
3682
}

0 commit comments

Comments
 (0)