kitty.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package completion
  3. import (
  4. "fmt"
  5. "strings"
  6. kitty_constants "kitty"
  7. "kitty/tools/cli"
  8. "kitty/tools/themes"
  9. "kitty/tools/utils"
  10. )
  11. var _ = fmt.Print
  12. func complete_kitty_override(completions *cli.Completions, word string, arg_num int) {
  13. mg := completions.AddMatchGroup("Config directives")
  14. mg.NoTrailingSpace = true
  15. scanner := utils.NewLineScanner(kitty_constants.OptionNames)
  16. for scanner.Scan() {
  17. line := strings.TrimSpace(scanner.Text())
  18. if strings.HasPrefix(line, word) {
  19. mg.AddMatch(line + "=")
  20. }
  21. }
  22. }
  23. func complete_kitty_listen_on(completions *cli.Completions, word string, arg_num int) {
  24. if !strings.Contains(word, ":") {
  25. mg := completions.AddMatchGroup("Address family")
  26. mg.NoTrailingSpace = true
  27. for _, q := range []string{"unix:", "tcp:"} {
  28. if strings.HasPrefix(q, word) {
  29. mg.AddMatch(q)
  30. }
  31. }
  32. } else if strings.HasPrefix(word, "unix:") && !strings.HasPrefix(word, "unix:@") {
  33. cli.FnmatchCompleter("UNIX sockets", cli.CWD, "*")(completions, word[len("unix:"):], arg_num)
  34. completions.AddPrefixToAllMatches("unix:")
  35. }
  36. }
  37. func complete_plus_launch(completions *cli.Completions, word string, arg_num int) {
  38. if arg_num == 1 {
  39. cli.FnmatchCompleter("Python scripts", cli.CWD, "*.py")(completions, word, arg_num)
  40. if strings.HasPrefix(word, ":") {
  41. exes := cli.CompleteExecutablesInPath(word[1:])
  42. mg := completions.AddMatchGroup("Python scripts in PATH")
  43. for _, exe := range exes {
  44. mg.AddMatch(":" + exe)
  45. }
  46. }
  47. } else {
  48. cli.FnmatchCompleter("Files", cli.CWD, "*")(completions, word, arg_num)
  49. }
  50. }
  51. func complete_plus_runpy(completions *cli.Completions, word string, arg_num int) {
  52. if arg_num > 1 {
  53. cli.FnmatchCompleter("Files", cli.CWD, "*")(completions, word, arg_num)
  54. }
  55. }
  56. func complete_plus_open(completions *cli.Completions, word string, arg_num int) {
  57. cli.FnmatchCompleter("Files", cli.CWD, "*")(completions, word, arg_num)
  58. }
  59. func complete_themes(completions *cli.Completions, word string, arg_num int) {
  60. themes.CompleteThemes(completions, word, arg_num)
  61. }
  62. func EntryPoint(tool_root *cli.Command) {
  63. tool_root.AddSubCommand(&cli.Command{
  64. Name: "__complete__", Hidden: true,
  65. Usage: "output_type [shell state...]",
  66. ShortDescription: "Generate completions for kitty commands",
  67. HelpText: "Generate completion candidates for kitty commands. The command line is read from STDIN. output_type can be one of the supported shells: :code:`zsh`, :code:`fish`, :code:`bash`, or :code:`setup` for completion setup script following with the shell name, or :code:`json` for JSON output.",
  68. Run: func(cmd *cli.Command, args []string) (ret int, err error) {
  69. return ret, cli.GenerateCompletions(args)
  70. },
  71. })
  72. }