gitstack

grindlemire/go-tui code browser

11.1 KB Go 417 lines 2026-05-30 ยท d89ae47 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package tui

import (
	"strconv"
	"unicode/utf8"
)

// escBuilder builds ANSI escape sequences.
// It uses a pre-allocated buffer to minimize allocations.
type escBuilder struct {
	buf []byte
}

// newEscBuilder creates a new escape sequence builder with the given initial capacity.
func newEscBuilder(capacity int) *escBuilder {
	return &escBuilder{
		buf: make([]byte, 0, capacity),
	}
}

// Reset clears the buffer for reuse.
func (e *escBuilder) Reset() {
	e.buf = e.buf[:0]
}

// Bytes returns the built escape sequence.
func (e *escBuilder) Bytes() []byte {
	return e.buf
}

// Len returns the current length of the buffer.
func (e *escBuilder) Len() int {
	return len(e.buf)
}

// writeCSI writes the Control Sequence Introducer (ESC [).
func (e *escBuilder) writeCSI() {
	e.buf = append(e.buf, '\x1b', '[')
}

// writeInt writes an integer to the buffer.
func (e *escBuilder) writeInt(n int) {
	e.buf = strconv.AppendInt(e.buf, int64(n), 10)
}

// MoveTo moves the cursor to the specified position.
// x and y are 0-indexed; ANSI sequences use 1-indexed positions.
func (e *escBuilder) MoveTo(x, y int) {
	e.writeCSI()
	e.writeInt(y + 1) // Convert to 1-indexed
	e.buf = append(e.buf, ';')
	e.writeInt(x + 1) // Convert to 1-indexed
	e.buf = append(e.buf, 'H')
}

// MoveUp moves the cursor up by n rows.
func (e *escBuilder) MoveUp(n int) {
	if n <= 0 {
		return
	}
	e.writeCSI()
	if n > 1 {
		e.writeInt(n)
	}
	e.buf = append(e.buf, 'A')
}

// MoveDown moves the cursor down by n rows.
func (e *escBuilder) MoveDown(n int) {
	if n <= 0 {
		return
	}
	e.writeCSI()
	if n > 1 {
		e.writeInt(n)
	}
	e.buf = append(e.buf, 'B')
}

// MoveRight moves the cursor right by n columns.
func (e *escBuilder) MoveRight(n int) {
	if n <= 0 {
		return
	}
	e.writeCSI()
	if n > 1 {
		e.writeInt(n)
	}
	e.buf = append(e.buf, 'C')
}

// MoveLeft moves the cursor left by n columns.
func (e *escBuilder) MoveLeft(n int) {
	if n <= 0 {
		return
	}
	e.writeCSI()
	if n > 1 {
		e.writeInt(n)
	}
	e.buf = append(e.buf, 'D')
}

// ClearScreen clears the entire screen.
func (e *escBuilder) ClearScreen() {
	e.writeCSI()
	e.buf = append(e.buf, '2', 'J')
}

// ClearScrollback clears the scrollback buffer (ESC[3J).
// This helps ensure a clean screen after terminal resize.
func (e *escBuilder) ClearScrollback() {
	e.writeCSI()
	e.buf = append(e.buf, '3', 'J')
}

// ClearToEndOfScreen clears from cursor to end of screen (ESC[J or ESC[0J).
func (e *escBuilder) ClearToEndOfScreen() {
	e.writeCSI()
	e.buf = append(e.buf, 'J')
}

// ClearLine clears the entire current line.
func (e *escBuilder) ClearLine() {
	e.writeCSI()
	e.buf = append(e.buf, '2', 'K')
}

// EraseToEndOfLine clears from the cursor to the end of the current line
// (ESC[K). Erased cells take the current background and are left in a blank,
// unwritten state, so terminals trim them when copying a selection.
func (e *escBuilder) EraseToEndOfLine() {
	e.writeCSI()
	e.buf = append(e.buf, 'K')
}

// HideCursor makes the cursor invisible.
func (e *escBuilder) HideCursor() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '2', '5', 'l')
}

// ShowCursor makes the cursor visible.
func (e *escBuilder) ShowCursor() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '2', '5', 'h')
}

// EnterAltScreen switches to the alternate screen buffer.
func (e *escBuilder) EnterAltScreen() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '4', '9', 'h')
}

// ExitAltScreen switches back to the main screen buffer.
func (e *escBuilder) ExitAltScreen() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '4', '9', 'l')
}

