Add completion for image filenames at command line
Sadly could not get it to work with the shell
This commit is contained in:
@@ -53,6 +53,7 @@ def cmd(
|
|||||||
argspec='...',
|
argspec='...',
|
||||||
string_return_is_error=False,
|
string_return_is_error=False,
|
||||||
args_count=None,
|
args_count=None,
|
||||||
|
args_completion=None
|
||||||
):
|
):
|
||||||
|
|
||||||
if options_spec:
|
if options_spec:
|
||||||
@@ -86,6 +87,7 @@ def cmd(
|
|||||||
func.args_count = 0 if not argspec else args_count
|
func.args_count = 0 if not argspec else args_count
|
||||||
func.get_default = get_defaut_value
|
func.get_default = get_defaut_value
|
||||||
func.payload_get = payload_get
|
func.payload_get = payload_get
|
||||||
|
func.args_completion = args_completion
|
||||||
cmap[func.name] = func
|
cmap[func.name] = func
|
||||||
return func
|
return func
|
||||||
return w
|
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,
|
''' + '\n\n' + MATCH_WINDOW_OPTION,
|
||||||
argspec='PATH_TO_PNG_IMAGE',
|
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):
|
def cmd_set_background_image(global_opts, opts, args):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@@ -266,7 +266,11 @@ def complete_remote_command(ans, cmd_name, words, new_word):
|
|||||||
aliases, alias_map = options_for_cmd(cmd_name)
|
aliases, alias_map = options_for_cmd(cmd_name)
|
||||||
if not alias_map:
|
if not alias_map:
|
||||||
return
|
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=''):
|
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)
|
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):
|
def config_file_predicate(filename):
|
||||||
return filename.endswith('.conf')
|
return filename.endswith('.conf')
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ def options_for_cmd(cmd):
|
|||||||
return tuple(sorted(ans)), alias_map
|
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:
|
for alias in aliases:
|
||||||
if (not prefix or alias.startswith(prefix)) and alias.startswith('--'):
|
if (not prefix or alias.startswith(prefix)) and alias.startswith('--'):
|
||||||
yield alias + ' '
|
yield alias + ' '
|
||||||
@@ -80,7 +80,7 @@ class Completer:
|
|||||||
if len(cmdline) < 2 and not line.endswith(' '):
|
if len(cmdline) < 2 and not line.endswith(' '):
|
||||||
self.matches = list(cmd_names_matching(text))
|
self.matches = list(cmd_names_matching(text))
|
||||||
else:
|
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):
|
if state < len(self.matches):
|
||||||
return self.matches[state]
|
return self.matches[state]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user