Skip to content

Commit 6fbd99a

Browse files
committed
WIP Add config pagerForStaging
Problems: - sometimes the wrapping is off; I suspect Decolorize is missing some cases - in the patch building panel, only shows the formatted diff as long as nothing has been staged, because then it wants to color the line starts of the included lines - with delta, selection is almost invisible because we are only changing the background color, but delta colors the background too (diff-so-fancy doesn't)
1 parent 4e38a94 commit 6fbd99a

File tree

6 files changed

+74
-5
lines changed

6 files changed

+74
-5
lines changed

docs/Config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ git:
280280
# ydiff -p cat -s --wrap --width={{columnWidth}}
281281
pager: ""
282282

283+
# e.g.
284+
# delta --dark --paging=never --color-only
285+
# diff-so-fancy --patch
286+
pagerForStaging: ""
287+
283288
# If true, Lazygit will use whatever pager is specified in `$GIT_PAGER`, `$PAGER`, or your *git config*. If the pager ends with something like ` | less` we will strip that part out, because less doesn't play nice with our rendering approach. If the custom pager uses less under the hood, that will also break rendering (hence the `--paging=never` flag for the `delta` pager).
284289
useConfig: false
285290

pkg/config/user_config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ func (PagerType) JSONSchemaExtend(schema *jsonschema.Schema) {
284284
}
285285
}
286286

