mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-10-29 19:56:11 +00:00
resize_window kitten: Go RC escape-code helper
This commit is contained in:
parent
2607d22ac3
commit
9e34cad010
2 changed files with 66 additions and 0 deletions
28
kittens/resize_window/rc.go
Normal file
28
kittens/resize_window/rc.go
Normal 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
|
||||
}
|
||||
38
kittens/resize_window/rc_test.go
Normal file
38
kittens/resize_window/rc_test.go
Normal 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")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue