mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-17 05:54:59 +02:00
more work on porting rc command parsing to Go
This commit is contained in:
60
tools/cmd/at/send_text.go
Normal file
60
tools/cmd/at/send_text.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package at
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type generator_function func(io_data *rc_io_data) (bool, error)
|
||||
|
||||
func parse_send_text(io_data *rc_io_data, args []string) error {
|
||||
generators := make([]generator_function, 0, 1)
|
||||
var payload send_text_json_type = io_data.rc.Payload.(send_text_json_type)
|
||||
|
||||
if len(args) > 0 {
|
||||
text := strings.Join(args, " ")
|
||||
text_gen := func(io_data *rc_io_data) (bool, error) {
|
||||
payload.Data = "text:" + text[:2048]
|
||||
text = text[2048:]
|
||||
return len(text) == 0, nil
|
||||
}
|
||||
generators = append(generators, text_gen)
|
||||
}
|
||||
|
||||
if options_send_text.from_file != "" {
|
||||
f, err := os.Open(options_send_text.from_file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
chunk := make([]byte, 2048)
|
||||
file_gen := func(io_data *rc_io_data) (bool, error) {
|
||||
n, err := f.Read(chunk)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return false, err
|
||||
}
|
||||
payload.Data = "base64:" + base64.StdEncoding.EncodeToString(chunk[:n])
|
||||
return n == 0, nil
|
||||
}
|
||||
generators = append(generators, file_gen)
|
||||
}
|
||||
|
||||
io_data.multiple_payload_generator = func(io_data *rc_io_data) (bool, error) {
|
||||
if len(generators) == 0 {
|
||||
payload.Data = "text:"
|
||||
return true, nil
|
||||
}
|
||||
finished, err := generators[0](io_data)
|
||||
if finished {
|
||||
generators = generators[1:]
|
||||
finished = len(generators) == 0
|
||||
}
|
||||
return finished, err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
74
tools/cmd/at/set_colors.go
Normal file
74
tools/cmd/at/set_colors.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package at
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"kitty/tools/utils"
|
||||
"kitty/tools/utils/style"
|
||||
)
|
||||
|
||||
var nullable_colors = map[string]bool{
|
||||
// generated by gen-config.py do not edit
|
||||
// NULLABLE_COLORS_START
|
||||
"active_border_color": true,
|
||||
"cursor": true,
|
||||
"cursor_text_color": true,
|
||||
"selection_background": true,
|
||||
"selection_foreground": true,
|
||||
"tab_bar_background": true,
|
||||
"tab_bar_margin_color": true,
|
||||
"visual_bell_color": true,
|
||||
// NULLABLE_COLORS_END
|
||||
}
|
||||
|
||||
func set_color_in_color_map(key, val string, ans map[string]interface{}, check_nullable, skip_nullable bool) error {
|
||||
if val == "none" {
|
||||
if check_nullable && !nullable_colors[key] {
|
||||
if skip_nullable {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("The color %s cannot be set to none", key)
|
||||
}
|
||||
ans[key] = nil
|
||||
} else {
|
||||
col, err := style.ParseColor(val)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s is not a valid color", val)
|
||||
}
|
||||
ans[key] = col.AsRGB()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parse_colors_and_files(args []string) (map[string]interface{}, error) {
|
||||
ans := make(map[string]interface{}, len(args))
|
||||
for _, arg := range args {
|
||||
key, val, found := utils.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 := utils.Cut(scanner.Text(), " ")
|
||||
if found {
|
||||
set_color_in_color_map(strings.ToLower(key), strings.ToLower(strings.TrimSpace(val)), ans, true, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans, nil
|
||||
}
|
||||
20
tools/cmd/at/set_font_size.go
Normal file
20
tools/cmd/at/set_font_size.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package at
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func parse_set_font_size(arg string, io_data *rc_io_data) error {
|
||||
payload := io_data.rc.Payload.(set_font_size_json_type)
|
||||
if len(arg) > 0 && (arg[0] == '+' || arg[0] == '-') {
|
||||
payload.Increment_op = arg[:1]
|
||||
}
|
||||
val, err := strconv.ParseFloat(arg, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload.Size = val
|
||||
return nil
|
||||
}
|
||||
53
tools/cmd/at/set_spacing.go
Normal file
53
tools/cmd/at/set_spacing.go
Normal file
@@ -0,0 +1,53 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package at
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
func parse_set_spacing(args []string) (map[string]interface{}, error) {
|
||||
ans := make(map[string]interface{}, len(args))
|
||||
mapper := make(map[string][]string, 32)
|
||||
types := [2]string{"margin", "padding"}
|
||||
for _, q := range types {
|
||||
mapper[q] = []string{q + "-left", q + "-top", q + "-right", q + "-bottom"}
|
||||
mapper[q+"-h"] = []string{q + "-left", q + "-right"}
|
||||
mapper[q+"-v"] = []string{q + "-top", q + "-bottom"}
|
||||
mapper[q+"-left"] = []string{q + "left"}
|
||||
mapper[q+"-right"] = []string{q + "right"}
|
||||
mapper[q+"-top"] = []string{q + "top"}
|
||||
mapper[q+"-bottom"] = []string{q + "bottom"}
|
||||
}
|
||||
for _, arg := range args {
|
||||
k, v, found := utils.Cut(arg, "=")
|
||||
if !found {
|
||||
return nil, fmt.Errorf("%s is not a valid setting", arg)
|
||||
}
|
||||
k = strings.ToLower(k)
|
||||
v = strings.ToLower(v)
|
||||
which, found := mapper[k]
|
||||
if !found {
|
||||
return nil, fmt.Errorf("%s is not a valid edge specification", k)
|
||||
}
|
||||
if v == "default" {
|
||||
for _, q := range which {
|
||||
ans[q] = nil
|
||||
}
|
||||
} else {
|
||||
val, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s is not a number", v)
|
||||
}
|
||||
for _, q := range which {
|
||||
ans[q] = val
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return ans, nil
|
||||
}
|
||||
30
tools/cmd/at/set_tab_color.go
Normal file
30
tools/cmd/at/set_tab_color.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package at
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var valid_color_names = map[string]bool{"active_fg": true, "active_bg": true, "inactive_fg": true, "inactive_bg": true}
|
||||
|
||||
func parse_tab_colors(args []string) (map[string]interface{}, error) {
|
||||
ans := make(map[string]interface{}, len(args))
|
||||
for _, arg := range args {
|
||||
key, val, found := utils.Cut(strings.ToLower(arg), "=")
|
||||
if !found {
|
||||
return nil, fmt.Errorf("%s is not a valid setting", arg)
|
||||
}
|
||||
if !valid_color_names[key] {
|
||||
return nil, fmt.Errorf("%s is not a valid color name", key)
|
||||
}
|
||||
err := set_color_in_color_map(key, val, ans, false, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ans, nil
|
||||
}
|
||||
@@ -49,6 +49,6 @@ func read_window_logo(path string) (func(io_data *rc_io_data) (bool, error), err
|
||||
return false, err
|
||||
}
|
||||
buf = buf[:n]
|
||||
return n == 0, nil
|
||||
return false, nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user