287+
type PagerForStagingType string
288+
289+
func (PagerForStagingType) JSONSchemaExtend(schema *jsonschema.Schema) {
290+
schema.Examples = []any{
291+
"delta --dark --paging=never --color-only",
292+
"diff-so-fancy --patch",
293+
}
294+
}
295+
287296
type PagingConfig struct {
288297
// Value of the --color arg in the git diff command. Some pagers want this to be set to 'always' and some want it set to 'never'
289298
ColorArg string `yaml:"colorArg" jsonschema:"enum=always,enum=never"`
@@ -292,6 +301,10 @@ type PagingConfig struct {
292301
// delta --dark --paging=never
293302
// ydiff -p cat -s --wrap --width={{columnWidth}}
294303
Pager PagerType `yaml:"pager"`
304+
// e.g.
305+
// delta --dark --paging=never --color-only
306+
// diff-so-fancy --patch
307+
PagerForStaging PagerForStagingType `yaml:"pagerForStaging"`
295308
// If true, Lazygit will use whatever pager is specified in `$GIT_PAGER`, `$PAGER`, or your *git config*. If the pager ends with something like ` | less` we will strip that part out, because less doesn't play nice with our rendering approach. If the custom pager uses less under the hood, that will also break rendering (hence the `--paging=never` flag for the `delta` pager).
296309
UseConfig bool `yaml:"useConfig"`
297310
// e.g. 'difft --color=always'

pkg/gui/controllers/helpers/patch_building_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpt
9191

9292
oldState := context.GetState()
9393

94-
state := patch_exploring.NewState(diff, selectedLineIdx, context.GetView(), oldState)
94+
state := patch_exploring.NewState(diff, selectedLineIdx, context.GetView(), oldState, string(self.c.UserConfig().Git.Paging.PagerForStaging))
9595
context.SetState(state)
9696
if state == nil {
9797
self.Escape()

pkg/gui/controllers/helpers/staging_helper.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ func (self *StagingHelper) RefreshStagingPanel(focusOpts types.OnFocusOpts) {
6262
mainContext.GetMutex().Lock()
6363
secondaryContext.GetMutex().Lock()
6464

65+
pagerCommand := string(self.c.UserConfig().Git.Paging.PagerForStaging)
66+
6567
mainContext.SetState(
66-
patch_exploring.NewState(mainDiff, mainSelectedLineIdx, mainContext.GetView(), mainContext.GetState()),
68+
patch_exploring.NewState(mainDiff, mainSelectedLineIdx, mainContext.GetView(), mainContext.GetState(), pagerCommand),
6769
)
6870

6971
secondaryContext.SetState(
70-
patch_exploring.NewState(secondaryDiff, secondarySelectedLineIdx, secondaryContext.GetView(), secondaryContext.GetState()),
72+
patch_exploring.NewState(secondaryDiff, secondarySelectedLineIdx, secondaryContext.GetView(), secondaryContext.GetState(), pagerCommand),
7173
)
7274

7375
mainState := mainContext.GetState()

pkg/gui/patch_exploring/state.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package patch_exploring
22

33
import (
4+
"bytes"
5+
"io"
6+
"os/exec"
47
"strings"
58

69
"github.com/jesseduffield/generics/set"
710
"github.com/jesseduffield/gocui"
811
"github.com/jesseduffield/lazygit/pkg/commands/patch"
912
"github.com/jesseduffield/lazygit/pkg/utils"
13+
"github.com/mgutz/str"
1014
)
1115

1216
// State represents the current state of the patch explorer context i.e. when
@@ -20,6 +24,7 @@ type State struct {
2024
// Otherwise, we cancel the range when we move up or down.
2125
rangeIsSticky bool
2226
diff string
27+
diffFromPager string
2328
patch *patch.Patch
2429
selectMode selectMode
2530

@@ -38,7 +43,7 @@ const (
3843
HUNK
3944
)
4045

41-
func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *State) *State {
46+
func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *State, pagerCommand string) *State {
4247
if oldState != nil && diff == oldState.diff && selectedLineIdx == -1 {
4348
// if we're here then we can return the old state. If selectedLineIdx was not -1
4449
// then that would mean we were trying to click and potentially drag a range, which
@@ -52,7 +57,13 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
5257
return nil
5358
}
5459

55-
viewLineIndices, patchLineIndices := wrapPatchLines(diff, view)
60+
diffFromPager := formatDiffForStaging(diff, pagerCommand)
61+
diffToWrap := diff
62+
if len(diffFromPager) > 0 {
63+
diffToWrap = utils.Decolorise(diffFromPager)
64+
}
65+
66+
viewLineIndices, patchLineIndices := wrapPatchLines(diffToWrap, view)
5667

5768
rangeStartLineIdx := 0
5869
if oldState != nil {
@@ -85,11 +96,37 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
8596
rangeStartLineIdx: rangeStartLineIdx,
8697
rangeIsSticky: false,
8798
diff: diff,
99+
diffFromPager: diffFromPager,
88100
viewLineIndices: viewLineIndices,
89101
patchLineIndices: patchLineIndices,
90102
}
91103
}
92104

105+
func formatDiffForStaging(diff string, pagerCommand string) string {
106+
if len(pagerCommand) == 0 {
107+
return ""
108+
}
109+
110+
args := str.ToArgv(pagerCommand)
111+
cmd := exec.Command(args[0], args[1:]...)
112+
var stdout bytes.Buffer
113+
cmd.Stdout = &stdout
114+
cmd.Stderr = io.Discard
115+
cmd.Stdin = strings.NewReader(diff)
116+
117+
err := cmd.Run()
118+
if err != nil {
119+
// TODO report error somehow
120+
return ""
121+
}
122+
diffFromPager := stdout.String()
123+
if len(strings.Split(diffFromPager, "\n")) != len(strings.Split(diff, "\n")) {
124+
// TODO report error somehow
125+
return ""
126+
}
127+
return diffFromPager
128+
}
129+
93130
func (s *State) OnViewWidthChanged(view *gocui.View) {
94131
if !view.Wrap {
95132
return
@@ -294,6 +331,9 @@ func (s *State) AdjustSelectedLineIdx(change int) {
294331
}
295332

296333
func (s *State) RenderForLineIndices(includedLineIndices []int) string {
334+
if includedLineIndices == nil && len(s.diffFromPager) > 0 {
335+
return s.diffFromPager
336+
}
297337
includedLineIndicesSet := set.NewFromSlice(includedLineIndices)
298338
return s.patch.FormatView(patch.FormatViewOpts{
299339
IncLineIndices: includedLineIndicesSet,

schema/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,15 @@
15481548
"ydiff -p cat -s --wrap --width={{columnWidth}}"
15491549
]
15501550
},
1551+
"pagerForStaging": {
1552+
"type": "string",
1553+
"description": "e.g.\ndelta --dark --paging=never --color-only\ndiff-so-fancy --patch",
1554+
"default": "",
1555+
"examples": [
1556+
"delta --dark --paging=never --color-only",
1557+
"diff-so-fancy --patch"
1558+
]
1559+
},
15511560
"useConfig": {
15521561
"type": "boolean",
15531562
"description": "If true, Lazygit will use whatever pager is specified in `$GIT_PAGER`, `$PAGER`, or your *git config*. If the pager ends with something like ` | less` we will strip that part out, because less doesn't play nice with our rendering approach. If the custom pager uses less under the hood, that will also break rendering (hence the `--paging=never` flag for the `delta` pager).",

0 commit comments

Comments
 (0)