v0.2.11 498b769

terminal.go RawHistoryFolder

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	// SeqCursorHome moves to the top-left of the visible viewport.
99	// SeqClearToEnd erases from the cursor to the end of the screen
100	// without scrolling content into scrollback (unlike \x1b[2J on
101	// xterm.js). Together they clear the visible frame in place, which
102	// the VS Code terminal needs for duplicate-free full repaints.
103	SeqCursorHome        = "\x1b[H"
104	SeqClearToEnd        = "\x1b[0J"
105	SeqClearLine         = "\x1b[2K"
106	SeqResetScrollRegion = "\x1b[r"
107	SeqDeleteKittyImages = "\x1b_Ga=d\x1b\\"
108	SeqBracketedPasteOn  = "\x1b[?2004h"
109	SeqBracketedPasteOff = "\x1b[?2004l"
110	// Basic mouse tracking + SGR extended coordinates. Used only
111	// when explicitly enabled by the interactive mode (currently VS
112	// Code terminal) so terminals with good native scrolling, like
113	// Ghostty, are left alone.
114	SeqMouseOn         = "\x1b[?1000h\x1b[?1006h"
115	SeqMouseOff        = "\x1b[?1000l\x1b[?1006l"
116	SeqAltScreenOn     = "\x1b[?1049h"
117	SeqAltScreenOff    = "\x1b[?1049l"
118	SeqSynchronizedOn  = "\x1b[?2026h"
119	SeqSynchronizedOff = "\x1b[?2026l"
120	// SeqSaveCursor / SeqRestoreCursor use DECSC/DECRC. All terminals
121	// we target adjust the saved row when natural scrolling occurs, so
122	// these survive scroll-on-write inside the bottom band.
123	SeqSaveCursor    = "\x1b7"
124	SeqRestoreCursor = "\x1b8"
125	// SeqEraseToEnd erases from the cursor to the end of the screen.
126	SeqEraseToEnd = "\x1b[J"
127)
128
129// SetScrollRegion returns the DECSTBM sequence to set the terminal's
130// scroll region (1-indexed, inclusive). Lines that scroll out of this
131// region still flow into the terminal's scrollback, but content above
132// or below the region is not affected by scroll-induced movement.
133func SetScrollRegion(top, bottom int) string {
134	return "\x1b[" + itoa(top) + ";" + itoa(bottom) + "r"
135}
136
137// MoveTo moves the cursor to 1-indexed (row, col).
138func MoveTo(row, col int) string {
139	return "\x1b[" + itoa(row) + ";" + itoa(col) + "H"
140}

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 477

↑↓ moveenter openesc close