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

@@ -30,9 +30,11 @@ func json_output_serializer(completions []*Completions, shell_state map[string]s
return json.Marshal(completions)
}
type completion_script_func func(commands []string) ([]byte, error)
type parser_func func(data []byte, shell_state map[string]string) ([][]string, error)
type serializer_func func(completions []*Completions, shell_state map[string]string) ([]byte, error)
var completion_scripts = make(map[string]completion_script_func, 4)
var input_parsers = make(map[string]parser_func, 4)
var output_serializers = make(map[string]serializer_func, 4)
var init_completions = make(map[string]func(*Completions), 4)
@@ -61,6 +63,22 @@ func GenerateCompletions(args []string) error {
if n < 1 {
n = 1
}
if output_type == "setup" {
if len(args) == 0 {
return fmt.Errorf("The shell needs to be specified")
}
shell_name := args[0]
args = args[1:]
completion_script := completion_scripts[shell_name]
if completion_script == nil {
return fmt.Errorf("Unsupported shell: %s", shell_name)
}
output, err := completion_script(args)
if err == nil {
_, err = os.Stdout.Write(output)
}
return err
}
shell_state := make(map[string]string, n)
for _, arg := range args {
k, v, found := utils.Cut(arg, "=")