// BeginSyncUpdate starts a synchronized update block.
// The terminal will buffer all output until EndSyncUpdate is called,
// then display it atomically. This prevents tearing during updates.
// Supported by: iTerm2, Terminal.app, kitty, alacritty, foot, etc.
// Terminals that don't support it will simply ignore this sequence.
func (e *escBuilder) BeginSyncUpdate() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '2', '0', '2', '6', 'h')
}

// EndSyncUpdate ends a synchronized update block.
// The terminal will now display all buffered output atomically.
func (e *escBuilder) EndSyncUpdate() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '2', '0', '2', '6', 'l')
}

// EnableMouse enables mouse reporting using SGR-1006 extended mode.
// This enables button events (press/release) with SGR encoding for better
// coordinate support (works beyond column 223).
// Supported by most modern terminals: iTerm2, Terminal.app, kitty, alacritty, etc.
func (e *escBuilder) EnableMouse() {
	// Enable X10 mouse button tracking (basic click events)
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '0', '0', 'h')
	// Enable SGR extended mouse mode (better coordinate encoding)
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '0', '6', 'h')
}

// DisableMouse disables mouse reporting.
func (e *escBuilder) DisableMouse() {
	// Disable SGR extended mouse mode
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '0', '6', 'l')
	// Disable X10 mouse button tracking
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '0', '0', 'l')
}

// EnableAltScroll enables alternate-scroll mode (DEC private mode 1007).
// While on the alternate screen with mouse reporting disabled, the terminal
// translates mouse-wheel events into cursor up/down keys. This lets the wheel
// scroll the app while leaving native text selection and OSC 8 link clicking
// intact. Terminals gate this on mouse reporting being off, so it is a no-op
// when mouse mode is active.
func (e *escBuilder) EnableAltScroll() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '0', '7', 'h')
}

// DisableAltScroll disables alternate-scroll mode (DEC private mode 1007).
func (e *escBuilder) DisableAltScroll() {
	e.writeCSI()
	e.buf = append(e.buf, '?', '1', '0', '0', '7', 'l')
}

// KittyKeyboardPush pushes Kitty keyboard protocol mode onto the terminal's
// keyboard mode stack. flags=1 enables key disambiguation.
// Sequence: CSI > flags u
func (e *escBuilder) KittyKeyboardPush(flags int) {
	e.writeCSI()
	e.buf = append(e.buf, '>')
	e.writeInt(flags)
	e.buf = append(e.buf, 'u')
}

// KittyKeyboardPop pops the most recent Kitty keyboard protocol mode from
// the terminal's keyboard mode stack, restoring the previous mode.
// Sequence: CSI < u
func (e *escBuilder) KittyKeyboardPop() {
	e.writeCSI()
	e.buf = append(e.buf, '<', 'u')
}

// KittyKeyboardQuery queries the terminal for the current Kitty keyboard
// protocol mode. The terminal responds with CSI ? flags u.
// Sequence: CSI ? u
func (e *escBuilder) KittyKeyboardQuery() {
	e.writeCSI()
	e.buf = append(e.buf, '?', 'u')
}

// ResetStyle resets all text attributes to default.
func (e *escBuilder) ResetStyle() {
	e.writeCSI()
	e.buf = append(e.buf, '0', 'm')
}

// SetStyle sets the text style based on the given Style and terminal capabilities.
// It generates minimal escape sequences by only setting non-default attributes.
func (e *escBuilder) SetStyle(s Style, caps Capabilities) {
	// Always start with a reset to ensure clean state
	e.writeCSI()
	e.buf = append(e.buf, '0')

	// Add attributes
	if s.HasAttr(AttrBold) {
		e.buf = append(e.buf, ';', '1')
	}
	if s.HasAttr(AttrDim) {
		e.buf = append(e.buf, ';', '2')
	}
	if s.HasAttr(AttrItalic) {
		e.buf = append(e.buf, ';', '3')
	}
	if s.HasAttr(AttrUnderline) {
		e.buf = append(e.buf, ';', '4')
	}
	if s.HasAttr(AttrBlink) {
		e.buf = append(e.buf, ';', '5')
	}
	if s.HasAttr(AttrReverse) {
		e.buf = append(e.buf, ';', '7')
	}
	if s.HasAttr(AttrStrikethrough) {
		e.buf = append(e.buf, ';', '9')
	}

	// Add foreground color
	e.appendColor(s.Fg, true, caps)

	// Add background color
	e.appendColor(s.Bg, false, caps)

	e.buf = append(e.buf, 'm')
}

