Add completion for image filenames at command line

Sadly could not get it to work with the shell
This commit is contained in:
Kovid Goyal 2020-02-01 12:33:11 +05:30
parent 2f9dabd344
commit 226488717a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 27 additions and 4 deletions

View File

@ -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):
'''

View File

@ -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')

View File

@ -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]