Cleanup up path completions
This commit is contained in:
parent
5ef8cce1d7
commit
d4fc0af7de
@ -220,21 +220,39 @@ def complete_remote_command(ans, cmd_name, words, new_word):
|
|||||||
complete_alias_map(ans, words, new_word, alias_map)
|
complete_alias_map(ans, words, new_word, alias_map)
|
||||||
|
|
||||||
|
|
||||||
def complete_files_and_dirs(ans, prefix, files_group_name='Files', predicate=lambda filename: True):
|
def path_completion(prefix=''):
|
||||||
dirs, files = [], []
|
dirs, files = [], []
|
||||||
base = '.'
|
base = '.'
|
||||||
if prefix.endswith('/'):
|
if prefix.endswith('/'):
|
||||||
base = prefix
|
base = prefix
|
||||||
elif '/' in prefix:
|
elif '/' in prefix:
|
||||||
base = os.path.dirname(prefix)
|
base = os.path.dirname(prefix)
|
||||||
for x in os.scandir(base):
|
src = os.path.expandvars(os.path.expanduser(base))
|
||||||
q = os.path.relpath(x.path)
|
src_prefix = os.path.abspath(os.path.expandvars(os.path.expanduser(prefix))) if prefix else ''
|
||||||
if x.is_dir():
|
try:
|
||||||
if q.startswith(prefix):
|
items = os.scandir(src)
|
||||||
dirs.append(q.rstrip(os.sep) + os.sep)
|
except FileNotFoundError:
|
||||||
|
items = ()
|
||||||
|
for x in items:
|
||||||
|
abspath = os.path.abspath(x.path)
|
||||||
|
if prefix and not abspath.startswith(src_prefix):
|
||||||
|
continue
|
||||||
|
if prefix:
|
||||||
|
q = prefix + abspath[len(src_prefix):].lstrip(os.sep)
|
||||||
|
q = os.path.expandvars(os.path.expanduser(q))
|
||||||
else:
|
else:
|
||||||
if q.startswith(prefix) and predicate(q):
|
q = os.path.relpath(abspath)
|
||||||
files.append(q)
|
if x.is_dir():
|
||||||
|
dirs.append(q.rstrip(os.sep) + os.sep)
|
||||||
|
else:
|
||||||
|
files.append(q)
|
||||||
|
return dirs, files
|
||||||
|
|
||||||
|
|
||||||
|
def complete_files_and_dirs(ans, prefix, files_group_name='Files', predicate=None):
|
||||||
|
dirs, files = path_completion(prefix or '')
|
||||||
|
files = filter(predicate, files)
|
||||||
|
|
||||||
if dirs:
|
if dirs:
|
||||||
ans.match_groups['Directories'] = dict.fromkeys(dirs)
|
ans.match_groups['Directories'] = dict.fromkeys(dirs)
|
||||||
ans.files_groups.add('Directories'), ans.no_space_groups.add('Directories')
|
ans.files_groups.add('Directories'), ans.no_space_groups.add('Directories')
|
||||||
@ -255,9 +273,15 @@ 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 config_file_predicate(filename):
|
||||||
|
return filename.endswith('.conf')
|
||||||
|
|
||||||
|
|
||||||
def complete_diff_args(ans, opt, prefix):
|
def complete_diff_args(ans, opt, prefix):
|
||||||
if opt is None:
|
if opt is None:
|
||||||
complete_files_and_dirs(ans, prefix, 'Files')
|
complete_files_and_dirs(ans, prefix, 'Files')
|
||||||
|
elif opt['dest'] == 'config':
|
||||||
|
complete_files_and_dirs(ans, prefix, 'Config Files', config_file_predicate)
|
||||||
|
|
||||||
|
|
||||||
def complete_kitten(ans, kitten, words, new_word):
|
def complete_kitten(ans, kitten, words, new_word):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user