mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
New color_scheme_dark, color_scheme_light and color_scheme_no_preference options. When any of them is set, the ssh kitten sends the color schemes for all three system color schemes to kitty, which applies the one matching the current system color scheme and switches between them when the system color scheme changes. color_scheme is used for modes without a specific color scheme. The colors are applied in a new session color layer, above the colors of the window and below colors set by programs. They are associated with the color stack entry pushed by the ssh kitten and are removed when it is popped, so nested sessions work and the colors are restored on exit. Configs that only use color_scheme continue to use escape codes exactly as before, as does the kitten when running inside tmux. Fixes #10724
209 lines
6.3 KiB
Go
209 lines
6.3 KiB
Go
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
|
|
|
package themes
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
var _ = fmt.Print
|
|
|
|
func TestThemeCollections(t *testing.T) {
|
|
for fname, expected := range map[string]string{
|
|
"moose": "Moose",
|
|
"mooseCat": "Moose Cat",
|
|
"a_bC": "A B C",
|
|
} {
|
|
actual := ThemeNameFromFileName(fname)
|
|
if diff := cmp.Diff(expected, actual); diff != "" {
|
|
t.Fatalf("Unexpected theme name for %s:\n%s", fname, diff)
|
|
}
|
|
}
|
|
|
|
tdir := t.TempDir()
|
|
|
|
pt := func(expected ThemeMetadata, lines ...string) {
|
|
if err := os.WriteFile(filepath.Join(tdir, "temp.conf"), []byte(strings.Join(lines, "\n")), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual, _, err := ParseThemeMetadata(filepath.Join(tdir, "temp.conf"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if diff := cmp.Diff(&expected, actual); diff != "" {
|
|
t.Fatalf("Failed to parse:\n%s\n\n%s", strings.Join(lines, "\n"), diff)
|
|
}
|
|
}
|
|
pt(ThemeMetadata{Name: "XYZ", Blurb: "a b", Author: "A", Is_dark: true, Num_settings: 2},
|
|
"# some crap", " ", "## ", "## author: A", "## name: XYZ", "## blurb: a", "## b", "", "color red", "background black", "include inc.conf")
|
|
if err := os.WriteFile(filepath.Join(tdir, "inc.conf"), []byte("background white"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pt(ThemeMetadata{Name: "XYZ", Blurb: "a b", Author: "A", Num_settings: 2},
|
|
"# some crap", " ", "## ", "## author: A", "## name: XYZ", "## blurb: a", "## b", "", "color red", "background black", "include inc.conf")
|
|
|
|
buf := bytes.Buffer{}
|
|
zw := zip.NewWriter(&buf)
|
|
fw, _ := zw.Create("x/themes.json")
|
|
if _, err := fw.Write([]byte(`[
|
|
{
|
|
"author": "X Y",
|
|
"blurb": "A dark color scheme for the kitty terminal.",
|
|
"file": "themes/Alabaster_Dark.conf",
|
|
"is_dark": true,
|
|
"license": "MIT",
|
|
"name": "Alabaster Dark",
|
|
"num_settings": 30,
|
|
"upstream": "https://xxx.com"
|
|
},
|
|
{
|
|
"name": "Empty", "file": "empty.conf"
|
|
}
|
|
]`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fw, _ = zw.Create("x/empty.conf")
|
|
if _, err := fw.Write([]byte("empty")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fw, _ = zw.Create("x/themes/Alabaster_Dark.conf")
|
|
if _, err := fw.Write([]byte("alabaster")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zw.Close()
|
|
|
|
received_etag := ""
|
|
request_count := 0
|
|
send_count := 0
|
|
check_etag := true
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
request_count++
|
|
received_etag = r.Header.Get("If-None-Match")
|
|
if check_etag && received_etag == `"xxx"` {
|
|
w.WriteHeader(http.StatusNotModified)
|
|
return
|
|
}
|
|
send_count++
|
|
w.Header().Add("ETag", `"xxx"`)
|
|
w.Write(buf.Bytes())
|
|
}))
|
|
defer ts.Close()
|
|
|
|
if _, err := fetch_cached("test", ts.URL, tdir, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := zip.OpenReader(filepath.Join(tdir, "test.zip"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var jm JSONMetadata
|
|
err = json.Unmarshal([]byte(r.Comment), &jm)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if jm.Etag != `"xxx"` {
|
|
t.Fatalf("Unexpected ETag: %#v", jm.Etag)
|
|
}
|
|
_, err = fetch_cached("test", ts.URL, tdir, time.Hour)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if request_count != 1 {
|
|
t.Fatalf("Cached zip file was not used: %d", request_count)
|
|
}
|
|
_, err = fetch_cached("test", ts.URL, tdir, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if request_count != 2 {
|
|
t.Fatalf("Cached zip file was incorrectly used: %d", request_count)
|
|
}
|
|
if received_etag != `"xxx"` {
|
|
t.Fatalf("Got invalid ETag: %#v", received_etag)
|
|
}
|
|
if send_count != 1 {
|
|
t.Fatalf("Cached zip file was incorrectly re-downloaded: %d", send_count)
|
|
}
|
|
check_etag = false
|
|
_, err = fetch_cached("test", ts.URL, tdir, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if send_count != 2 {
|
|
t.Fatalf("Cached zip file was incorrectly not re-downloaded. %d", send_count)
|
|
}
|
|
coll := Themes{name_map: map[string]*Theme{}}
|
|
closer, err := coll.add_from_zip_file(filepath.Join(tdir, "test.zip"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer closer.Close()
|
|
if code, err := coll.ThemeByName("Empty").load_code(); code != "empty" {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Fatal("failed to load code for empty theme")
|
|
}
|
|
if code, err := coll.ThemeByName("Alabaster Dark").load_code(); code != "alabaster" {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Fatal("failed to load code for alabaster theme")
|
|
}
|
|
}
|
|
|
|
func TestColorSettingsAsEscapeCodes(t *testing.T) {
|
|
ans := ColorSettingsAsEscapeCodes(map[string]string{"color1": "#ff0000", "color17": "#00ff00"})
|
|
for _, q := range []string{";1;#ff0000;", ";17;#00ff00;", ";16;#000000;", ";255;#eeeeee\x1b\\"} {
|
|
if !strings.Contains(ans, q) {
|
|
t.Fatalf("%#v not found in %#v", q, ans)
|
|
}
|
|
}
|
|
if strings.Contains(ans, ";16;#ffffff") {
|
|
t.Fatalf("Unset color 16 was set to white: %#v", ans)
|
|
}
|
|
_, osc21, found := strings.Cut(ans, "\x1b]21;")
|
|
if !found {
|
|
t.Fatalf("No OSC 21 in %#v", ans)
|
|
}
|
|
if !strings.HasPrefix(osc21, "16=;18=;") || !strings.HasSuffix(osc21, ";255=\x1b\\") || strings.Contains(osc21, ";17=") {
|
|
t.Fatalf("Incorrect OSC 21: %#v", osc21)
|
|
}
|
|
}
|
|
|
|
func TestColorSettingsAsSpec(t *testing.T) {
|
|
settings := map[string]string{"color1": "#ff0000", "color17": "#00ff00", "background": "#010203", "cursor": "", "foreground": "invalid"}
|
|
ans := ColorSettingsAsSpec(settings)
|
|
expected := map[string]any{"background": uint32(0x010203), "color1": uint32(0xff0000), "color17": uint32(0x00ff00), "color16": nil, "color255": nil}
|
|
for k, v := range expected {
|
|
if actual, found := ans[k]; !found || actual != v {
|
|
t.Fatalf("Incorrect value for %s: %#v != %#v", k, actual, v)
|
|
}
|
|
}
|
|
// colors reset to their configured value by the escape codes are not present
|
|
for _, k := range []string{"cursor", "foreground"} {
|
|
if _, found := ans[k]; found {
|
|
t.Fatalf("%s should not be present in %#v", k, ans)
|
|
}
|
|
}
|
|
// colors not specified by the theme get the same defaults as with escape codes
|
|
if ans["selection_background"] != uint32(0xfffacd) || ans["color0"] != uint32(0) || len(ans) != 256+3 {
|
|
t.Fatalf("Incorrect default colors in: %#v", ans)
|
|
}
|
|
if codes := ColorSettingsAsEscapeCodes(settings); !strings.Contains(codes, "\x1b]11;#010203\x1b\\") || !strings.Contains(codes, "\x1b]112\x1b\\") ||
|
|
!strings.Contains(codes, "\x1b]4;0;#000000;") {
|
|
t.Fatalf("Escape codes not consistent with spec: %#v", codes)
|
|
}
|
|
}
|