Implement delegate based completion fof the ssh and hyperlinked_grep kittens
This commit is contained in:
parent
fd631bf402
commit
928a4db817
@ -59,10 +59,14 @@ def generate_completion_for_rc(name: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def generate_kittens_completion() -> None:
|
def generate_kittens_completion() -> None:
|
||||||
from kittens.runner import all_kitten_names, get_kitten_cli_docs
|
from kittens.runner import all_kitten_names, get_kitten_cli_docs, get_kitten_wrapper_of
|
||||||
for kitten in all_kitten_names():
|
for kitten in sorted(all_kitten_names()):
|
||||||
kn = 'kitten_' + kitten
|
kn = 'kitten_' + kitten
|
||||||
print(f'{kn} := plus_kitten.add_command("{kitten}", "Kittens")')
|
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)
|
kcd = get_kitten_cli_docs(kitten)
|
||||||
if kcd:
|
if kcd:
|
||||||
ospec = kcd['options']
|
ospec = kcd['options']
|
||||||
@ -127,7 +131,7 @@ def generate_completions_for_kitty() -> None:
|
|||||||
# kitten @
|
# kitten @
|
||||||
print('at := k.add_command("@", "Remote control")')
|
print('at := k.add_command("@", "Remote control")')
|
||||||
print('at.Description = "Control kitty using commands"')
|
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('_', '-')
|
name = go_name.replace('_', '-')
|
||||||
print(f'{go_name} := at.add_command("{name}", "")')
|
print(f'{go_name} := at.add_command("{name}", "")')
|
||||||
generate_completion_for_rc(go_name)
|
generate_completion_for_rc(go_name)
|
||||||
|
|||||||
@ -112,3 +112,6 @@ def main() -> None:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
elif __name__ == '__wrapper_of__':
|
||||||
|
cd = sys.cli_docs # type: ignore
|
||||||
|
cd['wrapper_of'] = 'rg'
|
||||||
|
|||||||
@ -155,6 +155,14 @@ def get_kitten_cli_docs(kitten: str) -> Any:
|
|||||||
return ans
|
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:
|
def get_kitten_completer(kitten: str) -> Any:
|
||||||
run_kitten(kitten, run_name='__completer__')
|
run_kitten(kitten, run_name='__completer__')
|
||||||
ans = getattr(sys, 'kitten_completer', None)
|
ans = getattr(sys, 'kitten_completer', None)
|
||||||
|
|||||||
@ -754,9 +754,9 @@ def main(args: List[str]) -> None:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main(sys.argv)
|
main(sys.argv)
|
||||||
elif __name__ == '__completer__':
|
elif __name__ == '__wrapper_of__':
|
||||||
from .completion import complete
|
cd = sys.cli_docs # type: ignore
|
||||||
setattr(sys, 'kitten_completer', complete)
|
cd['wrapper_of'] = 'ssh'
|
||||||
elif __name__ == '__conf__':
|
elif __name__ == '__conf__':
|
||||||
from .options.definition import definition
|
from .options.definition import definition
|
||||||
sys.options_definition = definition # type: ignore
|
sys.options_definition = definition # type: ignore
|
||||||
|
|||||||
@ -156,6 +156,7 @@ def completion(self: TestCompletion, tdir: str):
|
|||||||
add('kitty + kitten diff ', has_words('exe-not2.jpeg'))
|
add('kitty + kitten diff ', has_words('exe-not2.jpeg'))
|
||||||
add('kitty + kitten themes --', has_words('--cache-age'))
|
add('kitty + kitten themes --', has_words('--cache-age'))
|
||||||
add('kitty + kitten themes D', has_words('Default'))
|
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'))
|
add('clone-in-kitty --ty', has_words('--type'))
|
||||||
make_file('editable.txt')
|
make_file('editable.txt')
|
||||||
|
|||||||
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -183,6 +183,10 @@ func default_parse_args(cmd *Command, words []string, completions *Completions)
|
|||||||
arg_num = 0
|
arg_num = 0
|
||||||
completions.current_word_idx_in_parent = 0
|
completions.current_word_idx_in_parent = 0
|
||||||
only_args_allowed = false
|
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 {
|
} else if cmd.Stop_processing_at_arg > 0 && arg_num >= cmd.Stop_processing_at_arg {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user