// appendColor appends the appropriate escape sequence for a color.
// fg indicates whether this is a foreground (true) or background (false) color.
func (e *escBuilder) appendColor(c Color, fg bool, caps Capabilities) {
	if c.IsDefault() {
		return
	}

	// Determine color code base: 38 for foreground, 48 for background
	var base int
	if fg {
		base = 38
	} else {
		base = 48
	}

	switch c.Type() {
	case ColorANSI:
		idx := c.ANSI()
		if idx < 16 && caps.Colors >= Color16 {
			// Use basic color codes for colors 0-15
			// Foreground: 30-37 (normal), 90-97 (bright)
			// Background: 40-47 (normal), 100-107 (bright)
			if idx < 8 {
				if fg {
					e.buf = append(e.buf, ';')
					e.writeInt(30 + int(idx))
				} else {
					e.buf = append(e.buf, ';')
					e.writeInt(40 + int(idx))
				}
			} else {
				if fg {
					e.buf = append(e.buf, ';')
					e.writeInt(90 + int(idx) - 8)
				} else {
					e.buf = append(e.buf, ';')
					e.writeInt(100 + int(idx) - 8)
				}
			}
		} else if caps.Colors >= Color256 {
			// Use 256-color mode: ESC[38;5;{n}m or ESC[48;5;{n}m
			e.buf = append(e.buf, ';')
			e.writeInt(base)
			e.buf = append(e.buf, ';', '5', ';')
			e.writeInt(int(idx))
		}

	case ColorRGB:
		if caps.TrueColor && caps.Colors >= ColorTrue {
			// Use true color: ESC[38;2;{r};{g};{b}m or ESC[48;2;{r};{g};{b}m
			r, g, b := c.RGB()
			e.buf = append(e.buf, ';')
			e.writeInt(base)
			e.buf = append(e.buf, ';', '2', ';')
			e.writeInt(int(r))
			e.buf = append(e.buf, ';')
			e.writeInt(int(g))
			e.buf = append(e.buf, ';')
			e.writeInt(int(b))
		} else if caps.Colors >= Color256 {
			// Fall back to ANSI 256-color approximation
			ansi := c.ToANSI()
			e.buf = append(e.buf, ';')
			e.writeInt(base)
			e.buf = append(e.buf, ';', '5', ';')
			e.writeInt(int(ansi.ANSI()))
		} else if caps.Colors >= Color16 {
			// Fall back to basic 16-color approximation using 256-color mode
			// (most terminals that claim only 16 colors still support 256-color sequences)
			ansi := c.ToANSI()
			e.buf = append(e.buf, ';')
			e.writeInt(base)
			e.buf = append(e.buf, ';', '5', ';')
			e.writeInt(int(ansi.ANSI()))
		}
	}
}

// WriteRune appends a UTF-8 encoded rune to the buffer.
func (e *escBuilder) WriteRune(r rune) {
	var buf [utf8.UTFMax]byte
	n := utf8.EncodeRune(buf[:], r)
	e.buf = append(e.buf, buf[:n]...)
}

// WriteString appends a string to the buffer.
func (e *escBuilder) WriteString(s string) {
	e.buf = append(e.buf, s...)
}

// WriteBytes appends bytes to the buffer.
func (e *escBuilder) WriteBytes(b []byte) {
	e.buf = append(e.buf, b...)
}

// OpenHyperlink writes an OSC 8 hyperlink-open sequence for the given URL.
// Control bytes (< 0x20 and 0x7f) are stripped so they cannot terminate or
// corrupt the sequence.
func (e *escBuilder) OpenHyperlink(url string) {
	e.buf = append(e.buf, 0x1b, ']', '8', ';', ';')
	for i := 0; i < len(url); i++ {
		if c := url[i]; c >= 0x20 && c != 0x7f {
			e.buf = append(e.buf, c)
		}
	}
	e.buf = append(e.buf, 0x1b, '\\')
}

// CloseHyperlink writes an OSC 8 hyperlink-close sequence (empty URL).
func (e *escBuilder) CloseHyperlink() {
	e.buf = append(e.buf, 0x1b, ']', '8', ';', ';', 0x1b, '\\')
}

// linkTransition moves the open OSC 8 hyperlink from open to next, emitting the
// close/open sequences as needed, and returns the new open link. Passing
// next == "" closes any open link. Used by both the diff-based and row-based
// renderers so they emit identical hyperlink sequences.
func linkTransition(e *escBuilder, open, next string) string {
	if next == open {
		return open
	}
	if open != "" {
		e.CloseHyperlink()
	}
	if next != "" {
		e.OpenHyperlink(next)
	}
	return next
}