v0.0.61 c09a5c5

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	SeqClearLine         = "\x1b[2K"
90	SeqBracketedPasteOn  = "\x1b[?2004h"
91	SeqBracketedPasteOff = "\x1b[?2004l"
92	SeqAltScreenOn       = "\x1b[?1049h"
93	SeqAltScreenOff      = "\x1b[?1049l"
94	SeqSynchronizedOn    = "\x1b[?2026h"
95	SeqSynchronizedOff   = "\x1b[?2026l"
96)
97
98// MoveTo moves the cursor to 1-indexed (row, col).
99func MoveTo(row, col int) string {
100	return "\x1b[" + itoa(row) + ";" + itoa(col) + "H"
101}

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 476

↑↓ moveenter openesc close