Try yet another solution for the fight over the bg color (delta vs. selection)

This commit is contained in:
Stefan Haller
2026-08-08 12:59:00 +02:00
parent d355cb58a3
commit f4384add58
9 changed files with 52 additions and 49 deletions
+5 -5
View File
@@ -371,11 +371,11 @@ git:
# # renderer command.
# name: ""
#
# # If greater than zero, selected diff lines are highlighted only at the
# # left and right edges of the view, with this many columns painted on
# # each side. Useful for pagers whose own line background colors should
# # remain visible.
# selectionBgColorEdgeWidth: 0
# # If true, selected diff lines are highlighted only with a narrow bar
# # at the left edge of the view, rather than across the whole width.
# # Useful for pagers whose own line background colors should remain
# # visible.
# narrowSelectionHighlight: false
#
# # Value of the --color arg in the git diff command. Only used for type
# # 'stdinFilter'. Some renderers want this to be set to 'always' and some
+3 -3
View File
@@ -87,12 +87,12 @@ func (self *DiffRendererConfigManager) GetColorArg() string {
return colorArg
}
func (self *DiffRendererConfigManager) GetSelectionBgColorEdgeWidth() int {
func (self *DiffRendererConfigManager) GetNarrowSelectionHighlight() bool {
currentDiffRendererConfig := self.currentDiffRendererConfig()
if currentDiffRendererConfig == nil {
return 0
return false
}
return currentDiffRendererConfig.SelectionBgColorEdgeWidth
return currentDiffRendererConfig.NarrowSelectionHighlight
}
func (self *DiffRendererConfigManager) GetExternalDiffCommand(diffContext uint64) string {
@@ -70,18 +70,18 @@ func TestCurrentDiffRendererNameWithoutDiffRenderers(t *testing.T) {
assert.Equal(t, tr.DefaultDiffRendererName, config.CurrentDiffRendererName(tr))
}
func TestGetSelectionBgColorEdgeWidth(t *testing.T) {
func TestGetNarrowSelectionHighlight(t *testing.T) {
userConfig := &UserConfig{}
userConfig.Git.DiffRenderers = []DiffRendererConfig{{SelectionBgColorEdgeWidth: 12}}
userConfig.Git.DiffRenderers = []DiffRendererConfig{{NarrowSelectionHighlight: true}}
config := NewDiffRendererConfigManager(func() *UserConfig { return userConfig })
assert.Equal(t, 12, config.GetSelectionBgColorEdgeWidth())
assert.Equal(t, true, config.GetNarrowSelectionHighlight())
}
func TestGetSelectionBgColorEdgeWidthWithoutPagers(t *testing.T) {
func TestGetNarrowSelectionHighlightWithoutPagers(t *testing.T) {
config := NewDiffRendererConfigManager(func() *UserConfig { return &UserConfig{} })
assert.Equal(t, 0, config.GetSelectionBgColorEdgeWidth())
assert.Equal(t, false, config.GetNarrowSelectionHighlight())
}
func TestCycleDiffRenderers(t *testing.T) {
+7 -7
View File
@@ -281,11 +281,11 @@ type GitConfig struct {
// # renderer command.
// name: ""
//
// # If greater than zero, selected diff lines are highlighted only at the
// # left and right edges of the view, with this many columns painted on
// # each side. Useful for pagers whose own line background colors should
// # remain visible.
// selectionBgColorEdgeWidth: 0
// # If true, selected diff lines are highlighted only with a narrow bar
// # at the left edge of the view, rather than across the whole width.
// # Useful for pagers whose own line background colors should remain
// # visible.
// narrowSelectionHighlight: false
//
// # Value of the --color arg in the git diff command. Only used for type
// # 'stdinFilter'. Some renderers want this to be set to 'always' and some
@@ -383,8 +383,8 @@ type DiffRendererConfig struct {
Type string `yaml:"type" jsonschema:"enum=stdinFilter,enum=extDiff,enum=rawGit"`
// A name for the diff renderer, shown in the notification when cycling renderers. If not set, the name is derived from the first word of the renderer command.
Name string `yaml:"name"`
// If greater than zero, selected diff lines are highlighted only at the left and right edges of the view, with this many columns painted on each side. Useful for pagers whose own line background colors should remain visible.
SelectionBgColorEdgeWidth int `yaml:"selectionBgColorEdgeWidth" jsonschema:"minimum=0"`
// If true, selected diff lines are highlighted only with a narrow bar at the left edge of the view, rather than across the whole width. Useful for pagers whose own line background colors should remain visible.
NarrowSelectionHighlight bool `yaml:"narrowSelectionHighlight"`
// Value of the --color arg in the git diff command. Only used for type 'stdinFilter'. Some renderers want this to be set to 'always' and some want it set to 'never'.
ColorArg string `yaml:"colorArg" jsonschema:"enum=always,enum=never"`
// The command to use for rendering diffs. This is either a stdinFilter or an external diff command, depending on the type field; not applicable if the type is 'rawGit'.
+5 -5
View File
@@ -156,9 +156,9 @@ type View struct {
// instead of Sel{Bg,Fg}Colors for highlighting selected lines.
HighlightInactive bool
// If SelectedLineBgColorEdgeWidth is greater than zero, selection background
// color is painted only at the left and right edges of highlighted lines.
SelectedLineBgColorEdgeWidth int
// If SelectedLineBgColorWidth is greater than zero, selection background
// color is painted only at the left edge of highlighted lines.
SelectedLineBgColorWidth int
// InclusionGutterMarker is the glyph drawn in the on-demand inclusion gutter
// (see SetInclusionGutter) on marked lines; InclusionGutterMarkerColor is its
@@ -714,8 +714,8 @@ func (v *View) setCharacter(x, y int, ch string, fgColor, bgColor Attribute) {
rangeSelectEnd = max(relativeRangeSelectStart, v.cy)
}
edgeWidth := v.SelectedLineBgColorEdgeWidth
if y >= rangeSelectStart && y <= rangeSelectEnd && (edgeWidth == 0 || x < edgeWidth || x >= v.InnerWidth()-edgeWidth) {
bgColorWidth := v.SelectedLineBgColorWidth
if y >= rangeSelectStart && y <= rangeSelectEnd && (bgColorWidth == 0 || x < bgColorWidth) {
// this ensures we use the bright variant of a colour upon highlight
fgColorComponent := fgColor & ^AttrAll
if fgColorComponent >= AttrIsValidColor && fgColorComponent < AttrIsValidColor+8 {
+11 -11
View File
@@ -159,21 +159,21 @@ func TestAutoRenderingHyperlinks(t *testing.T) {
assert.Equal(t, "https://example.com", v.buf.lines[0].cells[0].hyperlink)
}
func TestSelectedLineBgColorEdgeWidth(t *testing.T) {
func TestSelectedLineBgColorWidth(t *testing.T) {
tests := []struct {
name string
edgeWidth int
selected func(screenX int) bool
name string
bgColorWidth int
selected func(screenX int) bool
}{
{
name: "zero uses full-width selection background",
edgeWidth: 0,
selected: func(_ int) bool { return true },
name: "zero uses full-width selection background",
bgColorWidth: 0,
selected: func(_ int) bool { return true },
},
{
name: "non-zero uses edge-only selection background",
edgeWidth: 2,
selected: func(screenX int) bool { return screenX <= 2 || screenX >= 9 },
name: "non-zero uses left-edge selection background",
bgColorWidth: 2,
selected: func(screenX int) bool { return screenX <= 2 },
},
}
@@ -184,7 +184,7 @@ func TestSelectedLineBgColorEdgeWidth(t *testing.T) {
v := NewView("name", 0, 0, 11, 3, OutputNormal) // InnerWidth=10
v.Highlight = true
v.SelBgColor = ColorBlue
v.SelectedLineBgColorEdgeWidth = test.edgeWidth
v.SelectedLineBgColorWidth = test.bgColorWidth
v.writeString("0123456789\n")
v.draw()
+6 -3
View File
@@ -214,9 +214,12 @@ func (self *GlobalController) onDiffRenderersChanged() {
}
func (self *GlobalController) applyCurrentPagerSelectionStyle() {
edgeWidth := self.c.State().GetDiffRendererConfigManager().GetSelectionBgColorEdgeWidth()
self.c.Contexts().Normal.GetView().SelectedLineBgColorEdgeWidth = edgeWidth
self.c.Contexts().NormalSecondary.GetView().SelectedLineBgColorEdgeWidth = edgeWidth
bgColorWidth := 0
if self.c.State().GetDiffRendererConfigManager().GetNarrowSelectionHighlight() {
bgColorWidth = 2
}
self.c.Contexts().Normal.GetView().SelectedLineBgColorWidth = bgColorWidth
self.c.Contexts().NormalSecondary.GetView().SelectedLineBgColorWidth = bgColorWidth
}
func (self *GlobalController) canCycleDiffRenderers() *types.DisabledReason {
+6 -5
View File
@@ -164,13 +164,14 @@ func (gui *Gui) createAllViews() error {
}
func (gui *Gui) applyCurrentPagerSelectionStyle() {
edgeWidth := 0
if gui.stateAccessor != nil && gui.stateAccessor.GetDiffRendererConfigManager() != nil {
edgeWidth = gui.stateAccessor.GetDiffRendererConfigManager().GetSelectionBgColorEdgeWidth()
bgColorWidth := 0
if gui.stateAccessor != nil && gui.stateAccessor.GetDiffRendererConfigManager() != nil &&
gui.stateAccessor.GetDiffRendererConfigManager().GetNarrowSelectionHighlight() {
bgColorWidth = 2
}
gui.Views.Main.SelectedLineBgColorEdgeWidth = edgeWidth
gui.Views.Secondary.SelectedLineBgColorEdgeWidth = edgeWidth
gui.Views.Main.SelectedLineBgColorWidth = bgColorWidth
gui.Views.Secondary.SelectedLineBgColorWidth = bgColorWidth
}
func (gui *Gui) configureViewProperties() {
+4 -5
View File
@@ -329,10 +329,9 @@
"type": "string",
"description": "A name for the diff renderer, shown in the notification when cycling renderers. If not set, the name is derived from the first word of the renderer command."
},
"selectionBgColorEdgeWidth": {
"type": "integer",
"minimum": 0,
"description": "If greater than zero, selected diff lines are highlighted only at the left and right edges of the view, with this many columns painted on each side. Useful for pagers whose own line background colors should remain visible."
"narrowSelectionHighlight": {
"type": "boolean",
"description": "If true, selected diff lines are highlighted only with a narrow bar at the left edge of the view, rather than across the whole width. Useful for pagers whose own line background colors should remain visible."
},
"colorArg": {
"type": "string",
@@ -370,7 +369,7 @@
"$ref": "#/$defs/DiffRendererConfig"
},
"type": "array",
"description": "Array of diff renderers. Each entry has the following format:\n\n # The type of diff renderer. One of: 'stdinFilter' (default) | 'extDiff'\n # | 'rawGit'\n type: \"stdinFilter\"\n\n # A name for the diff renderer, shown in the notification when cycling\n # renderers. If not set, the name is derived from the first word of the\n # renderer command.\n name: \"\"\n\n # If greater than zero, selected diff lines are highlighted only at the\n # left and right edges of the view, with this many columns painted on\n # each side. Useful for pagers whose own line background colors should\n # remain visible.\n selectionBgColorEdgeWidth: 0\n\n # Value of the --color arg in the git diff command. Only used for type\n # 'stdinFilter'. Some renderers want this to be set to 'always' and some\n # want it set to 'never'.\n colorArg: \"always\"\n\n # The command to use for rendering diffs. This is either a stdinFilter or\n # an external diff command, depending on the type field; not applicable if\n # the type is 'rawGit'.\n # e.g.\n # diff-so-fancy\n # delta --dark --paging=never\n # ydiff -p cat\n # difft --color=always\n command: \"\"\n\n # Extra arguments (array of strings) passed to the git command. Only\n # applicable if the type is 'rawGit'.\n args: []\n\nSee https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_DiffRenderers.md for more information."
"description": "Array of diff renderers. Each entry has the following format:\n\n # The type of diff renderer. One of: 'stdinFilter' (default) | 'extDiff'\n # | 'rawGit'\n type: \"stdinFilter\"\n\n # A name for the diff renderer, shown in the notification when cycling\n # renderers. If not set, the name is derived from the first word of the\n # renderer command.\n name: \"\"\n\n # If true, selected diff lines are highlighted only with a narrow bar\n # at the left edge of the view, rather than across the whole width.\n # Useful for pagers whose own line background colors should remain\n # visible.\n narrowSelectionHighlight: false\n\n # Value of the --color arg in the git diff command. Only used for type\n # 'stdinFilter'. Some renderers want this to be set to 'always' and some\n # want it set to 'never'.\n colorArg: \"always\"\n\n # The command to use for rendering diffs. This is either a stdinFilter or\n # an external diff command, depending on the type field; not applicable if\n # the type is 'rawGit'.\n # e.g.\n # diff-so-fancy\n # delta --dark --paging=never\n # ydiff -p cat\n # difft --color=always\n command: \"\"\n\n # Extra arguments (array of strings) passed to the git command. Only\n # applicable if the type is 'rawGit'.\n args: []\n\nSee https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_DiffRenderers.md for more information."
},
"commit": {
"$ref": "#/$defs/CommitConfig",