Completion: Delegate kitty +complete to kitten

Implement `kitten __complete__ setup` in Go.
Fix zsh completion script to check `kitten`.
This commit is contained in:
pagedown
2023-02-03 18:16:04 +08:00
parent bed4f33be8
commit 370aa3aaa6
8 changed files with 120 additions and 3 deletions

View File

@@ -12,12 +12,53 @@ import (
var _ = fmt.Print
func fish_completion_script(commands []string) ([]byte, error) {
// One command in fish requires one completion script.
// Usage: kitten __complete__ setup fish [kitty|kitten|clone-in-kitty]
all_commands := map[string]bool{
"kitty": true,
"clone-in-kitty": true,
"kitten": true,
}
if len(commands) == 0 {
for cmd, _ := range all_commands {
commands = append(commands, cmd)
}
}
script := strings.Builder{}
script.WriteString(`function __ksi_completions
set --local ct (commandline --current-token)
set --local tokens (commandline --tokenize --cut-at-cursor --current-process)
printf "%s\n" $tokens $ct | command kitten __complete__ fish | source -
end
`)
for _, cmd := range commands {
if all_commands[cmd] {
fmt.Fprintf(&script, "complete -f -c %s -a \"(__ksi_completions)\"\n", cmd)
} else if strings.Contains(cmd, "=") {
// Reserved for `setup SHELL [KEY=VALUE ...]`, not used now.
continue
} else {
return nil, fmt.Errorf("No fish completion script for command: %s", cmd)
}
}
return []byte(script.String()), nil
}
func fish_output_serializer(completions []*Completions, shell_state map[string]string) ([]byte, error) {
output := strings.Builder{}
f := func(format string, args ...any) { fmt.Fprintf(&output, format+"\n", args...) }
n := completions[0].Delegate.NumToRemove
fm := markup.New(false) // fish freaks out if there are escape codes in the description strings
if n > 0 {
legacy_completion := shell_state["_legacy_completion"]
if legacy_completion == "fish2" {
for _, mg := range completions[0].Groups {
for _, m := range mg.Matches {
f("%s", strings.ReplaceAll(m.Word+"\t"+fm.Prettify(m.Description), "\n", " "))
}
}
} else if n > 0 {
words := make([]string, len(completions[0].AllWords)-n+1)
words[0] = completions[0].Delegate.Command
copy(words[1:], completions[0].AllWords[n:])
@@ -40,6 +81,7 @@ func fish_output_serializer(completions []*Completions, shell_state map[string]s
}
func init() {
completion_scripts["fish"] = fish_completion_script
input_parsers["fish"] = shell_input_parser
output_serializers["fish"] = fish_output_serializer
}