v0.0.15 3ff6d9e

render.go RawHistoryFolder

1package tui
2
3import (
4	"io"
5	"strings"
6
7	"github.com/mattn/go-runewidth"
8)
9
10// runewidthRune reports the number of cells a rune occupies, pinned
11// here so the renderer does not depend on the editor's helper.
12func runewidthRune(r rune) int { return runewidth.RuneWidth(r) }
13
14// Renderer maintains a previous frame and writes only the lines that
15// changed on each Draw(). Callers pass a full target frame (slice of
16// styled lines, already wrapped to width).
17type Renderer struct {
18	out  io.Writer
19	prev []string
20	rows int // terminal rows
21	cols int // terminal cols
22
23	// Cursor position after last draw (for placing input cursor).
24	cursorRow int
25	cursorCol int
26
27	// hideCursor when true prevents ShowCursor from being emitted.
28	hideCursor bool
29
30	// prevHadImage tracks whether the previous frame contained an
31	// inline-image escape so we can force a full clear+repaint whenever
32	// the image set changes. Only matters when inline images are
33	// enabled via ZOT_INLINE_IMAGES; defaults to false.
34	prevHadImage bool
35}
36
37// NewRenderer returns a renderer that writes to out.
38func NewRenderer(out io.Writer) *Renderer {
39	return &Renderer{out: out}
40}
41
42// Resize tells the renderer the current terminal size.
43func (r *Renderer) Resize(cols, rows int) {
44	if cols != r.cols || rows != r.rows {
45		r.cols = cols
46		r.rows = rows
47		r.prev = nil
48	}
49}
50
51// Clear forces a full repaint on the next Draw and clears the screen.
52func (r *Renderer) Clear() {
53	r.prev = nil
54	_, _ = io.WriteString(r.out, SeqClearScreen)
55}
56
57// Draw updates the terminal so that the visible frame ends with the
58// given lines (bottom-aligned). cursorRow/cursorCol are offsets within
59// the lines slice indicating where to place the terminal cursor; use
60// -1 to hide it.
61// containsImageEscape reports whether the line carries an inline-image
62// escape we must repaint rather than diff against the previous frame.
63func containsImageEscape(s string) bool {
64	return strings.Contains(s, "\x1b]1337;File=") || strings.Contains(s, "\x1b_G")
65}
66
67// truncateToWidth clips s so its on-screen width doesn't exceed cols
68// cells, preserving ANSI CSI escape sequences (which don't consume
69// cells). Lines carrying an inline-image escape are returned as-is
70// since we can't measure their painted size.
71//
72// Fast path: a byte-length <= cols is a conservative upper bound
73// guaranteeing the cell width is also <= cols, so we skip all the
74// rune-width math. That covers the vast majority of lines in a
75// transcript (narrow terminals wrap early; wide ones leave headroom).
76func truncateToWidth(s string, cols int) string {
77	if cols <= 0 || containsImageEscape(s) {
78		return s
79	}
80	if len(s) <= cols {
81		return s
82	}
83	var out strings.Builder
84	out.Grow(len(s))
85	seen := 0
86	runes := []rune(s)
87	for i := 0; i < len(runes); {
88		r := runes[i]
89		// CSI escape sequence (ESC [ ... final): zero-width.
90		if r == 0x1b && i+1 < len(runes) && runes[i+1] == '[' {
91			out.WriteRune(r)
92			out.WriteRune(runes[i+1])
93			i += 2
94			for i < len(runes) {
95				c := runes[i]
96				out.WriteRune(c)
97				i++
98				if c >= 0x40 && c <= 0x7e {
99					break
100				}
101			}
102			continue
103		}
104		rw := runewidthRune(r)
105		if seen+rw > cols {
106			break
107		}
108		out.WriteRune(r)
109		seen += rw
110		i++
111	}
112	return out.String()
113}
114
115func (r *Renderer) Draw(lines []string, cursorRow, cursorCol int) {
116	if r.cols == 0 || r.rows == 0 {
117		return
118	}
119	// Bottom-align: only the last r.rows lines are visible.
120	visible := lines
121	if len(visible) > r.rows {
122		visible = visible[len(visible)-r.rows:]
123		cursorRow -= len(lines) - len(visible)
124	}
125	// Pad to r.rows with empty lines at the top. Every line is also
126	// hard-truncated to cols so the terminal never soft-wraps our output
127	// (which would push the status bar out of its row).
128	frame := make([]string, r.rows)
129	top := r.rows - len(visible)
130	for i := 0; i < top; i++ {
131		frame[i] = ""
132	}
133	for i, line := range visible {
134		frame[top+i] = truncateToWidth(line, r.cols)
135	}
136
137	var w strings.Builder
138	w.WriteString(SeqSynchronizedOn)
139	w.WriteString(SeqHideCursor)
140
141	// When inline images are in play we always full-repaint (clear
142	// screen first, then rewrite every row). Terminals manage image
143	// pixels in a layer we cannot diff against, so the per-line cache
144	// is unreliable. Inline images are opt-in via ZOT_INLINE_IMAGES;
145	// the common code path below is the fast cached diff.
146	curHasImage := false
147	for _, l := range frame {
148		if containsImageEscape(l) {
149			curHasImage = true
150			break
151		}
152	}
153	forceAll := curHasImage || r.prevHadImage
154	if forceAll {
155		w.WriteString(SeqClearScreen)
156	}
157
158	full := r.prev == nil || len(r.prev) != r.rows
159	for i := 0; i < r.rows; i++ {
160		if full || forceAll || r.prev[i] != frame[i] {
161			w.WriteString(MoveTo(i+1, 1))
162			w.WriteString(SeqClearLine)
163			w.WriteString(frame[i])
164		}
165	}
166
167	if cursorRow >= 0 {
168		absRow := top + cursorRow + 1
169		absCol := cursorCol + 1
170		if absRow >= 1 && absRow <= r.rows {
171			w.WriteString(MoveTo(absRow, absCol))
172			w.WriteString(SeqShowCursor)
173		}
174	}
175	w.WriteString(SeqSynchronizedOff)
176
177	_, _ = io.WriteString(r.out, w.String())
178
179	r.prev = frame
180	r.prevHadImage = curHasImage
181	r.cursorRow = cursorRow
182	r.cursorCol = cursorCol
183}

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