fish.go 2.7 KB

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