From c2a2b4c087f34504abed51af4d10cdf10492b3ca Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 16 Sep 2022 21:32:10 +0530 Subject: [PATCH] Add various special purpose entry points --- gen-go-code.py | 16 ++++++++++++++++ kitty_tests/completion.py | 4 +++- tools/completion/kitty.go | 25 +++++++++++++++++++++++++ tools/completion/types.go | 16 +++++++++++++--- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/gen-go-code.py b/gen-go-code.py index 66b03a4a6..ce753c3e5 100755 --- a/gen-go-code.py +++ b/gen-go-code.py @@ -66,6 +66,22 @@ def generate_completions_for_kitty() -> None: print('k.Subcommand_must_be_first = true') for opt in go_options_for_seq(parse_option_spec()[0]): print(opt.as_completion_option('k')) + print('plus := k.add_command("+", "Entry point")') + print('plus.Description = "Various special purpose tools and kittens"') + + print('plus_launch := plus.add_command("launch", "Launch Python script")') + print('plus_launch.Completion_for_arg = complete_plus_launch') + print('k.add_clone("+launch", "Launch Python scripts", plus_launch)') + + print('plus_runpy := plus.add_command("runpy", "Run python code")') + print('plus_runpy.Completion_for_arg = complete_plus_runpy') + print('k.add_clone("+runpy", "Run Python code", plus_runpy)') + + print('plus_open := plus.add_command("open", "Open files and URLs")') + print('plus_open.Completion_for_arg = complete_plus_open') + print('plus_open.clone_options_from(k)') + print('k.add_clone("+open", "Open files and URLs", plus_open)') + print('at := k.add_command("@", "Remote control")') print('at.Description = "Control kitty using commands"') for go_name in all_command_names(): diff --git a/kitty_tests/completion.py b/kitty_tests/completion.py index d99ae557c..da720f535 100644 --- a/kitty_tests/completion.py +++ b/kitty_tests/completion.py @@ -79,7 +79,7 @@ def completion(self: TestCompletion, tdir: str): self.assertEqual(cp.returncode, 0, f'kitty-tool __complete__ failed with exit code: {cp.returncode}') return json.loads(cp.stdout) - add('kitty ', has_words('@', '@ls')) + add('kitty ', has_words('@', '@ls', '+', '+open')) add('kitty @ l', has_words('ls', 'last-used-layout', 'launch')) add('kitty @l', has_words('@ls', '@last-used-layout', '@launch')) @@ -112,6 +112,8 @@ def completion(self: TestCompletion, tdir: str): add('kitty @ set-window-logo ', all_words('exe-not2.jpeg', 'sub/')) add('kitty @ set-window-logo e', all_words('exe-not2.jpeg')) add('kitty @ set-window-logo e e', all_words()) + add('kitty +ope', has_words('+open')) + add('kitty +open -', has_words('-1', '-T')) add('kitty -', has_words('-c', '-1', '--'), does_not_have_words('--config', '--single-instance')) add('kitty -c', all_words('-c')) diff --git a/tools/completion/kitty.go b/tools/completion/kitty.go index 8a01f2274..c48d5afc5 100644 --- a/tools/completion/kitty.go +++ b/tools/completion/kitty.go @@ -47,3 +47,28 @@ func complete_kitty(completions *Completions, word string, arg_num int) { }, "") } } + +func complete_plus_launch(completions *Completions, word string, arg_num int) { + if arg_num == 1 { + fnmatch_completer("Python scripts", CWD, "*.py")(completions, word, arg_num) + if strings.HasPrefix(word, ":") { + exes := complete_executables_in_path(word[1:]) + mg := completions.add_match_group("Python scripts in PATH") + for _, exe := range exes { + mg.add_match(":" + exe) + } + } + } else { + fnmatch_completer("Files", CWD, "*")(completions, word, arg_num) + } +} + +func complete_plus_runpy(completions *Completions, word string, arg_num int) { + if arg_num > 1 { + fnmatch_completer("Files", CWD, "*")(completions, word, arg_num) + } +} + +func complete_plus_open(completions *Completions, word string, arg_num int) { + fnmatch_completer("Files", CWD, "*")(completions, word, arg_num) +} diff --git a/tools/completion/types.go b/tools/completion/types.go index 8aca3807c..ab755a25b 100644 --- a/tools/completion/types.go +++ b/tools/completion/types.go @@ -22,6 +22,12 @@ func (self *MatchGroup) add_match(word string, description ...string) *Match { return &ans } +func (self *MatchGroup) add_prefix_to_all_matches(prefix string) { + for _, m := range self.Matches { + m.Word = prefix + m.Word + } +} + type Completions struct { Groups []*MatchGroup `json:"groups,omitempty"` @@ -32,9 +38,7 @@ type Completions struct { func (self *Completions) add_prefix_to_all_matches(prefix string) { for _, mg := range self.Groups { - for _, m := range mg.Matches { - m.Word = prefix + m.Word - } + mg.add_prefix_to_all_matches(prefix) } } @@ -77,6 +81,12 @@ type Command struct { Subcommand_must_be_first bool } +func (self *Command) clone_options_from(other *Command) { + for _, opt := range other.Options { + self.Options = append(self.Options, opt) + } +} + func (self *Command) add_group(name string) *CommandGroup { for _, g := range self.Groups { if g.Title == name {