gitstack

grindlemire/go-tui code browser

8.3 KB Go 317 lines 2026-06-03 ยท d15bb9f 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
//go:build !windows

package tui

import (
	"os"
	"sync/atomic"
	"syscall"
	"time"
)

// stdinReader implements EventReader for a real terminal.
type stdinReader struct {
	fd         int     // stdin file descriptor
	buf        []byte  // Read buffer for escape sequences
	partialBuf []byte  // Buffer for incomplete UTF-8 sequences
	pending    []Event // Parsed events waiting to be returned

	// Interrupt mechanism for blocking mode
	interruptPipe [2]int // [0]=read, [1]=write
	hasInterrupt  bool   // Whether interrupt is enabled

	paused atomic.Bool // When true, PollEvent returns immediately
}

// Ensure stdinReader implements InterruptibleReader and PausableReader.
var (
	_ InterruptibleReader = (*stdinReader)(nil)
	_ PausableReader      = (*stdinReader)(nil)
)

// NewEventReader creates an EventReader for the given terminal input.
// The terminal should already be in raw mode.
func NewEventReader(in *os.File) (EventReader, error) {
	r := &stdinReader{
		fd:  int(in.Fd()),
		buf: make([]byte, 256),
	}
	return r, nil
}

// Pause causes PollEvent to return immediately without reading stdin.
// Interrupts any in-progress blocking read via the interrupt pipe.
// If EnableInterrupt has not been called, the in-progress PollEvent will
// unblock only on its next timeout cycle; Kitty negotiation may race
// with the stuck read until then.
func (r *stdinReader) Pause() {
	r.paused.Store(true)
	if r.hasInterrupt {
		r.Interrupt()
	}
}

// Resume allows PollEvent to read stdin again.
func (r *stdinReader) Resume() {
	r.paused.Store(false)
}

// PollEvent reads the next event with a timeout.
// Returns (event, true) if an event was read, or (nil, false) on timeout.
func (r *stdinReader) PollEvent(timeout time.Duration) (Event, bool) {
	if r.paused.Load() {
		return nil, false
	}

	// Return pending events first
	if len(r.pending) > 0 {
		ev := r.pending[0]
		r.pending = r.pending[1:]
		return ev, true
	}

	// Use select() with timeout for non-blocking stdin check
	// If interrupt is enabled, use the interruptible version
	var ready bool
	var err error
	if r.hasInterrupt {
		var interrupted bool
		ready, interrupted, err = selectWithTimeoutAndInterrupt(r.fd, r.interruptPipe[0], timeout)
		if interrupted {
			return nil, false // Interrupted, return immediately
		}
	} else {
		ready, err = selectWithTimeout(r.fd, timeout)
	}

	if err != nil || !ready {
		return nil, false
	}

	// Read available bytes
	n, err := syscall.Read(r.fd, r.buf)
	if err != nil || n == 0 {
		return nil, false
	}

	// Combine with any partial UTF-8 buffer from previous read
	data := r.buf[:n]
	if len(r.partialBuf) > 0 {
		data = append(r.partialBuf, data...)
		r.partialBuf = nil
	}

	// Parse into events
	events, remaining := parseInputWithRemainder(data)
	if len(remaining) > 0 {
		r.partialBuf = make([]byte, len(remaining))
		copy(r.partialBuf, remaining)
	}

	r.pending = events
	if len(r.pending) > 0 {
		ev := r.pending[0]
		r.pending = r.pending[1:]
		return ev, true
	}

	return nil, false
}

// Close releases resources.
func (r *stdinReader) Close() error {
	if r.hasInterrupt {
		syscall.Close(r.interruptPipe[0])
		syscall.Close(r.interruptPipe[1])
	}
	return nil
}

// EnableInterrupt sets up the interrupt mechanism using a self-pipe.
// This allows Interrupt() to wake up a blocking PollEvent call.
func (r *stdinReader) EnableInterrupt() error {
	if r.hasInterrupt {
		return nil // Already enabled
	}
	var fds [2]int
	if err := syscall.Pipe(fds[:]); err != nil {
		return err
	}
	r.interruptPipe = fds
	r.hasInterrupt = true
	return nil
}

// Interrupt wakes up a blocking PollEvent call by writing to the interrupt pipe.
func (r *stdinReader) Interrupt() error {
	if !r.hasInterrupt {
		return nil
	}
	_, err := syscall.Write(r.interruptPipe[1], []byte{0})
	return err
}

