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:35:59 +01:00
commit 3d174c45a4
1134 files changed
+433629

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package shlex
import (
"fmt"
"strconv"
"strings"
"unicode/utf8"
)
var _ = fmt.Print
type state int
const (
normal state = iota
control_char
backslash
hex_digit
oct_digit
)
type ansi_c struct {
state state
max_num_of_digits, digit_idx int
digits [16]rune
output strings.Builder
}
func is_hex_char(ch rune) bool {
return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F')
}
func is_oct_char(ch rune) bool {
return '0' <= ch && ch <= '7'
}
func (self *ansi_c) write_digits(base int) {
if self.digit_idx > 0 {
text := string(self.digits[:self.digit_idx])
if val, err := strconv.ParseUint(text, base, 32); err == nil && val <= utf8.MaxRune {
self.output.WriteRune(rune(val))
}
}
self.digit_idx = 0
self.state = normal
}
func (self *ansi_c) parse(ch rune) {
switch self.state {
case normal:
if ch == '\\' {
self.state = backslash
} else {
self.output.WriteRune(ch)
}
case control_char:
self.output.WriteRune(ch & 0x1f)
self.state = normal
case hex_digit:
if self.digit_idx < self.max_num_of_digits && is_hex_char(ch) {
self.digits[self.digit_idx] = ch
self.digit_idx++
} else {
self.write_digits(16)
self.parse(ch)
}
case oct_digit:
if self.digit_idx < self.max_num_of_digits && is_oct_char(ch) {
self.digits[self.digit_idx] = ch
self.digit_idx++
} else {
self.write_digits(8)
self.parse(ch)
}
case backslash:
self.state = normal
switch ch {
default:
self.output.WriteRune('\\')
self.output.WriteRune(ch)
case 'a':
self.output.WriteRune(7)
case 'b':
self.output.WriteRune(8)
case 'c':
self.state = control_char
case 'e', 'E':
self.output.WriteRune(27)
case 'f':
self.output.WriteRune(12)
case 'n':
self.output.WriteRune(10)
case 'r':
self.output.WriteRune(13)
case 't':
self.output.WriteRune(9)
case 'v':
self.output.WriteRune(11)
case 'x':
self.max_num_of_digits, self.digit_idx, self.state = 2, 0, hex_digit
case 'u':
self.max_num_of_digits, self.digit_idx, self.state = 4, 0, hex_digit
case 'U':
self.max_num_of_digits, self.digit_idx, self.state = 8, 0, hex_digit
case '0', '1', '2', '3', '4', '5', '6', '7':
self.max_num_of_digits, self.digit_idx, self.state = 3, 1, oct_digit
self.digits[0] = ch
case '\\':
self.output.WriteRune('\\')
case '?':
self.output.WriteRune('?')
case '"':
self.output.WriteRune('"')
case '\'':
self.output.WriteRune('\'')
}
}
}
func (self *ansi_c) finish() string {
switch self.state {
case hex_digit:
self.write_digits(16)
case oct_digit:
self.write_digits(8)
case backslash:
self.output.WriteRune('\\')
case control_char:
self.output.WriteString("\\c")
}
self.state = normal
self.digit_idx = 0
s := self.output.String()
self.output.Reset()
return s
}
func ExpandANSICEscapes(src string) string {
p := ansi_c{}
p.output.Grow(len(src))
for _, ch := range src {
p.parse(ch)
}
return p.finish()
}
+232
View File
@@ -0,0 +1,232 @@
/*
Package shlex implements a simple lexer which splits input in to tokens using
shell-style rules for quoting.
The basic use case uses the default ASCII lexer to split a string into sub-strings:
shlex.Split("one \"two three\" four") -> []string{"one", "two three", "four"}
To process a stream of strings:
l := NewLexer(os.Stdin)
for ; token, err := l.Next(); err != nil {
// process token
}
*/
package shlex
import (
"fmt"
"github.com/kovidgoyal/kitty/tools/utils"
"strings"
"unicode/utf8"
)
type Word struct {
Value string // The word, which can be empty if it was an empty quoted string
Pos int // The position in the input string of the word or the trailer, -1 if EOF is reached
Err error // Indicates an error (unterminated string or trailing unescaped backslash)
Trailer string // Extra trailing data such as an unterminated string or an unescaped backslash. Present only if Err != nil
}
type lexer_state int
// Lexer state machine states
const (
lex_normal lexer_state = iota
word
string_without_escapes
string_with_escapes
)
// Lexer turns an input stream into a sequence of tokens. Whitespace is skipped.
type Lexer struct {
state lexer_state
src string
src_sz, src_pos, word_start int
buf strings.Builder
allow_empty bool
}
// NewLexer creates a new lexer from an input string.
func NewLexer(x string) *Lexer {
return &Lexer{src: x, src_sz: len(x)}
}
func (self *Lexer) start_word() {
self.buf.Reset()
self.word_start = self.src_pos - 1
}
func (self *Lexer) get_word() Word {
self.allow_empty = false
return Word{Pos: self.word_start, Value: self.buf.String()}
}
func (self *Lexer) write_ch(ch byte) {
self.buf.WriteByte(ch)
}
func (self *Lexer) write_escaped_ch() bool {
ch, count := utf8.DecodeRuneInString(self.src[self.src_pos:])
if count > 0 {
self.src_pos += count
// Invalid UTF-8 always decodes as (RuneError, 1), so a larger size means a real U+FFFD.
if ch != utf8.RuneError || count > 1 {
self.buf.WriteRune(ch)
}
return true
}
return false
}
// Next returns the next word. At EOF Word.Pos will be -1
func (self *Lexer) Next() (ans Word) {
const string_with_escapes_delim = '"'
const string_without_escapes_delim = '\''
const escape_char = '\\'
for self.src_pos < self.src_sz {
ch := self.src[self.src_pos]
self.src_pos++
switch self.state {
case lex_normal:
switch ch {
case ' ', '\n', '\r', '\t':
case string_with_escapes_delim:
self.state = string_with_escapes
self.start_word()
case string_without_escapes_delim:
self.state = string_without_escapes
self.start_word()
case escape_char:
self.start_word()
if !self.write_escaped_ch() {
ans.Trailer = "\\"
ans.Err = fmt.Errorf("Extra backslash at end of input")
ans.Pos = self.word_start
return
}
self.state = word
default:
self.state = word
self.start_word()
self.write_ch(ch)
}
case word:
switch ch {
case ' ', '\n', '\r', '\t':
self.state = lex_normal
if self.buf.Len() > 0 || self.allow_empty {
return self.get_word()
}
case string_with_escapes_delim:
self.state = string_with_escapes
case string_without_escapes_delim:
self.state = string_without_escapes
case escape_char:
if !self.write_escaped_ch() {
ans.Pos = self.word_start
ans.Trailer = self.buf.String() + "\\"
ans.Err = fmt.Errorf("Extra backslash at end of input")
return
}
default:
self.write_ch(ch)
}
case string_without_escapes:
switch ch {
case string_without_escapes_delim:
self.state = word
self.allow_empty = true
default:
self.write_ch(ch)
}
case string_with_escapes:
switch ch {
case string_with_escapes_delim:
self.state = word
self.allow_empty = true
case escape_char:
self.write_escaped_ch()
default:
self.write_ch(ch)
}
}
}
switch self.state {
case word:
self.state = lex_normal
if self.buf.Len() > 0 || self.allow_empty {
return self.get_word()
}
case string_with_escapes, string_without_escapes:
self.state = lex_normal
ans.Trailer = self.buf.String()
ans.Pos = self.word_start
ans.Err = fmt.Errorf("Unterminated string at end of input")
return
case lex_normal:
}
ans.Pos = -1
return
}
// Split partitions a string into a slice of strings.
func Split(s string) (ans []string, err error) {
l := NewLexer(s)
var word Word
for {
word = l.Next()
if word.Err != nil {
return ans, word.Err
}
if word.Pos < 0 {
break
}
ans = append(ans, word.Value)
}
return
}
func Quote(s string) string {
if s == "" {
return s
}
if utils.MustCompile(`[^\w@%+=:,./-]`).MatchString(s) {
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
}
return s
}
// SplitForCompletion partitions a string into a slice of strings. It differs from Split in being
// more relaxed about errors and also adding an empty string at the end if s ends with a Space.
func SplitForCompletion(s string) (argv []string, position_of_last_arg int) {
t := NewLexer(s)
argv = make([]string, 0, len(s)/4)
for {
word := t.Next()
if word.Pos < 0 || word.Err != nil {
if word.Trailer == "" {
trimmed := strings.TrimRight(s, " ")
if len(trimmed) < len(s) { // trailing spaces
pos := position_of_last_arg
if len(argv) > 0 {
pos += len(argv[len(argv)-1])
}
if pos < len(s) { // trailing whitespace
argv = append(argv, "")
position_of_last_arg = len(s)
}
}
} else {
argv = append(argv, word.Trailer)
position_of_last_arg = word.Pos
}
break
}
position_of_last_arg = word.Pos
argv = append(argv, word.Value)
}
return
}
+145
View File
@@ -0,0 +1,145 @@
package shlex
import (
"testing"
"github.com/google/go-cmp/cmp"
)
var (
// one two "three four" "five \"six\"" seven#eight # nine # ten
// eleven 'twelve\'
testString = "one two \"three four\" \"five \\\"six\\\"\" seven#eight # nine # ten eleven 'twelve\\' thirteen=13 fourteen/14"
)
func TestLexer(t *testing.T) {
testInput := testString
expectedStrings := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "#", "nine", "#", "ten", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
lexer := NewLexer(testInput)
for i, want := range expectedStrings {
got := lexer.Next()
if got.Value != want {
t.Errorf("Lexer.Next()[%v] of %q -> %v. Want: %v", i, testString, got, want)
}
}
}
type Tok struct {
Pos int
Val string
}
func TestSplit(t *testing.T) {
want := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "#", "nine", "#", "ten", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
got, err := Split(testString)
if err != nil {
t.Error(err)
}
if len(want) != len(got) {
t.Errorf("Split(%q) -> %v. Want: %v", testString, got, want)
}
for i := range got {
if got[i] != want[i] {
t.Errorf("Split(%q)[%v] -> %v. Want: %v", testString, i, got[i], want[i])
}
}
for _, x := range []string{
`abc\`, `\`, `'abc`, `'`, `"`, `asd\`,
} {
_, err := Split(x)
if err == nil {
t.Fatalf("Failed to get an error for: %#v", x)
}
}
s := func(q string) (ans []Tok) {
l := NewLexer(q)
for {
w := l.Next()
if w.Err != nil {
t.Fatal(w.Err)
}
if w.Pos < 0 {
break
}
ans = append(ans, Tok{w.Pos, w.Value})
}
return
}
for q, expected := range map[string][]Tok{
`a""`: {{0, "a"}},
`a""b`: {{0, "ab"}},
`-1 "" 2`: {{0, "-1"}, {3, ""}, {6, "2"}},
`-1 '' 2`: {{0, "-1"}, {3, ""}, {6, "2"}},
`a ""`: {{0, "a"}, {2, ""}},
`""`: {{0, ""}},
`"ab"`: {{0, "ab"}},
`x "ab"y \m`: {{0, `x`}, {2, `aby`}, {8, `m`}},
`x'y"\z'1`: {{0, `xy"\z1`}},
`\abc\ d`: {{0, `abc d`}},
``: nil,
` `: nil,
" \tabc\n\t\r ": {{2, "abc"}},
"a\\\xffb": {{0, "ab"}}, // an escaped invalid byte is still dropped
// An escaped U+FFFD is a real character, not a decoding failure, so it
// survives the escape the way it does unescaped or single quoted.
"a\\�b": {{0, "a�b"}},
"\"a\\�b\"": {{0, "a�b"}},
} {
if diff := cmp.Diff(expected, s(q)); diff != "" {
t.Fatalf("Failed for string: %#v\n%s", q, diff)
}
}
}
func TestSplitForCompletion(t *testing.T) {
test := func(cmdline string, last_arg_pos int, expected ...string) {
actual, actual_pos := SplitForCompletion(cmdline)
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("Failed to split: %s\n%s", cmdline, diff)
}
if last_arg_pos != actual_pos {
t.Fatalf("Failed to split: %#v\n Last arg pos: %d != %d", cmdline, last_arg_pos, actual_pos)
}
}
test("a b", 2, "a", "b")
test("a b ", 4, "a", "b", "")
test("a b ", 5, "a", "b", "")
test("ab cd ", 6, "ab", "cd", "")
test("launch --type ", 14, "launch", "--type", "")
test(`a "b c" `, 8, "a", "b c", "")
test(`a "b c"`, 2, "a", "b c")
test(`a "b c`, 2, "a", "b c")
test(`a "" b`, 5, "a", "", "b")
test(`a '' `, 5, "a", "", "")
}
func TestExpandANSICEscapes(t *testing.T) {
var m = map[string]string{
"abc": "abc",
`a\ab`: "a\ab",
`a\eb`: "a\x1bb",
`a\r\nb`: "a\r\nb",
`a\c b`: "a\000b",
`a\c`: "a\\c",
`a\x1bb`: "a\x1bb",
`a\x1b`: "a\x1b",
`a\x1`: "a\x01",
`a\x1\\`: "a\x01\\",
`a\x1g`: "a\x01g",
`a\z\"`: "a\\z\"",
`a\123b`: "a\123b",
`a\128b`: "a\0128b",
`a\u1234e`: "a\u1234e",
`a\U1f1eez`: "a\U0001f1eez",
}
for q, expected := range m {
actual := ExpandANSICEscapes(q)
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("Failed to process: %#v\n%s", q, diff)
}
}
}