diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-30 18:14:48 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-30 18:14:48 -0400 |
commit | 8417cfed896aa21753b55dce8be6ab8848edc6b3 (patch) | |
tree | 0778c0d52d9842224cb33d411e39d195443d73b4 /cmd2/ansi.py | |
parent | 5074f2573b08f4f44ab3a41ac7a450c4844dcd3d (diff) | |
download | cmd2-git-8417cfed896aa21753b55dce8be6ab8848edc6b3.tar.gz |
Refactored ansi.async_alert_str() so that the cursor offset within the readline buffer is passed in
This way, the function is independent of readline and can more easily be unit tested
Diffstat (limited to 'cmd2/ansi.py')
-rw-r--r-- | cmd2/ansi.py | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/cmd2/ansi.py b/cmd2/ansi.py index 59dcb43c..c0d8017a 100644 --- a/cmd2/ansi.py +++ b/cmd2/ansi.py @@ -188,19 +188,18 @@ style_warning = functools.partial(style, fg='bright_yellow') style_error = functools.partial(style, fg='bright_red') -def async_alert_str(*, prompt: str, line: str, alert_msg: str) -> str: +def async_alert_str(*, prompt: str, line: str, cursor_offset: int, alert_msg: str) -> str: """Calculate the desired string, including ANSI escape codes, for displaying an asynchronous alert message. :param prompt: prompt that is displayed on the current line :param line: current contents of the Readline line buffer + :param cursor_offset: the offset of the current cursor position within line :param alert_msg: the message to display to the user :return: the correct string so that the alert message appears to the user to be printed above the current line. """ import shutil from colorama import Cursor - from .rl_utils import rl_get_point - # Get the size of the terminal terminal_size = shutil.get_terminal_size() @@ -223,7 +222,7 @@ def async_alert_str(*, prompt: str, line: str, alert_msg: str) -> str: num_input_terminal_lines = int(input_width / terminal_size.columns) + 1 # Get the cursor's offset from the beginning of the first input line - cursor_input_offset = last_prompt_line_width + rl_get_point() + cursor_input_offset = last_prompt_line_width + cursor_offset # Calculate what input line the cursor is on cursor_input_line = int(cursor_input_offset / terminal_size.columns) + 1 |