resize_window kitten: Go RC escape-code helper

This commit is contained in:
OBrutus 2026-08-06 19:24:28 +05:30
parent 2607d22ac3
commit 9e34cad010
2 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,28 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package resize_window
import (
"encoding/json"
"github.com/kovidgoyal/kitty/tools/cmd/at"
"github.com/kovidgoyal/kitty/tools/utils"
)
type resize_payload struct {
Increment int `json:"increment"`
Axis string `json:"axis"`
Self bool `json:"self"`
}
func resize_command_escape_code(increment int, axis string) (string, error) {
rc := utils.RemoteControlCmd{
Cmd: "resize-window", Version: at.ProtocolVersion,
Payload: resize_payload{Increment: increment, Axis: axis, Self: true},
}
data, err := json.Marshal(rc)
if err != nil {
return "", err
}
return "\x1bP@kitty-cmd" + string(data) + "\x1b\\", nil
}

View file

@ -0,0 +1,38 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package resize_window
import (
"encoding/json"
"strings"
"testing"
)
func TestResizeCommandEscapeCode(t *testing.T) {
ec, err := resize_command_escape_code(-4, "vertical")
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(ec, "\x1bP@kitty-cmd") || !strings.HasSuffix(ec, "\x1b\\") {
t.Fatalf("bad framing: %q", ec)
}
var cmd struct {
Cmd string `json:"cmd"`
Version [3]int `json:"version"`
Payload struct {
Increment int `json:"increment"`
Axis string `json:"axis"`
Self bool `json:"self"`
} `json:"payload"`
}
body := strings.TrimSuffix(strings.TrimPrefix(ec, "\x1bP@kitty-cmd"), "\x1b\\")
if err := json.Unmarshal([]byte(body), &cmd); err != nil {
t.Fatal(err)
}
if cmd.Cmd != "resize-window" || cmd.Payload.Increment != -4 || cmd.Payload.Axis != "vertical" || !cmd.Payload.Self {
t.Fatalf("bad command: %+v", cmd)
}
if cmd.Version == [3]int{} {
t.Fatal("version not set")
}
}