Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
57dfac8
feat: parse delta's OSC 1717 line metadata
pablospe Nov 14, 2026
b6f55b3
fix: keep the first row that carries the OSC 1717 handshake
pablospe Nov 14, 2026
45d9dac
feat: keep the scroll position when toggling side-by-side or resizing
pablospe Nov 14, 2026
17254ce
fix: take the scroll anchor before the viewport is resized
pablospe Nov 14, 2026
3c5533e
fix: keep rows without a record in place when restoring the scroll
pablospe Nov 14, 2026
f25396b
refactor: drop the nearest-line fallback and skip rows without records
pablospe Nov 14, 2026
fe2ec61
test: run the real delta without the user's git config
pablospe Nov 14, 2026
0f2c9b6
fix: keep a wrapped-row offset within the anchor line's rows
pablospe Nov 14, 2026
71eb9b4
style: wrap a long test line for golines
pablospe Nov 14, 2026
f0e7568
fix: don't restore the scroll position after the user moved
pablospe Nov 15, 2026
654d4f5
fix: keep the scroll anchor when a scroll call doesn't move
pablospe Nov 15, 2026
67221b4
fix: follow a move made while the re-render is pending
pablospe Nov 15, 2026
6d05cce
docs: say how rows without a record and wrapped rows are anchored
pablospe Nov 15, 2026
18286d9
fix: unquote octal-escaped file names in OSC 1717 records
pablospe Nov 15, 2026
a98623c
fix: don't read records of the previous node while a new one renders
pablospe Nov 25, 2026
f552006
fix: unquote file names that keep git's quotes in OSC 1717 records
pablospe Nov 25, 2026
51b689b
refactor: compute the restored row in a pure anchorRow
pablospe Nov 26, 2026
ed42303
refactor: track view moves in one place
pablospe Nov 26, 2026
e674a5e
refactor: cut OSC 1717 sequences with strings.Cut and unquote names i…
pablospe Nov 26, 2026
74166ba
test: share the line and model fixtures of the scroll tests
pablospe Nov 26, 2026
e2250d9
docs: leave the offset rules to anchorRow's comments
pablospe Nov 26, 2026
771a561
refactor: reanchor after moves without a closure, and jump to edges w…
pablospe Nov 26, 2026
1cf712e
fix: keep g/G pressed at the edge while a re-render is pending
pablospe Nov 26, 2026
1921186
fix: don't overwrite a pending anchor when another re-render starts
pablospe Nov 26, 2026
b95383b
refactor: match records with slices helpers and check the file in sam…
pablospe Nov 26, 2026
45ab706
test: test anchorRow, and sharpen and share the scroll test fixtures
pablospe Nov 26, 2026
e1bda73
test: keep the line scrolled to with the wheel during a pending render
pablospe Nov 26, 2026
0778ecf
test: share the delivery of the pending render in a helper
pablospe Nov 26, 2026
d1f2aba
test: keep G when the wheel doesn't move at the bottom
pablospe Nov 26, 2026
3ca4c08
chore: slight renaming and docs
dlvhdr Nov 30, 2026
dbd8746
refactor: share the row scrolling of setLines
pablospe Nov 14, 2026
012cccb
feat: jump to the next / previous hunk with ] and [
pablospe Nov 14, 2026
6d09a37
fix: find hunks that delta shows without a header
pablospe Nov 14, 2026
8d4d3e9
fix: don't restore the scroll position after a hunk jump
pablospe Nov 15, 2026
f199840
fix: split headerless hunks where a deleted line skips an old line
pablospe Nov 15, 2026
d0fbeec
fix: land a hunk jump made during a pending render on the hunk
pablospe Nov 15, 2026
a9cce8b
docs: say a hunk jump can land on a hunk's first line
pablospe Nov 15, 2026
2956ce3
style: shorten a test comment for golines
pablospe Nov 15, 2026
af5aaac
docs: note that delta main gives headerless hunks an h record
pablospe Nov 15, 2026
2accf48
fix: keep counting from a hunk the view can't scroll to the top
pablospe Nov 25, 2026
7d305cd
refactor: anchor a hunk jump on its record without an offset
pablospe Nov 25, 2026
4fe740b
test: ignore a hunk jump while showing matching lines only
pablospe Nov 25, 2026
3916237
test: find the hunks of real delta output without h records
pablospe Nov 25, 2026
6a63848
test: deliver the pending render with the shared helper
pablospe Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ ui:
| <kbd>k</kbd> | Previous node |
| <kbd>n</kbd> | Next file |
| <kbd>p</kbd> / <kbd>N</kbd> | Previous file |
| <kbd>]</kbd> | Next hunk |
| <kbd>[</kbd> | Previous hunk |
| <kbd>Ctrl-d</kbd> | Scroll the diff half page down |
| <kbd>Ctrl-u</kbd> | Scroll the diff half page up |
| <kbd>Ctrl-e</kbd> | Scroll the diff one line down |
Expand Down
12 changes: 12 additions & 0 deletions pkg/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type KeyMap struct {
Top key.Binding
NextFile key.Binding
PrevFile key.Binding
NextHunk key.Binding
PrevHunk key.Binding
CtrlD key.Binding
CtrlU key.Binding
CtrlE key.Binding
Expand Down Expand Up @@ -75,6 +77,14 @@ var keys = &KeyMap{
key.WithKeys("p", "N"),
key.WithHelp("p/N", "prev file"),
),
NextHunk: key.NewBinding(
key.WithKeys("]"),
key.WithHelp("]", "next hunk"),
),
PrevHunk: key.NewBinding(
key.WithKeys("["),
key.WithHelp("[", "prev hunk"),
),
CtrlD: key.NewBinding(
key.WithKeys("ctrl+d"),
key.WithHelp("ctrl+d", "diff half page down"),
Expand Down Expand Up @@ -182,6 +192,8 @@ func KeyGroups() [][]key.Binding {
keys.Bottom,
keys.NextFile,
keys.PrevFile,
keys.NextHunk,
keys.PrevHunk,
keys.CtrlD,
keys.CtrlU,
keys.ScrollLeft,
Expand Down
8 changes: 6 additions & 2 deletions pkg/ui/panes/diffviewer/delta_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ func TestMakeDeltaArgs(t *testing.T) {
func TestMakeDeltaEnv(t *testing.T) {
t.Setenv("GIT_CONFIG_PARAMETERS", "'delta.navigate'='true'")

if got := makeDeltaEnv(deltaOpts{SideBySide: true}); !slices.Equal(got, os.Environ()) {
t.Fatalf("themed: expected the plain environment, got %q", got)
wantEnv := append(os.Environ(), "OSC1717=V1")
if got := makeDeltaEnv(deltaOpts{SideBySide: true}); !slices.Equal(got, wantEnv) {
t.Fatalf("themed: expected %q, got %q", wantEnv, got)
}

env := makeDeltaEnv(deltaOpts{UseUserDeltaTheme: true})
if !slices.Contains(env, "OSC1717=V1") {
t.Errorf("user delta theme: expected OSC1717=V1 in %q", env)
}
got := env[len(env)-1]
want := "GIT_CONFIG_PARAMETERS='delta.navigate'='true' 'delta.side-by-side'='false'"
if got != want {
Expand Down
211 changes: 198 additions & 13 deletions pkg/ui/panes/diffviewer/diffviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func cacheKey(path string, sideBySide bool) string {
}

type diffLine struct {
item item.Item
item item.Item
records []oscRecord
}

func (o diffLine) GetItem() item.Item {
Expand Down Expand Up @@ -95,8 +96,36 @@ type Model struct {
useUserDeltaTheme bool
preamble string
sb common.Scrollbar
// shown are the lines on screen: the last render while a new one is pending.
shown []diffLine
// anchor is where to scroll back to once the re-rendered diff is shown.
anchor *scrollAnchor
// jump is the last hunk jump, for one the view couldn't scroll to the top.
jump *hunkJump
}

// hunkJump is the row a hunk jump went to and the top row the view stopped at.
type hunkJump struct{ row, top int }

// scrollAnchor is where to scroll back to after a re-render
type scrollAnchor struct {
// whether the user explicitly navigated to the top or bottom
edge anchorEdge

// otherwise - the position the view was in: the line's osc record and the view's offset from its first row.
oscRec oscRecord
offset int
}

// anchorEdge is the edge of the diff an anchor sticks to, if any.
type anchorEdge int

const (
edgeNone anchorEdge = iota
edgeTop
edgeBottom
)

// SetPreamble stores the preamble text (e.g. commit metadata from git show).
func (m *Model) SetPreamble(preamble string) {
m.preamble = preamble
Expand Down Expand Up @@ -228,13 +257,23 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}
node.diff = msg.lines
if node == m.file || node == m.dir {
m.fvp.SetObjects(msg.lines)
m.setLines(msg.lines)
}
}

edge := edgeNone
if k, ok := msg.(tea.KeyPressMsg); ok && !m.fvp.FilterFocused() {
if key.Matches(k, ViewportKeyMap.Top) {
edge = edgeTop
} else if key.Matches(k, ViewportKeyMap.Bottom) {
edge = edgeBottom
}
}
row := m.currentRow()
fvp, fvpCmd := m.fvp.Update(msg)
cmds = append(cmds, fvpCmd)
m.fvp = fvp
m.reanchor(row, edge)

return m, tea.Batch(cmds...)
}
Expand All @@ -257,6 +296,8 @@ func (m Model) View() string {
}

func (m *Model) SetSize(width, height int) tea.Cmd {
// Before resizing: the viewport clamps its top row to the new size.
m.saveAnchor()
m.Width = width
m.Height = height
m.fvp.SetWidth(m.contentWidth())
Expand All @@ -266,10 +307,145 @@ func (m *Model) SetSize(width, height int) tea.Cmd {
}

func (m *Model) UpdateTheme() tea.Cmd {
m.saveAnchor()
m.ClearCache()
return m.diff()
}

// saveAnchor remembers the diff osc record at the top of the view (or the selected one) and
// the view's offset from its first row, so the next render scrolls back to it (anchorRow).
func (m *Model) saveAnchor() {
// there's already a valid anchor
if m.anchor != nil {
return
}
if rec, ok := m.currentOSCRecord(); ok {
m.anchor = &scrollAnchor{oscRec: rec, offset: m.currentRow() - findOSCRecord(m.shown, rec)}
}
}

// currentOSCRecord returns the first record at or below the current row of the lines on
// screen, so it is correct even while a new render is pending.
func (m *Model) currentOSCRecord() (oscRecord, bool) {
row := m.currentRow()
if row < 0 || m.fvp.GetMatchingItemsOnly() {
return oscRecord{}, false
}
return firstOSCRecord(m.shown, row)
}

// currentRow is the selected row, or the top visible one without a selection.
func (m *Model) currentRow() int {
if m.SelectionEnabled() {
return m.fvp.GetSelectedItemIdx()
}
return m.fvp.GetItemMetrics().FirstVisibleItemIdx
}

// reanchor keeps a pending render from undoing a move the user made from row: a jump to
// the top or bottom (edge) stays there, other moves keep the line they land on.
func (m *Model) reanchor(row int, edge anchorEdge) {
if m.anchor == nil {
return
}
if edge != edgeNone {
m.anchor = &scrollAnchor{edge: edge}
return
}
if m.currentRow() == row {
return
}
m.anchor = nil
m.saveAnchor()
}

// GoToHunk moves to the next (dir > 0) or previous (dir < 0) hunk start (its header, or
// its first line when delta prints none): it scrolls it to the top, or selects it in
// selection mode. It stays put at the last / first hunk and without OSC 1717 records.
// Near the end of the diff the view can't scroll a hunk to the top: the next jump then
// counts from that hunk, not from the top row, while the view still shows the same top row.
func (m *Model) GoToHunk(dir int) {
if m.fvp.GetMatchingItemsOnly() {
return
}
from := m.currentRow()
if m.jump != nil && m.jump.top == from {
from = m.jump.row
}
row := hunkRow(m.shown, from, dir)
if row < 0 {
return
}
m.showRow(row)
m.jump = &hunkJump{row: row, top: m.currentRow()}
// A pending render lands on the hunk, not where the view was before.
if m.anchor != nil {
rec, ok := hunkRecord(m.shown[row])
if !ok {
rec = m.shown[row].records[0]
}
m.anchor = &scrollAnchor{oscRec: rec} // a hunk start is the first row of its record
}
}

// setLines shows lines and scrolls to the pending anchor, if any.
func (m *Model) setLines(lines []diffLine) {
m.shown = lines
m.jump = nil
m.fvp.SetObjects(lines)
if m.anchor == nil {
return
}
a := *m.anchor
m.anchor = nil
switch a.edge {
case edgeTop:
m.fvp.GoToTop()
return
case edgeBottom:
m.fvp.GoToBottom()
return
}
if m.fvp.GetMatchingItemsOnly() {
return
}
row, ok := anchorRow(lines, a.oscRec, a.offset)
if !ok {
return
}
m.showRow(row)
}

// showRow selects row in selection mode, or scrolls it to the top.
func (m *Model) showRow(row int) {
if m.SelectionEnabled() {
m.fvp.SetSelectedItemIdx(row)
return
}
m.fvp.GoToTop()
m.fvp.ScrollDown(row)
}

// anchorRow returns the row offset rows from rec's first row, or false when rec is gone.
// Rows above it (offset < 0) have no record, like blank or decoration rows. Rows below it
// are its wrapped rows, so it stops at the line's last row.
func anchorRow(lines []diffLine, rec oscRecord, offset int) (int, bool) {
row := findOSCRecord(lines, rec)
if row < 0 {
return 0, false
}
if offset < 0 {
return max(row+offset, 0), true
}
for range offset {
if row+1 == len(lines) || !lines[row+1].showsOSCRecord(rec) {
break
}
row++
}
return row, true
}

func (m Model) contentWidth() int {
return m.Width - scrollbarWidth
}
Expand All @@ -281,7 +457,7 @@ func (m *Model) diff() tea.Cmd {
key := cacheKey(m.file.path, sideBySide)
if cached, ok := m.cache[key]; ok && len(cached.diff) != 0 {
m.file = cached
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return nil
}
node := &cachedNode{
Expand All @@ -297,7 +473,7 @@ func (m *Model) diff() tea.Cmd {
key := cacheKey(m.dir.path, m.sideBySide)
if cached, ok := m.cache[key]; ok && len(cached.diff) != 0 {
m.dir = cached
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return nil
}
node := &cachedNode{
Expand Down Expand Up @@ -363,14 +539,16 @@ func (m Model) dirHeaderView() string {

func (m Model) SetFilePatch(file *gitdiff.File) (Model, tea.Cmd) {
m.dir = nil
// The rows on screen are another node's until this one is shown.
m.anchor, m.shown = nil, nil

fname := filenode.GetFileName(file)
sideBySide := m.sideBySide && !file.IsNew && !file.IsDelete
key := cacheKey(fname, sideBySide)
if cached, ok := m.cache[key]; ok {
m.file = cached
m.updateHeader()
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return m, nil
}

Expand All @@ -391,12 +569,13 @@ func (m Model) SetFilePatch(file *gitdiff.File) (Model, tea.Cmd) {

func (m Model) SetDirPatch(dirPath string, files []*gitdiff.File) (Model, tea.Cmd) {
m.file = nil
m.anchor, m.shown = nil, nil

key := cacheKey(dirPath, m.sideBySide)
if cached, ok := m.cache[key]; ok {
m.dir = cached
m.updateHeader()
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return m, nil
}

Expand Down Expand Up @@ -428,17 +607,22 @@ func (m *Model) updateHeader() {
// SetSideBySide updates the diff view mode and re-renders.
func (m *Model) SetSideBySide(sideBySide bool) tea.Cmd {
m.sideBySide = sideBySide
m.saveAnchor()
return m.diff()
}

// ScrollUp scrolls the viewport up by the given number of lines.
func (m *Model) ScrollUp(lines int) {
row := m.currentRow()
m.fvp.ScrollUp(lines)
m.reanchor(row, edgeNone)
}

// ScrollDown scrolls the viewport down by the given number of lines.
func (m *Model) ScrollDown(lines int) {
row := m.currentRow()
m.fvp.ScrollDown(lines)
m.reanchor(row, edgeNone)
}

// ScrollBottom scrolls the viewport to the bottom.
Expand Down Expand Up @@ -488,7 +672,7 @@ func (m *Model) diffFile(node *cachedNode, width int) tea.Cmd {
return common.ErrMsg{Err: err}
}

return diffContentMsg{cacheKey: key, node: node, lines: stringToDiffLines(string(out))}
return diffContentMsg{cacheKey: key, node: node, lines: parseDeltaOutput(string(out))}
}
}

Expand Down Expand Up @@ -522,11 +706,11 @@ func (m *Model) diffDir(dir *cachedNode, width int, sideBySide bool, preamble st
return common.ErrMsg{Err: err}
}

text := string(out)
lines := parseDeltaOutput(string(out))
if preamble != "" {
text = m.renderPreamble(preamble) + "\n" + text
lines = append(stringToDiffLines(m.renderPreamble(preamble)), lines...)
}
return diffContentMsg{cacheKey: key, node: dir, lines: stringToDiffLines(text)}
return diffContentMsg{cacheKey: key, node: dir, lines: lines}
}
}

Expand Down Expand Up @@ -623,10 +807,11 @@ type deltaOpts struct {
UseUserDeltaTheme bool
}

// makeDeltaEnv sets side-by-side like `git -c delta.side-by-side=...` would.
// The --side-by-side flag would override the user's line-number format and can't turn it off.
// makeDeltaEnv asks for OSC 1717 line metadata and, with the user's theme, sets side-by-side
// like `git -c delta.side-by-side=...` would. The --side-by-side flag would override the
// user's line-number format and can't turn it off.
func makeDeltaEnv(opts deltaOpts) []string {
env := os.Environ()
env := append(os.Environ(), "OSC1717=V1")
if !opts.UseUserDeltaTheme {
return env
}
Expand Down
Loading
Loading