2022-09-19 12:42:28 +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 12:42:28 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"kitty/tools/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = fmt.Print
|
|
|
|
|
2023-02-03 13:44:24 +03:00
|
|
|
func bash_completion_script(commands []string) (string, error) {
|
|
|
|
return `_ksi_completions() {
|
2023-02-03 13:16:04 +03:00
|
|
|
builtin local src
|
|
|
|
builtin local limit
|
|
|
|
# Send all words up to the word the cursor is currently on
|
|
|
|
builtin let limit=1+$COMP_CWORD
|
|
|
|
src=$(builtin printf "%s\n" "${COMP_WORDS[@]:0:$limit}" | builtin command kitten __complete__ bash)
|
|
|
|
if [[ $? == 0 ]]; then
|
|
|
|
builtin eval "${src}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin complete -F _ksi_completions kitty
|
|
|
|
builtin complete -F _ksi_completions edit-in-kitty
|
|
|
|
builtin complete -F _ksi_completions clone-in-kitty
|
|
|
|
builtin complete -F _ksi_completions kitten
|
2023-02-03 13:44:24 +03:00
|
|
|
`, nil
|
2023-02-03 13:16:04 +03:00
|
|
|
}
|
|
|
|
|
2022-09-19 12:42:28 +03:00
|
|
|
func bash_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 16:37:46 +03:00
|
|
|
n := completions[0].Delegate.NumToRemove
|
|
|
|
if n > 0 {
|
2022-09-19 17:06:36 +03:00
|
|
|
f("compopt +o nospace")
|
|
|
|
f("COMP_WORDS[%d]=%s", n, utils.QuoteStringForSH(completions[0].Delegate.Command))
|
|
|
|
f("_command_offset %d", n)
|
2022-09-19 16:37:46 +03:00
|
|
|
} else {
|
|
|
|
for _, mg := range completions[0].Groups {
|
|
|
|
mg.remove_common_prefix()
|
2022-09-19 17:06:36 +03:00
|
|
|
if mg.NoTrailingSpace {
|
|
|
|
f("compopt -o nospace")
|
|
|
|
} else {
|
|
|
|
f("compopt +o nospace")
|
|
|
|
}
|
|
|
|
if mg.IsFiles {
|
|
|
|
f("compopt -o filenames")
|
|
|
|
for _, m := range mg.Matches {
|
|
|
|
if strings.HasSuffix(m.Word, utils.Sep) {
|
|
|
|
m.Word = m.Word[:len(m.Word)-1]
|
|
|
|
}
|
2022-09-19 16:37:46 +03:00
|
|
|
}
|
2022-09-19 17:06:36 +03:00
|
|
|
} else {
|
|
|
|
f("compopt +o filenames")
|
|
|
|
}
|
|
|
|
for _, m := range mg.Matches {
|
|
|
|
f("COMPREPLY+=(%s)", utils.QuoteStringForSH(m.Word))
|
2022-09-19 15:17:37 +03:00
|
|
|
}
|
2022-09-19 12:42:28 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-19 16:37:46 +03:00
|
|
|
// debugf("%#v", output.String())
|
2022-09-19 12:42:28 +03:00
|
|
|
return []byte(output.String()), nil
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:07:25 +03:00
|
|
|
func bash_init_completions(completions *Completions) {
|
|
|
|
completions.split_on_equals = true
|
|
|
|
}
|
|
|
|
|
2022-09-19 12:42:28 +03:00
|
|
|
func init() {
|
2023-02-03 13:16:04 +03:00
|
|
|
completion_scripts["bash"] = bash_completion_script
|
2022-09-19 12:42:28 +03:00
|
|
|
input_parsers["bash"] = shell_input_parser
|
|
|
|
output_serializers["bash"] = bash_output_serializer
|
2022-09-19 15:07:25 +03:00
|
|
|
init_completions["bash"] = bash_init_completions
|
2022-09-19 12:42:28 +03:00
|
|
|
}
|