fish.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package cli
  3. import (
  4. "fmt"
  5. "strings"
  6. "kitty/tools/cli/markup"
  7. "kitty/tools/utils"
  8. "golang.org/x/exp/maps"
  9. )
  10. var _ = fmt.Print
  11. func fish_completion_script(commands []string) (string, error) {
  12. // One command in fish requires one completion script.
  13. // Usage: kitten __complete__ setup fish [kitty|kitten|clone-in-kitty]
  14. all_commands := map[string]bool{
  15. "kitty": true,
  16. "clone-in-kitty": true,
  17. "kitten": true,
  18. }
  19. if len(commands) == 0 {
  20. commands = append(commands, maps.Keys(all_commands)...)
  21. }
  22. script := strings.Builder{}
  23. script.WriteString(`function __ksi_completions
  24. set --local ct (commandline --current-token)
  25. set --local tokens (commandline --tokenize --cut-at-cursor --current-process)
  26. printf "%s\n" $tokens $ct | command kitten __complete__ fish | source -
  27. end
  28. `)
  29. for _, cmd := range commands {
  30. if all_commands[cmd] {
  31. fmt.Fprintf(&script, "complete -f -c %s -a \"(__ksi_completions)\"\n", cmd)
  32. } else if strings.Contains(cmd, "=") {
  33. // Reserved for `setup SHELL [KEY=VALUE ...]`, not used now.
  34. continue
  35. } else {
  36. return "", fmt.Errorf("No fish completion script for command: %s", cmd)
  37. }
  38. }
  39. return script.String(), nil
  40. }
  41. func fish_output_serializer(completions []*Completions, shell_state map[string]string) ([]byte, error) {
  42. output := strings.Builder{}
  43. f := func(format string, args ...any) { fmt.Fprintf(&output, format+"\n", args...) }
  44. n := completions[0].Delegate.NumToRemove
  45. fm := markup.New(false) // fish freaks out if there are escape codes in the description strings
  46. legacy_completion := shell_state["_legacy_completion"]
  47. if legacy_completion == "fish2" {
  48. for _, mg := range completions[0].Groups {
  49. for _, m := range mg.Matches {
  50. f("%s", strings.ReplaceAll(m.Word+"\t"+fm.Prettify(m.Description), "\n", " "))
  51. }
  52. }
  53. } else if n > 0 {
  54. words := make([]string, len(completions[0].AllWords)-n+1)
  55. words[0] = completions[0].Delegate.Command
  56. copy(words[1:], completions[0].AllWords[n:])
  57. for i, w := range words {
  58. words[i] = fmt.Sprintf("(string escape -- %s)", utils.QuoteStringForFish(w))
  59. }
  60. cmdline := strings.Join(words, " ")
  61. f("set __ksi_cmdline " + cmdline)
  62. f("complete -C \"$__ksi_cmdline\"")
  63. f("set --erase __ksi_cmdline")
  64. } else {
  65. for _, mg := range completions[0].Groups {
  66. for _, m := range mg.Matches {
  67. f("echo -- %s", utils.QuoteStringForFish(m.Word+"\t"+fm.Prettify(m.Description)))
  68. }
  69. }
  70. }
  71. // debugf("%#v", output.String())
  72. return []byte(output.String()), nil
  73. }
  74. func init() {
  75. completion_scripts["fish"] = fish_completion_script
  76. input_parsers["fish"] = shell_input_parser
  77. output_serializers["fish"] = fish_output_serializer
  78. }