summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-03-22 18:31:32 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-03-22 18:31:32 -0400
commitfb7171a36aae2191a8cf813279b9b8f2182e0756 (patch)
tree2bbf1f35b4275b77d166e747458c0e732f2e34f0 /cmd2.py
parent54073ddd76284bce13cc445f27894a18d25629e4 (diff)
downloadcmd2-git-fb7171a36aae2191a8cf813279b9b8f2182e0756.tar.gz
Fixed GNU readline issue where the prompt did not always redraw properly
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py67
1 files changed, 35 insertions, 32 deletions
diff --git a/cmd2.py b/cmd2.py
index e338d8f1..eadd5a60 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -629,19 +629,22 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
set_allow_appended_space(False)
set_allow_closing_quote(False)
- # We will display only the basename of paths in the tab-completion suggestions
- display_matches = [os.path.basename(cur_completion) for cur_completion in path_completions]
-
- # Extract just the completed text portion of the paths for completions
+ # Build the completion lists
completions = []
+ display_matches = []
+
for cur_completion in path_completions:
+ # Only keep where text started for the tab completion
+ completions.append(cur_completion[starting_index:])
+
+ # Display only the basename of this path in the tab-completion suggestions
+ display_matches.append(os.path.basename(cur_completion))
+
# Add a separator after directories if the next character isn't already a separator
if os.path.isdir(cur_completion) and add_trailing_sep_if_dir:
- cur_completion += os.path.sep
-
- # Only keep where text started
- completions.append(cur_completion[starting_index:])
+ completions[-1] += os.path.sep
+ display_matches[-1] += os.path.sep
# Remove cwd if it was added
if cwd_added:
@@ -650,7 +653,6 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
# Restore a tilde if we expanded one
if tilde_expanded:
completions = [cur_path.replace(user_path, '~', 1) for cur_path in completions]
- display_matches = [cur_path.replace(user_path, '~', 1) for cur_path in display_matches]
# Set the matches that will display as tab-completion suggestions
set_matches_to_display(display_matches)
@@ -1634,35 +1636,36 @@ class Cmd(cmd.Cmd):
:param matches: the tab completion matches to display
:param longest_match_length: length of the longest match
"""
- if matches_to_display is None:
- display_list = matches
- else:
- display_list = matches_to_display
+ if readline_lib is not None:
+ if matches_to_display is None:
+ display_list = matches
+ else:
+ display_list = matches_to_display
- # Eliminate duplicates and sort
- display_set = set(display_list)
- display_list = list(display_set)
- display_list.sort()
+ # Eliminate duplicates and sort
+ display_set = set(display_list)
+ display_list = list(display_set)
+ display_list.sort()
- # Print the matches
- self.poutput("\n")
+ # Print the matches
+ self.poutput("\n")
- if sys.version_info >= (3, 3):
- num_cols = shutil.get_terminal_size().columns
- else:
- proc = subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE)
- out, err = proc.communicate()
- if six.PY2:
- rows, columns = out.split()
+ if sys.version_info >= (3, 3):
+ num_cols = shutil.get_terminal_size().columns
else:
- rows, columns = out.decode().split()
- num_cols = int(columns)
+ proc = subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE)
+ out, err = proc.communicate()
+ if six.PY2:
+ rows, columns = out.split()
+ else:
+ rows, columns = out.decode().split()
+ num_cols = int(columns)
- self.columnize(display_list, num_cols)
+ self.columnize(display_list, num_cols)
- # Print the prompt
- self.poutput(self.prompt, readline.get_line_buffer())
- sys.stdout.flush()
+ # rl_forced_update_display() is the proper way to redraw the prompt and line, but we
+ # have to use ctypes to do it since Python's readline API does not wrap the function
+ readline_lib.rl_forced_update_display()
@staticmethod
def _display_matches_pyreadline(matches):