From 226488717a3dcc73df38d6c2687367a04a7330eb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 1 Feb 2020 12:33:11 +0530 Subject: [PATCH] Add completion for image filenames at command line Sadly could not get it to work with the shell --- kitty/cmds.py | 5 ++++- kitty/complete.py | 22 +++++++++++++++++++++- kitty/shell.py | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/kitty/cmds.py b/kitty/cmds.py index 3405dfb43..8a4c2adfa 100644 --- a/kitty/cmds.py +++ b/kitty/cmds.py @@ -53,6 +53,7 @@ def cmd( argspec='...', string_return_is_error=False, args_count=None, + args_completion=None ): if options_spec: @@ -86,6 +87,7 @@ def cmd( func.args_count = 0 if not argspec else args_count func.get_default = get_defaut_value func.payload_get = payload_get + func.args_completion = args_completion cmap[func.name] = func return func return w @@ -1279,7 +1281,8 @@ How the image should be displayed. The value of configured will use the configur ''' + '\n\n' + MATCH_WINDOW_OPTION, argspec='PATH_TO_PNG_IMAGE', - args_count=1 + args_count=1, + args_completion={'files': ('PNG Images', ('*.png',))} ) def cmd_set_background_image(global_opts, opts, args): ''' diff --git a/kitty/complete.py b/kitty/complete.py index 2c05b3c01..a8db1fcad 100644 --- a/kitty/complete.py +++ b/kitty/complete.py @@ -266,7 +266,11 @@ def complete_remote_command(ans, cmd_name, words, new_word): aliases, alias_map = options_for_cmd(cmd_name) if not alias_map: return - complete_alias_map(ans, words, new_word, alias_map) + args_completion = cmap[cmd_name].args_completion + args_completer = None + if 'files' in args_completion: + args_completer = remote_files_completer(args_completion['files']) + complete_alias_map(ans, words, new_word, alias_map, complete_args=args_completer) def path_completion(prefix=''): @@ -326,6 +330,22 @@ def complete_icat_args(ans, opt, prefix): complete_files_and_dirs(ans, prefix, 'Images', icat_file_predicate) +def remote_files_completer(spec): + name, matchers = spec + + def complete_files_map(ans, opt, prefix): + + def predicate(filename): + for m in matchers: + if isinstance(m, str): + from fnmatch import fnmatch + return fnmatch(filename, m) + + if opt is None: + complete_files_and_dirs(ans, prefix, name, predicate) + return complete_files_map + + def config_file_predicate(filename): return filename.endswith('.conf') diff --git a/kitty/shell.py b/kitty/shell.py index 569743b19..1a03f5a4e 100644 --- a/kitty/shell.py +++ b/kitty/shell.py @@ -58,7 +58,7 @@ def options_for_cmd(cmd): return tuple(sorted(ans)), alias_map -def options_matching(prefix, aliases, alias_map): +def options_matching(prefix, cmd, last_word, aliases, alias_map): for alias in aliases: if (not prefix or alias.startswith(prefix)) and alias.startswith('--'): yield alias + ' ' @@ -80,7 +80,7 @@ class Completer: if len(cmdline) < 2 and not line.endswith(' '): self.matches = list(cmd_names_matching(text)) else: - self.matches = list(options_matching(text, *options_for_cmd(cmdline[0]))) + self.matches = list(options_matching(text, cmdline[0], cmdline[-1], *options_for_cmd(cmdline[0]))) if state < len(self.matches): return self.matches[state]