Implement delegate based completion fof the ssh and hyperlinked_grep kittens

This commit is contained in:
Kovid Goyal 2022-09-19 13:31:32 +05:30
parent fd631bf402
commit 928a4db817
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
7 changed files with 33 additions and 6 deletions

View File

@ -59,10 +59,14 @@ def generate_completion_for_rc(name: str) -> None:
def generate_kittens_completion() -> None:
from kittens.runner import all_kitten_names, get_kitten_cli_docs
for kitten in all_kitten_names():
from kittens.runner import all_kitten_names, get_kitten_cli_docs, get_kitten_wrapper_of
for kitten in sorted(all_kitten_names()):
kn = 'kitten_' + kitten
print(f'{kn} := plus_kitten.add_command("{kitten}", "Kittens")')
wof = get_kitten_wrapper_of(kitten)
if wof:
print(f'{kn}.Parse_args = completion_for_wrapper("{serialize_as_go_string(wof)}")')
continue
kcd = get_kitten_cli_docs(kitten)
if kcd:
ospec = kcd['options']
@ -127,7 +131,7 @@ def generate_completions_for_kitty() -> None:
# kitten @
print('at := k.add_command("@", "Remote control")')
print('at.Description = "Control kitty using commands"')
for go_name in all_command_names():
for go_name in sorted(all_command_names()):
name = go_name.replace('_', '-')
print(f'{go_name} := at.add_command("{name}", "")')
generate_completion_for_rc(go_name)

View File

@ -112,3 +112,6 @@ def main() -> None:
if __name__ == '__main__':
main()
elif __name__ == '__wrapper_of__':
cd = sys.cli_docs # type: ignore
cd['wrapper_of'] = 'rg'

View File

@ -155,6 +155,14 @@ def get_kitten_cli_docs(kitten: str) -> Any:
return ans
def get_kitten_wrapper_of(kitten: str) -> str:
setattr(sys, 'cli_docs', {})
run_kitten(kitten, run_name='__wrapper_of__')
ans = getattr(sys, 'cli_docs')
delattr(sys, 'cli_docs')
return ans.get('wrapper_of') or ''
def get_kitten_completer(kitten: str) -> Any:
run_kitten(kitten, run_name='__completer__')
ans = getattr(sys, 'kitten_completer', None)

View File

@ -754,9 +754,9 @@ def main(args: List[str]) -> None:
if __name__ == '__main__':
main(sys.argv)
elif __name__ == '__completer__':
from .completion import complete
setattr(sys, 'kitten_completer', complete)
elif __name__ == '__wrapper_of__':
cd = sys.cli_docs # type: ignore
cd['wrapper_of'] = 'ssh'
elif __name__ == '__conf__':
from .options.definition import definition
sys.options_definition = definition # type: ignore

View File

@ -156,6 +156,7 @@ def completion(self: TestCompletion, tdir: str):
add('kitty + kitten diff ', has_words('exe-not2.jpeg'))
add('kitty + kitten themes --', has_words('--cache-age'))
add('kitty + kitten themes D', has_words('Default'))
add('kitty + kitten hyperlinked_grep ', is_delegate(3, 'rg'))
add('clone-in-kitty --ty', has_words('--type'))
make_file('editable.txt')

View File

@ -122,3 +122,10 @@ func complete_themes(completions *Completions, word string, arg_num int) {
}
}
}
func completion_for_wrapper(wrapped_cmd string) func(*Command, []string, *Completions) {
return func(cmd *Command, args []string, completions *Completions) {
completions.Delegate.NumToRemove = completions.current_word_idx + 1
completions.Delegate.Command = wrapped_cmd
}
}

View File

@ -183,6 +183,10 @@ func default_parse_args(cmd *Command, words []string, completions *Completions)
arg_num = 0
completions.current_word_idx_in_parent = 0
only_args_allowed = false
if cmd.Parse_args != nil {
cmd.Parse_args(cmd, words[i+1:], completions)
return
}
} else if cmd.Stop_processing_at_arg > 0 && arg_num >= cmd.Stop_processing_at_arg {
return
} else {