v0.3.71 0dbbc58

image_test.go RawHistoryFolder

1package tui
2
3import (
4	"bytes"
5	"encoding/base64"
6	"image"
7	"image/color"
8	"image/jpeg"
9	"image/png"
10	"regexp"
11	"strings"
12	"testing"
13)
14
15func testPNG(t *testing.T, w, h int) []byte {
16	t.Helper()
17	img := image.NewRGBA(image.Rect(0, 0, w, h))
18	for y := 0; y < h; y++ {
19		for x := 0; x < w; x++ {
20			img.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255})
21		}
22	}
23	var buf bytes.Buffer
24	if err := png.Encode(&buf, img); err != nil {
25		t.Fatal(err)
26	}
27	return buf.Bytes()
28}
29
30func testJPEG(t *testing.T, w, h int) []byte {
31	t.Helper()
32	img := image.NewRGBA(image.Rect(0, 0, w, h))
33	for y := 0; y < h; y++ {
34		for x := 0; x < w; x++ {
35			img.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255})
36		}
37	}
38	var buf bytes.Buffer
39	if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 90}); err != nil {
40		t.Fatal(err)
41	}
42	return buf.Bytes()
43}
44
45var kittyPayloadRE = regexp.MustCompile(`\x1b_G[^;]*;([^\x1b]*)\x1b\\`)
46
47// kittyDecodePayload reassembles the base64 chunks from a kitty
48// graphics escape sequence and returns the decoded raw bytes.
49func kittyDecodePayload(t *testing.T, seq string) []byte {
50	t.Helper()
51	ms := kittyPayloadRE.FindAllStringSubmatch(seq, -1)
52	if len(ms) == 0 {
53		t.Fatalf("no kitty payload chunks found in sequence")
54	}
55	var b64 strings.Builder
56	for _, m := range ms {
57		b64.WriteString(m[1])
58	}
59	raw, err := base64.StdEncoding.DecodeString(b64.String())
60	if err != nil {
61		t.Fatalf("decode base64 payload: %v", err)
62	}
63	return raw
64}
65
66func TestRenderKittyReencodesJPEGToPNG(t *testing.T) {
67	// A JPEG handed to the kitty renderer must be re-encoded to PNG,
68	// because kitty's f=100 path only decodes PNG. Otherwise the
69	// terminal reserves the cell rectangle but paints nothing, which is
70	// the empty-box symptom.
71	jpg := testJPEG(t, 40, 30)
72	seq := renderKitty(jpg, 20, 10)
73	if seq == "" {
74		t.Fatal("renderKitty returned empty sequence")
75	}
76	raw := kittyDecodePayload(t, seq)
77	pngMagic := []byte{0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A}
78	if len(raw) < 8 || !bytes.Equal(raw[:8], pngMagic) {
79		t.Fatalf("kitty payload is not PNG; got prefix %x", raw[:min(8, len(raw))])
80	}
81}
82
83func TestRenderKittyLeavesPNGUntouched(t *testing.T) {
84	src := testPNG(t, 40, 30)
85	seq := renderKitty(src, 20, 10)
86	raw := kittyDecodePayload(t, seq)
87	if !bytes.Equal(raw, src) {
88		t.Fatal("PNG payload was needlessly re-encoded")
89	}
90}
91
92func TestRowsForInlineImageRoundsUp(t *testing.T) {
93	t.Setenv("ZOT_CELL_ASPECT", "")
94	data := testPNG(t, 100, 51)
95	got := RowsForInlineImage(data, 10, 0)
96	// 51px high at 10 cells wide with a 2.0 cell aspect is 2.55 rows.
97	// Rounding down to 2 lets following text overlap; we need 3.
98	if got != 3 {
99		t.Fatalf("RowsForInlineImage = %d, want 3", got)
100	}
101}
102
103func TestRowsForInlineImageCellAspectOverride(t *testing.T) {
104	data := testPNG(t, 100, 100)
105	t.Setenv("ZOT_CELL_ASPECT", "1")
106	if got := RowsForInlineImage(data, 10, 0); got != 10 {
107		t.Fatalf("aspect=1 rows = %d, want 10", got)
108	}
109	t.Setenv("ZOT_CELL_ASPECT", "4")
110	if got := RowsForInlineImage(data, 10, 0); got != 3 {
111		t.Fatalf("aspect=4 rows = %d, want 3", got)
112	}
113}
114
115func TestDetectImageProtocolPlaceholderAndIntermediaries(t *testing.T) {
116	t.Setenv("ZOT_INLINE_IMAGES", "placeholder")
117	if got := DetectImageProtocol(); got != ImageProtocolNone {
118		t.Fatalf("placeholder protocol = %v, want none", got)
119	}
120
121	t.Setenv("ZOT_INLINE_IMAGES", "")
122	t.Setenv("HERDR_ENV", "")
123	t.Setenv("TERM_PROGRAM", "vscode")
124	t.Setenv("KITTY_WINDOW_ID", "1")
125	if got := DetectImageProtocol(); got != ImageProtocolNone {
126		t.Fatalf("vscode auto protocol = %v, want none", got)
127	}
128
129	t.Setenv("TERM_PROGRAM", "ghostty")
130	t.Setenv("HERDR_ENV", "1")
131	if got := DetectImageProtocol(); got != ImageProtocolNone {
132		t.Fatalf("Herdr auto protocol = %v, want none", got)
133	}
134
135	t.Setenv("ZOT_INLINE_IMAGES", "kitty")
136	if got := DetectImageProtocol(); got != ImageProtocolKitty {
137		t.Fatalf("forced kitty protocol = %v, want kitty", got)
138	}
139}

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