// parseInputWithRemainder parses input and returns any incomplete trailing bytes.
// This handles partial UTF-8 sequences and incomplete escape sequences at the end of the buffer.
func parseInputWithRemainder(data []byte) ([]Event, []byte) {
	// Check for trailing incomplete escape sequence first
	escRemaining := findIncompleteEscapeSequence(data)
	if len(escRemaining) > 0 {
		// Only buffer the escape sequence if there's other data before it.
		// If the entire buffer is just the incomplete sequence, don't buffer -
		// this handles the case where user actually pressed ESC key.
		if len(escRemaining) < len(data) {
			data = data[:len(data)-len(escRemaining)]
		} else {
			// Entire buffer is the incomplete sequence - don't buffer it
			escRemaining = nil
		}
	}

	// Check for trailing incomplete UTF-8 sequence
	// A UTF-8 leading byte (0xC0-0xFF) without enough continuation bytes
	remaining := findIncompleteUTF8Suffix(data)
	if len(remaining) > 0 {
		data = data[:len(data)-len(remaining)]
	}

	events := parseInput(data)

	// Combine remainders (escape sequence remainder takes precedence if both exist)
	if len(escRemaining) > 0 {
		return events, escRemaining
	}
	return events, remaining
}

// findIncompleteEscapeSequence finds any incomplete escape sequence at the end of data.
// Returns the incomplete bytes (if any).
// Handles:
// - Lone ESC at end (could be start of ESC[... or ESC O...)
// - ESC[ without terminating byte (CSI sequence in progress)
// - ESC[< without M/m terminator (SGR mouse sequence in progress)
func findIncompleteEscapeSequence(data []byte) []byte {
	if len(data) == 0 {
		return nil
	}

	// Scan backwards to find a potential incomplete escape sequence
	// Look for ESC (0x1b) in the last ~64 bytes (max reasonable escape sequence length)
	searchStart := max(len(data)-64, 0)

	for i := len(data) - 1; i >= searchStart; i-- {
		if data[i] != 0x1b {
			continue
		}

		// Found ESC at position i
		suffix := data[i:]

		// Lone ESC at end - definitely incomplete
		if len(suffix) == 1 {
			return suffix
		}

		// Check what follows ESC
		switch suffix[1] {
		case '[':
			// CSI sequence - check if it's complete
			// Complete CSI ends with byte in range 0x40-0x7e (@ through ~)
			if len(suffix) == 2 {
				// Just "ESC[" - incomplete
				return suffix
			}

			// Check for SGR mouse sequence: ESC[<...M or ESC[<...m
			if suffix[2] == '<' {
				// SGR mouse - look for terminating M or m
				for j := 3; j < len(suffix); j++ {
					if suffix[j] == 'M' || suffix[j] == 'm' {
						// Found terminator - sequence is complete
						// Continue scanning for another ESC
						break
					}
					// Check for invalid characters in mouse sequence
					// Valid: 0-9, ;, and final M/m
					if suffix[j] != ';' && (suffix[j] < '0' || suffix[j] > '9') {
						// Invalid character - not an SGR mouse sequence
						break
					}
					if j == len(suffix)-1 {
						// Reached end without finding M/m - incomplete
						return suffix
					}
				}
			} else {
				// Regular CSI - look for terminating byte (0x40-0x7e)
				for j := 2; j < len(suffix); j++ {
					if suffix[j] >= 0x40 && suffix[j] <= 0x7e {
						// Found terminator - sequence is complete
						break
					}
					if j == len(suffix)-1 {
						// Reached end without finding terminator - incomplete
						return suffix
					}
				}
			}

		case 'O':
			// SS3 sequence - needs one more byte
			if len(suffix) == 2 {
				return suffix
			}
			// SS3 sequences are always 3 bytes total, so complete

		default:
			// Could be Alt+key (ESC + printable) - these are complete at 2 bytes
			// Or unknown sequence - treat as complete to avoid infinite buffering
		}
	}

	return nil
}

// findIncompleteUTF8Suffix finds any incomplete UTF-8 sequence at the end of data.
// Returns the incomplete bytes (if any).
func findIncompleteUTF8Suffix(data []byte) []byte {
	if len(data) == 0 {
		return nil
	}

	// Check last 1-3 bytes for incomplete UTF-8 sequences
	for i := 1; i <= 3 && i <= len(data); i++ {
		b := data[len(data)-i]

		// If this is a UTF-8 leading byte, check if sequence is complete
		if b >= 0xC0 {
			// Determine expected sequence length
			var expectedLen int
			switch {
			case b < 0xE0:
				expectedLen = 2
			case b < 0xF0:
				expectedLen = 3
			default:
				expectedLen = 4
			}

			// If we don't have enough bytes for the full sequence, it's incomplete
			if i < expectedLen {
				return data[len(data)-i:]
			}
			// Sequence is complete
			return nil
		}

		// If this is a continuation byte (0x80-0xBF), keep looking for the lead byte
		if b >= 0x80 && b < 0xC0 {
			continue
		}

		// ASCII byte - no incomplete sequence
		return nil
	}

	return nil
}