2022-09-19 20:49:36 +03:00
|
|
|
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
|
|
|
|
2022-09-25 18:21:38 +03:00
|
|
|
package cli
|
2022-09-19 20:49:36 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"kitty/tools/cli/markup"
|
|
|
|
"kitty/tools/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = fmt.Print
|
|
|
|
|
2023-02-03 13:44:24 +03:00
|
|
|
func fish_completion_script(commands []string) (string, error) {
|
2023-02-03 13:16:04 +03:00
|
|
|
// 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 {
|
2024-05-04 12:04:40 +03:00
|
|
|
commands = append(commands, utils.Keys(all_commands)...)
|
2023-02-03 13:16:04 +03:00
|
|
|
}
|
|
|
|
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 {
|
2023-02-03 13:44:24 +03:00
|
|
|
return "", fmt.Errorf("No fish completion script for command: %s", cmd)
|
2023-02-03 13:16:04 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-03 13:44:24 +03:00
|
|
|
return script.String(), nil
|
2023-02-03 13:16:04 +03:00
|
|
|
}
|
|
|
|
|
2022-09-19 20:49:36 +03:00
|
|
|
func fish_output_serializer(completions []*Completions, shell_state map[string]string) ([]byte, error) {
|
|
|
|
output := strings.Builder{}
|
2022-09-21 05:41:46 +03:00
|
|
|
f := func(format string, args ...any) { fmt.Fprintf(&output, format+"\n", args...) }
|
2022-09-19 20:49:36 +03:00
|
|
|
n := completions[0].Delegate.NumToRemove
|
|
|
|
fm := markup.New(false) // fish freaks out if there are escape codes in the description strings
|
2023-02-03 13:16:04 +03:00
|
|
|
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 {
|
2022-09-26 05:04:49 +03:00
|
|
|
words := make([]string, len(completions[0].AllWords)-n+1)
|
2022-09-19 20:49:36 +03:00
|
|
|
words[0] = completions[0].Delegate.Command
|
2022-09-26 05:04:49 +03:00
|
|
|
copy(words[1:], completions[0].AllWords[n:])
|
2022-09-19 20:49:36 +03:00
|
|
|
for i, w := range words {
|
|
|
|
words[i] = fmt.Sprintf("(string escape -- %s)", utils.QuoteStringForFish(w))
|
|
|
|
}
|
|
|
|
cmdline := strings.Join(words, " ")
|
|
|
|
f("set __ksi_cmdline " + cmdline)
|
|
|
|
f("complete -C \"$__ksi_cmdline\"")
|
|
|
|
f("set --erase __ksi_cmdline")
|
|
|
|
} else {
|
|
|
|
for _, mg := range completions[0].Groups {
|
|
|
|
for _, m := range mg.Matches {
|
|
|
|
f("echo -- %s", utils.QuoteStringForFish(m.Word+"\t"+fm.Prettify(m.Description)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// debugf("%#v", output.String())
|
|
|
|
return []byte(output.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-02-03 13:16:04 +03:00
|
|
|
completion_scripts["fish"] = fish_completion_script
|
2022-09-19 20:49:36 +03:00
|
|
|
input_parsers["fish"] = shell_input_parser
|
|
|
|
output_serializers["fish"] = fish_output_serializer
|
|
|
|
}
|