Skip to content

Commit d41fd7c

Browse files
committed
Fixed how we complete ~
1 parent 4f63fdf commit d41fd7c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

cmd2.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
323323
if endidx == len(line) or (endidx < len(line) and line[endidx] != os.path.sep):
324324
add_trailing_sep_if_dir = True
325325

326-
add_sep_after_tilde = False
327-
328326
# Readline places begidx after ~ and path separators (/) so we need to extract any directory
329327
# path that appears before the search text
330328
dirname = line[prev_space_index + 1:begidx]
@@ -342,8 +340,12 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
342340
if not dirname:
343341
dirname = os.getcwd()
344342
elif dirname == '~':
345-
# If tilde was used without separator, add a separator after the tilde in the completions
346-
add_sep_after_tilde = True
343+
# If a ~ was used without a separator between text, then this is invalid
344+
if text:
345+
return []
346+
# If only a ~ was entered, then complete it with a slash
347+
else:
348+
return [os.path.sep]
347349

348350
# Build the search string
349351
search_str = os.path.join(dirname, text + '*')
@@ -376,9 +378,6 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
376378
# If it is a file and we are at the end of the line, then add a space
377379
if os.path.isfile(path_completions[0]) and endidx == len(line):
378380
completions[0] += ' '
379-
# If tilde was expanded without a separator, prepend one
380-
elif os.path.isdir(path_completions[0]) and add_sep_after_tilde:
381-
completions[0] = os.path.sep + completions[0]
382381

383382
completions.sort()
384383
return completions

tests/test_completion.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,29 @@ def test_path_completion_doesnt_match_wildcards(request):
382382
# Currently path completion doesn't accept wildcards, so will always return empty results
383383
assert path_complete(text, line, begidx, endidx) == []
384384

385-
def test_path_completion_user_expansion():
385+
def test_path_completion_just_tilde():
386386
# Run path with just a tilde
387387
text = ''
388+
line = 'shell fake ~'
389+
endidx = len(line)
390+
begidx = endidx - len(text)
391+
completions_tilde = path_complete(text, line, begidx, endidx)
392+
393+
# Path complete should return a slash
394+
assert completions_tilde == [os.path.sep]
395+
396+
def test_path_completion_user_expansion():
397+
# Run path with a tilde and a slash
398+
text = ''
388399
if sys.platform.startswith('win'):
389-
line = 'shell dir ~{}'.format(text)
400+
cmd = 'dir'
390401
else:
391-
line = 'shell ls ~{}'.format(text)
402+
cmd = 'ls'
403+
404+
line = 'shell {} ~{}'.format(cmd, os.path.sep)
392405
endidx = len(line)
393406
begidx = endidx - len(text)
394-
completions_tilde = path_complete(text, line, begidx, endidx)
407+
completions_tilde_slash = path_complete(text, line, begidx, endidx)
395408

396409
# Run path complete on the user's home directory
397410
user_dir = os.path.expanduser('~')
@@ -404,7 +417,7 @@ def test_path_completion_user_expansion():
404417
completions_home = path_complete(text, line, begidx, endidx)
405418

406419
# Verify that the results are the same in both cases
407-
assert completions_tilde == completions_home
420+
assert completions_tilde_slash == completions_home
408421

409422
def test_path_completion_directories_only(request):
410423
test_dir = os.path.dirname(request.module.__file__)

0 commit comments

Comments
 (0)