Visitar URL original
Ask diff renderers for OSC 1717 metadata · jesseduffield/lazygit@661842c · GitHub
Skip to content

Commit 661842c

Browse files
stefanhallerclaude
andcommitted
Ask diff renderers for OSC 1717 metadata
A renderer that speaks the protocol emits nothing unless it is asked to, so that its output stays plain wherever it is used outside lazygit. The variable names the versions we understand. git is one of the renderers we ask. It has no pager to spawn, and so no terminal to spawn one in, but it still renders the diff itself — for the word-diff formats, whose markup nothing else could resolve — so the request has to be made before we decide a pty isn't needed. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent fdd60ff commit 661842c

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎pkg/gui/main_view_render.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ type renderSpec struct {
3535
// user has configured. The renderer lays its rendering out to the width of the
3636
// view, which only the layout settles, so the task is created after it.
3737
func (gui *Gui) newRenderTask(view *gocui.View, cmd *exec.Cmd, prefix string) error {
38+
// Ask whatever renders the diff to state, in an OSC 1717 record per line,
39+
// which line of which file it is rendering. This lets us act on the line the
40+
// user is pointing at even when the rendering no longer looks like a diff.
41+
// The variable names the protocol versions we understand, and a renderer
42+
// that doesn't understand it ignores it, so we can set it always. It has to
43+
// be set before the plain path below, since on that path git renders the
44+
// diff itself, and git speaks the protocol too, for its word-diff formats,
45+
// whose markup we could not otherwise resolve.
46+
cmd.Env = append(cmd.Env, "OSC1717=V1")
47+
3848
if gui.stateAccessor.GetDiffRendererConfigManager().GetDiffRendererType() == config.DiffRendererType_RawGit {
3949
// If we're not using a custom diff renderer, then we don't need to use a pty
4050
return gui.newCmdTask(view, cmd, prefix)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package diff
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var DiffRendererMetadata = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "A diff renderer is told that we understand the OSC 1717 metadata protocol, and the records it emits don't show up in the rendered diff",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(cfg *config.AppConfig) {
13+
// A fake conforming renderer: it announces the protocol with a
14+
// version-only record, reports the protocol versions it was offered, and
15+
// then passes the diff through with a per-line record in front of every
16+
// line.
17+
cfg.GetUserConfig().Git.DiffRenderers = []config.DiffRendererConfig{
18+
{Command: `printf '\033]1717;1\007'; ` +
19+
`printf 'OFFERED:%s\n' "$OSC1717"; ` +
20+
`while IFS= read -r line; do printf '\033]1717;1;c;1;;file1\007%s\n' "$line"; done`},
21+
}
22+
},
23+
SetupRepo: func(shell *Shell) {
24+
shell.CreateFileAndAdd("file1", "one\ntwo\n")
25+
shell.Commit("one")
26+
},
27+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
28+
t.Views().Commits().
29+
Focus().
30+
Lines(
31+
Contains("one").IsSelected(),
32+
)
33+
34+
t.Views().Main().
35+
// The renderer was offered the protocol version we understand.
36+
Content(Contains("OFFERED:V1")).
37+
// Its records are escape sequences, so none of them reaches the
38+
// screen; the diff reads exactly as the renderer wrote it.
39+
ContainsLines(
40+
Equals("diff --git a/file1 b/file1"),
41+
Contains("new file mode"),
42+
Contains("index "),
43+
Equals("--- /dev/null"),
44+
Equals("+++ b/file1"),
45+
Equals("@@ -0,0 +1,2 @@"),
46+
Equals("+one"),
47+
Equals("+two"),
48+
)
49+
},
50+
})

‎pkg/integration/tests/test_list.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ var tests = []*components.IntegrationTest{
228228
diff.DiffAndApplyPatch,
229229
diff.DiffCommits,
230230
diff.DiffNonStickyRange,
231+
diff.DiffRendererMetadata,
231232
diff.IgnoreWhitespace,
232233
diff.RenameSimilarityThresholdChange,
233234
diff.RenderThroughAPipe,

0 commit comments

Comments
 (0)