chore: import official kitty sources from upstream

Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty,
vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
BlackAngelTV committed 2026-11-04 11:20:47 +01:00
commit 7e7d249099
1134 files changed
+433629

No files matched your search

View File
Whitespace-only changes.
+80
View File
@@ -0,0 +1,80 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package broadcast
import "github.com/kovidgoyal/kitty/tools/tui/loop"
// Minimal line editor: covers the subset of kittens/tui/line_edit.py that
// the broadcast kitten actually uses (insert, backspace, arrows, home/end).
type line_edit struct {
text []rune
cursor int
}
func (l *line_edit) String() string { return string(l.text) }
func (l *line_edit) on_text(t string) {
r := []rune(t)
l.text = append(l.text[:l.cursor], append(r, l.text[l.cursor:]...)...)
l.cursor += len(r)
}
func (l *line_edit) backspace() bool {
if l.cursor == 0 {
return false
}
l.text = append(l.text[:l.cursor-1], l.text[l.cursor:]...)
l.cursor--
return true
}
func (l *line_edit) left() {
if l.cursor > 0 {
l.cursor--
}
}
func (l *line_edit) right() {
if l.cursor < len(l.text) {
l.cursor++
}
}
func (l *line_edit) home() { l.cursor = 0 }
func (l *line_edit) end() { l.cursor = len(l.text) }
func (l *line_edit) clear() {
l.text = l.text[:0]
l.cursor = 0
}
// on_key returns true if the key was consumed as an editing action.
func (l *line_edit) on_key(e *loop.KeyEvent) bool {
switch {
case e.MatchesPressOrRepeat("backspace"):
e.Handled = true
return l.backspace()
case e.MatchesPressOrRepeat("left"):
e.Handled = true
l.left()
return true
case e.MatchesPressOrRepeat("right"):
e.Handled = true
l.right()
return true
case e.MatchesPressOrRepeat("home"), e.MatchesPressOrRepeat("ctrl+a"):
e.Handled = true
l.home()
return true
case e.MatchesPressOrRepeat("end"), e.MatchesPressOrRepeat("ctrl+e"):
e.Handled = true
l.end()
return true
}
return false
}
// write draws the current line and positions the cursor.
func (l *line_edit) write(lp *loop.Loop) {
lp.QueueWriteString(string(l.text))
if diff := len(l.text) - l.cursor; diff > 0 {
lp.MoveCursorHorizontally(-diff)
}
}
+35
View File
@@ -0,0 +1,35 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package broadcast
import "testing"
func TestLineEdit(t *testing.T) {
le := line_edit{}
le.on_text("hello")
if le.String() != "hello" || le.cursor != 5 {
t.Fatalf("insert failed: %q cursor=%d", le.String(), le.cursor)
}
le.backspace()
if le.String() != "hell" {
t.Fatalf("backspace failed: %q", le.String())
}
le.left()
le.left()
le.on_text("X")
if le.String() != "heXll" || le.cursor != 3 {
t.Fatalf("mid-insert failed: %q cursor=%d", le.String(), le.cursor)
}
le.home()
if le.cursor != 0 {
t.Fatal("home failed")
}
le.end()
if le.cursor != 5 {
t.Fatal("end failed")
}
le.clear()
if le.String() != "" || le.cursor != 0 {
t.Fatal("clear failed")
}
}
+167
View File
@@ -0,0 +1,167 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package broadcast
import (
"encoding/base64"
"os"
"github.com/kovidgoyal/kitty/tools/cli"
"github.com/kovidgoyal/kitty/tools/cli/markup"
"github.com/kovidgoyal/kitty/tools/tui/loop"
"github.com/kovidgoyal/kitty/tools/utils"
)
type handler struct {
lp *loop.Loop
opts *Options
ctx *markup.Context
payload broadcast_payload
initial_strings []string
hide_input bool
session_started bool
line_edit line_edit
}
func b64text(text string) string {
return "base64:" + base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(text))
}
func (h *handler) broadcast_data(data string) error {
p := h.payload
p.Data = data
ec, err := send_text_escape_code(p)
if err != nil {
return err
}
h.lp.QueueWriteString(ec)
return nil
}
func (h *handler) broadcast_text(text string) error { return h.broadcast_data(b64text(text)) }
func (h *handler) commit_line() {
h.lp.RestoreCursorPosition()
h.lp.SaveCursorPosition()
h.lp.ClearToEndOfScreen()
h.line_edit.write(h.lp)
}
func (h *handler) end_line() {
h.lp.Println()
h.line_edit.clear()
h.lp.SaveCursorPosition()
}
func (h *handler) initialize() (string, error) {
h.session_started = true
ec, err := session_escape_code(h.payload, true)
if err != nil {
return "", err
}
h.lp.QueueWriteString(ec)
h.lp.Println("Type the text to broadcast below, press", h.ctx.Yellow(h.opts.EndSession), "to quit:")
for _, x := range h.initial_strings {
if err := h.broadcast_text(x); err != nil {
return "", err
}
}
h.lp.SaveCursorPosition()
return "", nil
}
func (h *handler) on_text(text string) error {
if err := h.broadcast_text(text); err != nil {
return err
}
if !h.hide_input {
h.line_edit.on_text(text)
}
h.commit_line()
return nil
}
func (h *handler) on_key(e *loop.KeyEvent) error {
if e.MatchesPressOrRepeat(h.opts.HideInputToggle) {
e.Handled = true
h.hide_input = !h.hide_input
h.lp.SetCursorVisible(!h.hide_input)
if h.hide_input {
h.end_line()
h.lp.Println("Input hidden, press", h.ctx.Yellow(h.opts.HideInputToggle), "to unhide:")
h.end_line()
}
return nil
}
if e.MatchesPressOrRepeat(h.opts.EndSession) {
e.Handled = true
h.lp.Quit(0)
return nil
}
if e.MatchesPressOrRepeat("ctrl+c") {
e.Handled = true
if err := h.broadcast_text("\x03"); err != nil {
return err
}
h.line_edit.clear()
h.commit_line()
return nil
}
if e.MatchesPressOrRepeat("ctrl+d") {
e.Handled = true
return h.broadcast_text("\x04")
}
if !h.hide_input && h.line_edit.on_key(e) {
h.commit_line()
}
if e.MatchesPressOrRepeat("enter") {
e.Handled = true
if err := h.broadcast_text("\r"); err != nil {
return err
}
h.end_line()
return nil
}
if e.Text == "" { // non-text key: forward in kitty-key encoding
e.Handled = true
return h.broadcast_data("kitty-key:" + base64.StdEncoding.EncodeToString([]byte(e.AsCSI())))
}
return nil
}
func run_loop(opts *Options, args []string) (rc int, err error) {
lp, err := loop.New(loop.FullKeyboardProtocol, loop.NoAlternateScreen)
if err != nil {
return 1, err
}
h := &handler{lp: lp, opts: opts, ctx: markup.New(true), initial_strings: args}
h.payload = broadcast_payload{
ExcludeActive: true, Match: opts.Match, MatchTab: opts.MatchTab, SessionId: new_session_id(),
}
if opts.Match == "" && opts.MatchTab == "" {
h.payload.All = true
}
lp.OnInitialize = h.initialize
lp.OnResize = func(old, new loop.ScreenSize) error { h.commit_line(); return nil }
lp.OnText = func(text string, from_key_event, in_bracketed_paste bool) error { return h.on_text(text) }
lp.OnKeyEvent = h.on_key
err = lp.Run()
if h.session_started {
if ec, err2 := session_escape_code(h.payload, false); err2 == nil {
os.Stdout.WriteString(ec)
}
}
if err != nil {
return 1, err
}
lp.KillIfSignalled()
return lp.ExitCode(), nil
}
func main(cmd *cli.Command, opts *Options, args []string) (rc int, err error) {
return run_loop(opts, args)
}
func EntryPoint(parent *cli.Command) {
create_cmd(parent, main)
}
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
import sys
from kitty.rc.base import MATCH_TAB_OPTION, MATCH_WINDOW_OPTION
OPTIONS = (
"""
--hide-input-toggle
default=Ctrl+Alt+Esc
Key to press that will toggle hiding of the input in the broadcast window itself.
Useful while typing a password, prevents the password from being visible on the screen.
--end-session
default=Ctrl+Esc
Key to press to end the broadcast session.
"""
+ MATCH_WINDOW_OPTION
+ '\n\n'
+ MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t')
).format
help_text = 'Broadcast typed text to kitty windows. By default text is sent to all windows, unless one of the matching options is specified'
usage = '[initial text to send ...]'
def main(args: list[str]) -> None:
raise SystemExit('This should be run as kitten broadcast')
if __name__ == '__main__':
main(sys.argv)
elif __name__ == '__doc__':
cd = sys.cli_docs # type: ignore
cd['usage'] = usage
cd['options'] = OPTIONS
cd['help_text'] = help_text
cd['short_desc'] = 'Broadcast typed text to kitty windows'
+45
View File
@@ -0,0 +1,45 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package broadcast
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"github.com/kovidgoyal/kitty/tools/cmd/at"
"github.com/kovidgoyal/kitty/tools/utils"
)
type broadcast_payload struct {
ExcludeActive bool `json:"exclude_active"`
Data string `json:"data"`
Match string `json:"match,omitempty"`
MatchTab string `json:"match_tab,omitempty"`
SessionId string `json:"session_id"`
All bool `json:"all,omitempty"`
}
func send_text_escape_code(p broadcast_payload) (string, error) {
rc := utils.RemoteControlCmd{Cmd: "send-text", Version: at.ProtocolVersion, NoResponse: true, Payload: p}
data, err := json.Marshal(rc)
if err != nil {
return "", err
}
return "\x1bP@kitty-cmd" + string(data) + "\x1b\\", nil
}
func session_escape_code(p broadcast_payload, start bool) (string, error) {
if start {
p.Data = "session:start"
} else {
p.Data = "session:end"
}
return send_text_escape_code(p)
}
func new_session_id() string {
b := make([]byte, 16)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}
+60
View File
@@ -0,0 +1,60 @@
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package broadcast
import (
"encoding/json"
"strings"
"testing"
)
func decode(t *testing.T, ec string) map[string]any {
t.Helper()
if !strings.HasPrefix(ec, "\x1bP@kitty-cmd") || !strings.HasSuffix(ec, "\x1b\\") {
t.Fatalf("bad framing: %q", ec)
}
body := strings.TrimSuffix(strings.TrimPrefix(ec, "\x1bP@kitty-cmd"), "\x1b\\")
var m map[string]any
if err := json.Unmarshal([]byte(body), &m); err != nil {
t.Fatal(err)
}
return m
}
func TestSendTextEscapeCode(t *testing.T) {
p := broadcast_payload{ExcludeActive: true, Data: "base64:aGk=", SessionId: "abc", All: true}
ec, err := send_text_escape_code(p)
if err != nil {
t.Fatal(err)
}
m := decode(t, ec)
if m["cmd"] != "send-text" || m["no_response"] != true {
t.Fatalf("bad cmd: %v", m)
}
payload := m["payload"].(map[string]any)
if payload["data"] != "base64:aGk=" || payload["exclude_active"] != true || payload["all"] != true || payload["session_id"] != "abc" {
t.Fatalf("bad payload: %v", payload)
}
if _, has := payload["match"]; has {
t.Fatal("empty match should be omitted")
}
}
func TestSessionEscapeCode(t *testing.T) {
p := broadcast_payload{SessionId: "abc"}
ec, err := session_escape_code(p, false)
if err != nil {
t.Fatal(err)
}
payload := decode(t, ec)["payload"].(map[string]any)
if payload["data"] != "session:end" {
t.Fatalf("bad session data: %v", payload)
}
}
func TestNewSessionId(t *testing.T) {
a, b := new_session_id(), new_session_id()
if a == b || len(a) != 32 {
t.Fatalf("bad session ids: %q %q", a, b)
}
}