kitty/tools/tui/graphics/command_test.go
Kovid Goyal 535c7502e8
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
Graphics protocol: Add indexed color image formats
Add two new image formats, f=8 with one byte per pixel that is an index
into the terminal color palette and f=16 with a palette index followed by
an alpha byte. Indices 0-247 are the SGR 256 color table entries and
248-255 are special colors: default background, default foreground,
selection background and foreground, cursor, cursor text, URL color and
one reserved slot. Such images change color automatically when the
terminal colors change.

The images are uploaded as integer textures and drawn by a separate
shader program that looks up colors in the existing per window color
table uniform block, so a color scheme change costs nothing extra and
rendering of normal images is unaffected. Programs are switched only at
boundaries between runs of indexed and normal images in draw order. The
shader does bilinear filtering after the palette lookup since integer
textures cannot be filtered. Animation and frame composition are not
supported for these formats.

Closes #10666
2026-10-15 22:05:23 +05:30

115 lines
3.1 KiB
Go

// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package graphics
import (
"bytes"
"compress/zlib"
"fmt"
"io"
"strings"
"testing"
"github.com/emmansun/base64"
"github.com/google/go-cmp/cmp"
"golang.org/x/exp/rand"
)
var _ = fmt.Print
func from_full_apc_escape_code(raw string) *GraphicsCommand {
return GraphicsCommandFromAPC([]byte(raw[2 : len(raw)-2]))
}
func TestGraphicsCommandSerialization(t *testing.T) {
gc := &GraphicsCommand{}
test_serialize := func(payload string, vals ...string) {
expected := "\033_G" + strings.Join(vals, ",")
if payload != "" {
expected += ";" + base64.RawStdEncoding.EncodeToString([]byte(payload))
}
expected += "\033\\"
if diff := cmp.Diff(expected, gc.AsAPC([]byte(payload))); diff != "" {
t.Fatalf("Failed to write vals: %#v with payload: %#v\n%s", vals, payload, diff)
}
}
test_chunked_payload := func(payload []byte) {
c := &GraphicsCommand{}
data := c.AsAPC([]byte(payload))
encoded := strings.Builder{}
compressed := false
is_first := true
for {
idx := strings.Index(data, "\033_")
if idx < 0 {
break
}
l := strings.Index(data, "\033\\")
apc := data[idx+2 : l]
data = data[l+2:]
g := GraphicsCommandFromAPC([]byte(apc))
if is_first {
compressed = g.Compression() != 0
is_first = false
}
encoded.WriteString(g.ResponseMessage())
if g.m == GRT_more_nomore {
break
}
}
if len(data) > 0 {
t.Fatalf("Unparsed remnant: %#v", string(data))
}
decoded, err := base64.RawStdEncoding.DecodeString(encoded.String())
if err != nil {
t.Fatalf("Encoded data not valid base-64 with error: %v", err)
}
if compressed {
b := bytes.Buffer{}
b.Write(decoded)
r, _ := zlib.NewReader(&b)
o := bytes.Buffer{}
if _, err = io.Copy(&o, r); err != nil {
t.Fatal(err)
}
r.Close()
decoded = o.Bytes()
}
if diff := cmp.Diff(payload, decoded); diff != "" {
t.Fatalf("Decoded payload does not match original\nlen decoded=%d len payload=%d", len(decoded), len(payload))
}
}
test_serialize("")
gc.SetTransmission(GRT_transmission_sharedmem).SetAction(GRT_action_query).SetZIndex(-3).SetWidth(33).SetImageId(11)
test_serialize("abcd", "a=q", "t=s", "w=33", "i=11", "z=-3")
q := from_full_apc_escape_code(gc.AsAPC([]byte("abcd")))
if diff := cmp.Diff(gc.AsAPC(nil), q.AsAPC(nil)); diff != "" {
t.Fatalf("Parsing failed:\n%s", diff)
}
if diff := cmp.Diff(q.response_message, base64.RawStdEncoding.EncodeToString([]byte("abcd"))); diff != "" {
t.Fatalf("Failed to parse payload:\n%s", diff)
}
for _, x := range []struct {
f GRT_f
s string
}{{GRT_format_indexed, "f=8"}, {GRT_format_indexed_alpha, "f=16"}} {
gc = &GraphicsCommand{}
gc.SetFormat(x.f).SetDataWidth(2).SetDataHeight(1)
test_serialize("ab", x.s, "s=2", "v=1")
if q = from_full_apc_escape_code(gc.AsAPC(nil)); q.Format() != x.f {
t.Fatalf("Failed to parse format %s, got: %s", x.s, q.Format())
}
}
test_chunked_payload([]byte("abcd"))
data := make([]byte, 8111)
_, _ = rand.Read(data)
test_chunked_payload(data)
test_chunked_payload([]byte(strings.Repeat("a", 8007)))
}