Resolve the commands of all kinds of diff renderers in one place

The getters for the stdin filter and the external diff command each
take the values they fill in as parameters of their own, and each
builds the placeholders for them. The next commit checks the commands
when the config is loaded, and for that it needs to resolve a command
whatever its kind. A value that both kinds can use comes after that.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-09-27 08:09:45 +02:00
co-authored by Claude Opus 5.5
parent d2519cdf26
commit 4ce8d77173
3 changed files with 32 additions and 17 deletions
+27 -13
View File
@@ -60,19 +60,21 @@ func (self *DiffRendererConfigManager) GetDiffRendererType() DiffRendererType {
return currentDiffRendererConfig.getType()
}
func (self *DiffRendererConfigManager) GetStdinFilterCommand(width int) string {
// DiffRendererValues are what the command of a diff renderer can refer to.
type DiffRendererValues struct {
// The width of the view that the diff is rendered into
Width int
// The number of lines of context around each hunk
DiffContext uint64
}
func (self *DiffRendererConfigManager) GetStdinFilterCommand(values DiffRendererValues) string {
currentDiffRendererConfig := self.currentDiffRendererConfig()
if currentDiffRendererConfig == nil || currentDiffRendererConfig.getType() != DiffRendererType_StdinFilter {
return ""
}
templateValues := map[string]string{
"width": strconv.Itoa(width),
"columnWidth": strconv.Itoa(width/2 - 6),
}
commandTemplate := string(currentDiffRendererConfig.Command)
return utils.ResolvePlaceholderString(commandTemplate, templateValues)
return currentDiffRendererConfig.resolveCommand(values)
}
func (self *DiffRendererConfigManager) GetColorArg() string {
@@ -88,18 +90,30 @@ func (self *DiffRendererConfigManager) GetColorArg() string {
return colorArg
}
func (self *DiffRendererConfigManager) GetExternalDiffCommand(diffContext uint64, width int) string {
func (self *DiffRendererConfigManager) GetExternalDiffCommand(values DiffRendererValues) string {
currentDiffRendererConfig := self.currentDiffRendererConfig()
if currentDiffRendererConfig == nil || currentDiffRendererConfig.getType() != DiffRendererType_ExtDiff {
return ""
}
templateValues := map[string]string{
"diffContext": strconv.Itoa(int(diffContext)),
"width": strconv.Itoa(width),
return currentDiffRendererConfig.resolveCommand(values)
}
// resolveCommand fills in the values that the renderer's command refers to.
func (self *DiffRendererConfig) resolveCommand(values DiffRendererValues) string {
placeholders := map[string]string{
"width": strconv.Itoa(values.Width),
}
switch self.getType() {
case DiffRendererType_StdinFilter:
placeholders["columnWidth"] = strconv.Itoa(values.Width/2 - 6)
case DiffRendererType_ExtDiff:
placeholders["diffContext"] = strconv.Itoa(int(values.DiffContext))
case DiffRendererType_RawGit:
// has no command
}
return utils.ResolvePlaceholderString(string(currentDiffRendererConfig.Command), templateValues)
return utils.ResolvePlaceholderString(string(self.Command), placeholders)
}
func (self *DiffRendererConfigManager) GetRawGitArgs() []string {
@@ -108,7 +108,7 @@ func TestGetStdinFilterCommand(t *testing.T) {
userConfig.Git.DiffRenderers = []DiffRendererConfig{s.diffRendererConfig}
config := NewDiffRendererConfigManager(func() *UserConfig { return userConfig })
assert.Equal(t, s.expected, config.GetStdinFilterCommand(s.width))
assert.Equal(t, s.expected, config.GetStdinFilterCommand(DiffRendererValues{Width: s.width}))
})
}
}
@@ -147,7 +147,7 @@ func TestGetExternalDiffCommand(t *testing.T) {
userConfig.Git.DiffRenderers = []DiffRendererConfig{s.diffRendererConfig}
config := NewDiffRendererConfigManager(func() *UserConfig { return userConfig })
assert.Equal(t, s.expected, config.GetExternalDiffCommand(3, 120))
assert.Equal(t, s.expected, config.GetExternalDiffCommand(DiffRendererValues{Width: 120, DiffContext: 3}))
})
}
}
+3 -2
View File
@@ -56,8 +56,9 @@ func (gui *Gui) newRenderTask(view *gocui.View, cmd *exec.Cmd, prefix string) er
// width to render at known, and with it the renderer command.
width := view.InnerWidth()
diffRendererConfigManager := gui.stateAccessor.GetDiffRendererConfigManager()
stdinFilter := diffRendererConfigManager.GetStdinFilterCommand(width)
externalDiff := diffRendererConfigManager.GetExternalDiffCommand(gui.c.UserConfig().Git.DiffContextSize, width)
values := config.DiffRendererValues{Width: width, DiffContext: gui.c.UserConfig().Git.DiffContextSize}
stdinFilter := diffRendererConfigManager.GetStdinFilterCommand(values)
externalDiff := diffRendererConfigManager.GetExternalDiffCommand(values)
cmdStr := strings.Join(cmd.Args, " ")