v0.11.3 6c26357

render_test.go RawHistoryFolder

1package tui
2
3import (
4	"bytes"
5	"strings"
6	"testing"
7)
8
9// TestDrawLogIdleNoOpEmitsNothing pins the cursor-blink fix: when
10// DrawLog is called with the exact same buffer and cursor position
11// as the previous call, it must emit ZERO bytes.
12//
13// The bug this regresses: at the 120ms animation tick the renderer
14// used to always emit SeqHideCursor + cursor-position +
15// SeqShowCursor, which resets the terminal's blink timer. Faster
16// than the OS blink interval, so an idle dialog editor (e.g. a
17// re-opened subagent transcript whose agent isn't producing output)
18// rendered the caret as a solid non-blinking block.
19//
20// With the no-op fast path the renderer leaves the screen alone
21// on idle frames, letting the terminal run its own blink cycle.
22func TestDrawLogIdleNoOpEmitsNothing(t *testing.T) {
23	var buf bytes.Buffer
24	r := NewRenderer(&buf)
25	r.Resize(80, 24)
26
27	chat := []string{"hello", "world"}
28	bottom := []string{"▌ "}
29	// First draw populates the renderer's cached buffer.
30	r.DrawLog(chat, bottom, 0, 2)
31	first := buf.Len()
32	if first == 0 {
33		t.Fatal("first DrawLog wrote nothing; setup is broken")
34	}
35	buf.Reset()
36
37	// Identical second draw: same content, same cursor placement.
38	r.DrawLog(chat, bottom, 0, 2)
39	if buf.Len() != 0 {
40		t.Fatalf("idle re-draw emitted %d bytes; expected 0 so terminal blink keeps ticking\n%q",
41			buf.Len(), buf.String())
42	}
43}
44
45// TestDrawLogContentChangeBreaksFastPath proves the no-op fast path
46// only fires when nothing changed. A buffer mutation must still
47// produce output, otherwise streaming agent replies would freeze on
48// screen.
49func TestDrawLogContentChangeBreaksFastPath(t *testing.T) {
50	var buf bytes.Buffer
51	r := NewRenderer(&buf)
52	r.Resize(80, 24)
53
54	r.DrawLog([]string{"hello"}, []string{"▌ "}, 0, 2)
55	buf.Reset()
56
57	// New chat row lands.
58	r.DrawLog([]string{"hello", "world"}, []string{"▌ "}, 0, 2)
59	if buf.Len() == 0 {
60		t.Fatal("content change suppressed by fast path; streaming output would freeze")
61	}
62}
63
64// TestDrawLogCursorMoveBreaksFastPath proves a cursor-only change
65// (no buffer change) still produces output. Without this, typing in
66// the editor would visually move the caret but the terminal would
67// keep drawing it at the old column.
68func TestDrawLogCursorMoveBreaksFastPath(t *testing.T) {
69	var buf bytes.Buffer
70	r := NewRenderer(&buf)
71	r.Resize(80, 24)
72
73	r.DrawLog([]string{"hi"}, []string{"▌ "}, 0, 2)
74	buf.Reset()
75
76	// Same buffer, different cursor column.
77	r.DrawLog([]string{"hi"}, []string{"▌ "}, 0, 3)
78	if buf.Len() == 0 {
79		t.Fatal("cursor-only change suppressed by fast path; caret would lag behind typing")
80	}
81	// And the emitted bytes must at least reposition the cursor.
82	if !strings.Contains(buf.String(), "\x1b[") {
83		t.Errorf("cursor move emission missing CSI escapes: %q", buf.String())
84	}
85}
86
87// TestDrawLogResizeForcesFullRedraw confirms a resize invalidates
88// the cache so the next DrawLog with identical inputs still emits.
89// Resize sets logInit=false; without that, a resize followed by an
90// identical buffer would falsely no-op and leave a stale frame.
91func TestDrawLogResizeForcesFullRedraw(t *testing.T) {
92	var buf bytes.Buffer
93	r := NewRenderer(&buf)
94	r.Resize(80, 24)
95	r.DrawLog([]string{"hi"}, []string{"▌ "}, 0, 2)
96	buf.Reset()
97
98	r.Resize(100, 30)
99	r.DrawLog([]string{"hi"}, []string{"▌ "}, 0, 2)
100	if buf.Len() == 0 {
101		t.Fatal("post-resize redraw skipped; the new frame would never reach the terminal")
102	}
103}
104
105// TestDrawLogInaccessibleChangePreservesScrollbackSelection covers long
106// streaming output whose changing first row has already scrolled above the
107// viewport. That row is immutable terminal history: clearing or replaying it
108// either destroys a native mouse selection or duplicates stale tool frames.
109func TestDrawLogInaccessibleChangePreservesScrollbackSelection(t *testing.T) {
110	t.Setenv("TERM_PROGRAM", "")
111	var buf bytes.Buffer
112	r := NewRenderer(&buf)
113	r.Resize(80, 3)
114	r.DrawLog([]string{"selected partial", "line 2", "line 3", "line 4"}, []string{"input"}, 0, 0)
115	buf.Reset()
116
117	// Only the historical row changed. DrawLog must leave the terminal alone
118	// instead of clearing and replaying the retained scrollback.
119	r.DrawLog([]string{"selected partial response", "line 2", "line 3", "line 4"}, []string{"input"}, 0, 0)
120	got := buf.String()
121	if strings.Contains(got, SeqClearScreenNoHome) || strings.Contains(got, SeqClearScrollback) {
122		t.Fatalf("inaccessible change cleared selected scrollback: %q", got)
123	}
124	if strings.Contains(got, "selected partial response") {
125		t.Fatalf("inaccessible row was replayed into retained scrollback: %q", got)
126	}
127}
128
129func TestDrawLogStructuralReflowResetsVisibleViewport(t *testing.T) {
130	t.Setenv("TERM_PROGRAM", "ghostty")
131	var buf bytes.Buffer
132	r := NewRenderer(&buf)
133	r.Resize(80, 3)
134	r.DrawLog([]string{"old 0", "old 1"}, []string{"working 0s"}, -1, 0)
135	buf.Reset()
136
137	// Chat and the bottom band both grow while an old row is above the
138	// viewport. Rebasing by chat growth alone leaves both tracked terminal
139	// coordinates one row behind, so future status updates paint off-screen.
140	chat := []string{"new row", "old 0", "old 1"}
141	r.DrawLog(chat, []string{"working 0s", "editor"}, 1, 3)
142	got := buf.String()
143	if !strings.Contains(got, SeqCursorHome+SeqClearToEnd) {
144		t.Fatalf("structural reflow did not reset the visible viewport: %q", got)
145	}
146	if strings.Contains(got, SeqClearScreenNoHome) || strings.Contains(got, SeqClearScrollback) {
147		t.Fatalf("structural reflow cleared retained scrollback: %q", got)
148	}
149	if strings.Contains(got, "old 1") {
150		t.Fatalf("viewport repaint included a row above the visible tail: %q", got)
151	}
152	for _, visible := range []string{"working 0s", "editor", SeqShowCursor, "\x1b[3C"} {
153		if !strings.Contains(got, visible) {
154			t.Fatalf("viewport repaint missing %q: %q", visible, got)
155		}
156	}
157	if want := len(r.logLines) - r.rows; r.logViewportTop != want {
158		t.Fatalf("viewport top = %d, want %d", r.logViewportTop, want)
159	}
160	if want := len(chat) + 1; r.logHardwareRow != want {
161		t.Fatalf("hardware row = %d, want cursor row %d", r.logHardwareRow, want)
162	}
163
164	buf.Reset()
165	r.DrawLog(chat, []string{"working 1s", "editor"}, 1, 3)
166	if got := buf.String(); !strings.Contains(got, "working 1s") {
167		t.Fatalf("status update after viewport reset was lost: %q", got)
168	}
169
170	buf.Reset()
171	r.DrawLog(append(chat, "next tool"), []string{"working 2s", "editor"}, 1, 3)
172	if got := buf.String(); !strings.Contains(got, "next tool") {
173		t.Fatalf("append after viewport reset was lost: %q", got)
174	}
175}
176
177func TestDrawLogStructuralReflowRepaintsVisibleImageFootprint(t *testing.T) {
178	t.Setenv("TERM_PROGRAM", "ghostty")
179	var buf bytes.Buffer
180	r := NewRenderer(&buf)
181	r.Resize(80, 4)
182	image := "\x1b_Ga=T;payload\x1b\\"
183	boxedBlank := "  │      │  "
184	r.DrawLog([]string{image, boxedBlank, "caption"}, []string{"working 0s"}, -1, 0)
185	buf.Reset()
186
187	// The visible tail begins deep inside an image footprint spanning more
188	// than one screen. Real footprint rows may retain their sentinel or be
189	// wrapped in empty tool-box edges. A viewport reset deletes Kitty images,
190	// so it must scan across both forms and replay the escape row above them.
191	r.DrawLog([]string{
192		"new row", image, boxedBlank, imageFootprintSentinel, boxedBlank,
193		boxedBlank, boxedBlank, "caption",
194	}, []string{"working 1s"}, -1, 0)
195	got := buf.String()
196	if !strings.Contains(got, SeqDeleteKittyImages) {
197		t.Fatalf("viewport reset did not clear stale Kitty images: %q", got)
198	}
199	if !strings.Contains(got, image) {
200		t.Fatalf("viewport reset did not repaint the visible image: %q", got)
201	}
202}
203
204func TestDrawLogInaccessibleChangeStillAppendsNewOutput(t *testing.T) {
205	t.Setenv("TERM_PROGRAM", "")
206	var buf bytes.Buffer
207	r := NewRenderer(&buf)
208	r.Resize(80, 3)
209	r.DrawLog([]string{"selected partial", "line 2", "line 3", "line 4"}, []string{"input"}, 0, 0)
210	buf.Reset()
211
212	// A streaming reflow changes inaccessible history while a tool result is
213	// appended. Only the new suffix should be emitted, naturally scrolling the
214	// selected old text upward without replaying the complete frame.
215	r.DrawLog([]string{"selected partial response", "line 2", "line 3", "line 4", "tool output"}, []string{"input"}, 0, 0)
216	got := buf.String()
217	if strings.Contains(got, SeqClearScreenNoHome) || strings.Contains(got, SeqClearScrollback) {
218		t.Fatalf("append after inaccessible change cleared selected scrollback: %q", got)
219	}
220	if strings.Contains(got, "selected partial response") || strings.Contains(got, "line 2") {
221		t.Fatalf("append replayed historical rows: %q", got)
222	}
223	if !strings.Contains(got, "tool output") {
224		t.Fatalf("new tool output was not appended: %q", got)
225	}
226}
227
228// TestDrawLogInvalidationPreservesScrollbackSelection pins the same rule for
229// cache invalidations, which can happen during an active turn independently
230// of an inaccessible changed row.
231func TestDrawLogInvalidationPreservesScrollbackSelection(t *testing.T) {
232	t.Setenv("TERM_PROGRAM", "")
233	var buf bytes.Buffer
234	r := NewRenderer(&buf)
235	r.Resize(80, 3)
236	r.DrawLog([]string{"one", "two", "three", "four"}, []string{"input"}, 0, 0)
237	buf.Reset()
238
239	r.Invalidate()
240	r.DrawLog([]string{"one", "two", "three", "four"}, []string{"input"}, 0, 0)
241	if got := buf.String(); strings.Contains(got, SeqClearScrollback) {
242		t.Fatalf("invalidation repaint erased scrollback and native selection: %q", got)
243	}
244}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close