mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-16 13:08:25 +00:00
set-colors: Clean up parsing of colors and conf files
Pass the CLI options to parse_colors_and_files instead of reading global state, and have it set all and configured when reset is specified, matching the Python client. Read conf files with the standard config parser so that tabs, indentation, long lines and include directives work, and report invalid colors instead of silently ignoring them. Non-color settings and special values of colors such as wayland_titlebar_color are still ignored, as in the Python client. Allow special_parse functions to take over fields that are backed by options in the generated Go remote control code.
This commit is contained in:
parent
a3da75bcef
commit
48674a7869
6 changed files with 110 additions and 40 deletions
|
|
@ -264,6 +264,11 @@ Detailed list of changes
|
|||
- Remote control: Fix ``kitten @ set-colors --reset`` failing on invalid
|
||||
color arguments or missing configuration files instead of ignoring them
|
||||
|
||||
- Remote control: ``kitten @ set-colors`` now reports invalid colors in
|
||||
configuration files instead of silently ignoring them, and parses them the
|
||||
same way as :file:`kitty.conf`, with support for tabs, indentation and
|
||||
``include`` directives
|
||||
|
||||
- ssh kitten: Fix hanging when re-using a shared connection whose connection
|
||||
to the server has silently died, for example, after a network change or
|
||||
suspend. Now the kitten detects this and reconnects (:iss:`10691`)
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ def main(args: list[str] = sys.argv) -> None:
|
|||
nullable_colors.append(opt.name)
|
||||
|
||||
patch_color_list('tools/cmd/at/set_colors.go', nullable_colors, 'NULLABLE')
|
||||
patch_color_list('tools/cmd/at/set_colors.go', special_colors, 'SPECIAL')
|
||||
patch_color_list('tools/themes/collection.go', all_colors, 'ALL')
|
||||
nc = ',\n '.join(f'{x!r}' for x in nullable_colors)
|
||||
sc = ',\n '.join(f'{x!r}' for x in special_colors)
|
||||
|
|
|
|||
|
|
@ -440,7 +440,11 @@ def go_code_for_remote_command(name: str, cmd: RemoteCommand, template: str) ->
|
|||
unhandled = {}
|
||||
used_options = set()
|
||||
for field in json_fields:
|
||||
if field.go_option_name in option_map:
|
||||
if field.field in handled_fields:
|
||||
# the special parser is responsible for setting this field and reading its option
|
||||
if field.go_option_name in option_map:
|
||||
used_options.add(field.go_option_name)
|
||||
elif field.go_option_name in option_map:
|
||||
o = option_map[field.go_option_name]
|
||||
used_options.add(field.go_option_name)
|
||||
optstring = f'options_{name}.{o.go_var_name}'
|
||||
|
|
@ -454,8 +458,6 @@ def go_code_for_remote_command(name: str, cmd: RemoteCommand, template: str) ->
|
|||
jc.append(f'payload.{field.struct_field_name} = escape_dict_of_strings({optstring})')
|
||||
else:
|
||||
jc.append(f'payload.{field.struct_field_name} = {optstring}')
|
||||
elif field.field in handled_fields:
|
||||
pass
|
||||
else:
|
||||
unhandled[field.field] = field
|
||||
for x in tuple(unhandled):
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ this option, any color arguments are ignored and :option:`kitten @ set-colors --
|
|||
args = RemoteCommand.Args(
|
||||
spec='COLOR_OR_FILE ...',
|
||||
json_field='colors',
|
||||
special_parse='parse_colors_and_files(args)',
|
||||
special_parse='+all,configured,reset:parse_colors_and_files(args, options_set_colors, &payload)',
|
||||
completion=RemoteCommand.CompletionSpec.from_string('type:file group:"CONF files", ext:conf'),
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@
|
|||
package at
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/config"
|
||||
"github.com/kovidgoyal/kitty/tools/themes"
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
"github.com/kovidgoyal/kitty/tools/utils/style"
|
||||
)
|
||||
|
|
@ -271,6 +271,17 @@ var nullable_colors = map[string]bool{
|
|||
// NULLABLE_COLORS_END
|
||||
}
|
||||
|
||||
// Colors that also accept special values such as background or system
|
||||
var special_colors = map[string]bool{
|
||||
// generated by gen-config.py do not edit
|
||||
// SPECIAL_COLORS_START
|
||||
"macos_titlebar_color": true,
|
||||
"scrollbar_handle_color": true,
|
||||
"scrollbar_track_color": true,
|
||||
"wayland_titlebar_color": true,
|
||||
// SPECIAL_COLORS_END
|
||||
}
|
||||
|
||||
func set_color_in_color_map(key, val string, ans map[string]any, check_nullable, skip_nullable bool) error {
|
||||
if val == "none" {
|
||||
if check_nullable && !nullable_colors[key] {
|
||||
|
|
@ -292,33 +303,43 @@ func set_color_in_color_map(key, val string, ans map[string]any, check_nullable,
|
|||
return nil
|
||||
}
|
||||
|
||||
func parse_colors_and_files(args []string) (map[string]any, error) {
|
||||
ans := make(map[string]any, len(args))
|
||||
if options_set_colors.Reset {
|
||||
return ans, nil
|
||||
func read_colors_from_conf_file(path string, ans map[string]any) error {
|
||||
var color_err error
|
||||
cp := config.ConfigParser{LineHandler: func(key, val string) error {
|
||||
key = strings.ToLower(key)
|
||||
if key != "transparent_background_colors" && !themes.AllColorSettingNames[key] {
|
||||
// conf files can contain non-color settings, ignore them
|
||||
return nil
|
||||
}
|
||||
if err := set_color_in_color_map(key, strings.ToLower(val), ans, true, true); err != nil && !special_colors[key] && color_err == nil {
|
||||
color_err = fmt.Errorf("Invalid value for %s in %s: %w", key, path, err)
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
if err := cp.ParseFiles(path); err != nil {
|
||||
return err
|
||||
}
|
||||
return color_err
|
||||
}
|
||||
|
||||
func parse_colors_and_files(args []string, opts options_set_colors_type, payload *set_colors_json_type) error {
|
||||
// reset implies all and configured and any color arguments are ignored
|
||||
payload.Reset = opts.Reset
|
||||
payload.All = opts.All || opts.Reset
|
||||
payload.Configured = opts.Configured || opts.Reset
|
||||
if opts.Reset {
|
||||
return nil
|
||||
}
|
||||
ans := make(map[string]any, len(args))
|
||||
for _, arg := range args {
|
||||
key, val, found := strings.Cut(strings.ToLower(arg), "=")
|
||||
if found {
|
||||
err := set_color_in_color_map(key, val, ans, true, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
path := utils.Expanduser(arg)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
key, val, found := strings.Cut(scanner.Text(), " ")
|
||||
if found {
|
||||
set_color_in_color_map(strings.ToLower(key), strings.ToLower(strings.TrimSpace(val)), ans, true, true)
|
||||
}
|
||||
if key, val, found := strings.Cut(strings.ToLower(arg), "="); found {
|
||||
if err := set_color_in_color_map(key, val, ans, true, false); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err := read_colors_from_conf_file(utils.Expanduser(arg), ans); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return ans, nil
|
||||
payload.Colors = ans
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,15 +4,13 @@ package at
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
func TestSetColorsResetIgnoresArguments(t *testing.T) {
|
||||
original_reset := options_set_colors.Reset
|
||||
t.Cleanup(func() { options_set_colors.Reset = original_reset })
|
||||
missing_file := filepath.Join(t.TempDir(), "missing.conf")
|
||||
for _, reset := range []bool{false, true} {
|
||||
for _, test := range []struct {
|
||||
|
|
@ -24,9 +22,8 @@ func TestSetColorsResetIgnoresArguments(t *testing.T) {
|
|||
{"valid-color", "foreground=red", false},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%s/reset=%t", test.name, reset), func(t *testing.T) {
|
||||
options_set_colors.Reset = reset
|
||||
io_data := rc_io_data{rc: &utils.RemoteControlCmd{}}
|
||||
err := create_payload_set_colors(&io_data, nil, []string{test.arg})
|
||||
payload := set_colors_json_type{}
|
||||
err := parse_colors_and_files([]string{test.arg}, options_set_colors_type{Reset: reset}, &payload)
|
||||
want_error := test.invalid && !reset
|
||||
if (err != nil) != want_error {
|
||||
t.Fatalf("expected error=%t, got: %v", want_error, err)
|
||||
|
|
@ -34,9 +31,8 @@ func TestSetColorsResetIgnoresArguments(t *testing.T) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
payload := io_data.rc.Payload.(set_colors_json_type)
|
||||
if payload.Reset != reset {
|
||||
t.Fatalf("expected reset=%t, got: %#v", reset, payload)
|
||||
if payload.Reset != reset || payload.All != reset || payload.Configured != reset {
|
||||
t.Fatalf("reset=%t should set reset, all and configured to the same value, got: %#v", reset, payload)
|
||||
}
|
||||
if reset {
|
||||
if len(payload.Colors) != 0 {
|
||||
|
|
@ -49,3 +45,48 @@ func TestSetColorsResetIgnoresArguments(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetColorsConfFiles(t *testing.T) {
|
||||
tdir := t.TempDir()
|
||||
write := func(name string, lines ...string) string {
|
||||
path := filepath.Join(tdir, name)
|
||||
if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
write("base.conf", "selection_background #00ff00")
|
||||
good := write("good.conf",
|
||||
"# a comment",
|
||||
"foreground\t#ff0000",
|
||||
" background #0000ff",
|
||||
"font_size 12",
|
||||
"cursor none",
|
||||
"background_opacity 0.5",
|
||||
"wayland_titlebar_color background",
|
||||
"include base.conf",
|
||||
"color1 #"+strings.Repeat("a", 6)+" "+strings.Repeat(" ", 70000),
|
||||
)
|
||||
payload := set_colors_json_type{}
|
||||
if err := parse_colors_and_files([]string{good}, options_set_colors_type{}, &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := map[string]any{
|
||||
"foreground": uint32(0xff0000), "background": uint32(0xff), "cursor": nil,
|
||||
"selection_background": uint32(0xff00), "color1": uint32(0xaaaaaa),
|
||||
}
|
||||
if len(payload.Colors) != len(expected) {
|
||||
t.Fatalf("unexpected colors parsed from conf file: %#v", payload.Colors)
|
||||
}
|
||||
for k, v := range expected {
|
||||
if actual, found := payload.Colors[k]; !found || actual != v {
|
||||
t.Fatalf("%s: expected %#v got %#v", k, v, actual)
|
||||
}
|
||||
}
|
||||
|
||||
bad := write("bad.conf", "background #000000", "foreground #zzzzzz")
|
||||
err := parse_colors_and_files([]string{bad}, options_set_colors_type{}, &set_colors_json_type{})
|
||||
if err == nil || !strings.Contains(err.Error(), "foreground") {
|
||||
t.Fatalf("expected an error for an invalid color in a conf file, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue