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 SeqClearScrollback = "\x1b[3J"
90 SeqClearLine = "\x1b[2K"
91 SeqResetScrollRegion = "\x1b[r"
92 SeqDeleteKittyImages = "\x1b_Ga=d\x1b\\"
93 SeqBracketedPasteOn = "\x1b[?2004h"
94 SeqBracketedPasteOff = "\x1b[?2004l"
95 // Basic mouse tracking + SGR extended coordinates. Used only
96 // when explicitly enabled by the interactive mode (currently VS
97 // Code terminal) so terminals with good native scrolling, like
98 // Ghostty, are left alone.
99 SeqMouseOn = "\x1b[?1000h\x1b[?1006h"
100 SeqMouseOff = "\x1b[?1000l\x1b[?1006l"
101 SeqAltScreenOn = "\x1b[?1049h"
102 SeqAltScreenOff = "\x1b[?1049l"
103 SeqSynchronizedOn = "\x1b[?2026h"
104 SeqSynchronizedOff = "\x1b[?2026l"
105 // SeqSaveCursor / SeqRestoreCursor use DECSC/DECRC. All terminals
106 // we target adjust the saved row when natural scrolling occurs, so
107 // these survive scroll-on-write inside the bottom band.
108 SeqSaveCursor = "\x1b7"
109 SeqRestoreCursor = "\x1b8"
110 // SeqEraseToEnd erases from the cursor to the end of the screen.
111 SeqEraseToEnd = "\x1b[J"
112)
113
114// MoveTo moves the cursor to 1-indexed (row, col).
115func MoveTo(row, col int) string {
116 return "\x1b[" + itoa(row) + ";" + itoa(col) + "H"
117}