1package tui
2
3import (
4 "io"
5 "os"
6 "time"
7
8 "golang.org/x/term"
9)
10
11// Terminal abstracts the real terminal for tests.
12type Terminal interface {
13 io.Writer
14 // Size returns (cols, rows).
15 Size() (int, int)
16 // OnResize registers a callback invoked on SIGWINCH (best effort).
17 OnResize(func())
18 // EnterRaw puts the tty into raw mode. Returns a restore func.
19 EnterRaw() (restore func() error, err error)
20 // ReadByte reads one byte of input. Blocks.
21 ReadByte() (byte, error)
22 // PeekByteTimeout reads one byte of input but returns (0, false, nil)
23 // if no byte arrives within the timeout. Used to disambiguate bare
24 // Esc from the start of an escape sequence.
25 PeekByteTimeout(time.Duration) (byte, bool, error)
26 // SetNonblock sets stdin to non-blocking mode (used by paste handling).
27 // May be a no-op on some platforms.
28 SetNonblock(bool) error
29}
30
31// ProcTerm is a Terminal bound to the current process's tty.
32type ProcTerm struct {
33 out *os.File
34 in *os.File
35 resizeCBs []func()
36}
37
38// NewProcTerm returns a Terminal bound to stdin/stdout.
39func NewProcTerm() *ProcTerm {
40 return &ProcTerm{out: os.Stdout, in: os.Stdin}
41}
42
43func (p *ProcTerm) Write(b []byte) (int, error) { return p.out.Write(b) }
44
45func (p *ProcTerm) Size() (int, int) {
46 w, h, err := term.GetSize(int(p.out.Fd()))
47 if err != nil || w <= 0 || h <= 0 {
48 return 80, 24
49 }
50 return w, h
51}
52
53func (p *ProcTerm) OnResize(fn func()) {
54 p.resizeCBs = append(p.resizeCBs, fn)
55 p.installResizeHandler()
56}
57
58func (p *ProcTerm) EnterRaw() (func() error, error) {
59 fd := int(p.in.Fd())
60 if !term.IsTerminal(fd) {
61 return func() error { return nil }, nil
62 }
63 st, err := term.MakeRaw(fd)
64 if err != nil {
65 return nil, err
66 }
67 return func() error { return term.Restore(fd, st) }, nil
68}
69
70func (p *ProcTerm) ReadByte() (byte, error) {
71 var b [1]byte
72 _, err := p.in.Read(b[:])
73 return b[0], err
74}
75
76// PeekByteTimeout uses a platform-specific non-blocking read to decide
77// whether another byte is available within d. If not, returns (0, false, nil).
78func (p *ProcTerm) PeekByteTimeout(d time.Duration) (byte, bool, error) {
79 return peekStdin(p.in, d)
80}
81
82// ---- terminal control sequences ----
83
84// HideCursor, ShowCursor, ClearScreen, BracketedPasteOn/Off, etc.
85const (
86 SeqHideCursor = "\x1b[?25l"
87 SeqShowCursor = "\x1b[?25h"
88 SeqClearScreen = "\x1b[2J\x1b[H"
89 // SeqClearScreenNoHome is SeqClearScreen without the trailing
90 // cursor-home (\x1b[H). Use it whenever a MoveTo(...) follows
91 // immediately. VS Code's integrated terminal interprets a bare
92 // CUP-no-args during a clear as "snap the viewport scrollbar to
93 // row 0", which makes the user's scroll position jump on every
94 // repaint. The explicit MoveTo we emit afterwards positions the
95 // cursor identically without triggering that snap.
96 SeqClearScreenNoHome = "\x1b[2J"
97 SeqClearScrollback = "\x1b[3J"
98 SeqClearLine = "\x1b[2K"
99 SeqResetScrollRegion = "\x1b[r"
100 SeqDeleteKittyImages = "\x1b_Ga=d\x1b\\"
101 SeqBracketedPasteOn = "\x1b[?2004h"
102 SeqBracketedPasteOff = "\x1b[?2004l"
103 // Basic mouse tracking + SGR extended coordinates. Used only
104 // when explicitly enabled by the interactive mode (currently VS
105 // Code terminal) so terminals with good native scrolling, like
106 // Ghostty, are left alone.
107 SeqMouseOn = "\x1b[?1000h\x1b[?1006h"
108 SeqMouseOff = "\x1b[?1000l\x1b[?1006l"
109 SeqAltScreenOn = "\x1b[?1049h"
110 SeqAltScreenOff = "\x1b[?1049l"
111 SeqSynchronizedOn = "\x1b[?2026h"
112 SeqSynchronizedOff = "\x1b[?2026l"
113 // SeqSaveCursor / SeqRestoreCursor use DECSC/DECRC. All terminals
114 // we target adjust the saved row when natural scrolling occurs, so
115 // these survive scroll-on-write inside the bottom band.
116 SeqSaveCursor = "\x1b7"
117 SeqRestoreCursor = "\x1b8"
118 // SeqEraseToEnd erases from the cursor to the end of the screen.
119 SeqEraseToEnd = "\x1b[J"
120)
121
122// SetScrollRegion returns the DECSTBM sequence to set the terminal's
123// scroll region (1-indexed, inclusive). Lines that scroll out of this
124// region still flow into the terminal's scrollback, but content above
125// or below the region is not affected by scroll-induced movement.
126func SetScrollRegion(top, bottom int) string {
127 return "\x1b[" + itoa(top) + ";" + itoa(bottom) + "r"
128}
129
130// MoveTo moves the cursor to 1-indexed (row, col).
131func MoveTo(row, col int) string {
132 return "\x1b[" + itoa(row) + ";" + itoa(col) + "H"
133}