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 | |
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
-rw-r--r-- | cmd2/ansi.py | 7 | ||||
-rw-r--r-- | cmd2/cmd2.py | 4 |
2 files changed, 5 insertions, 6 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 diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 77af3910..217a92c8 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -52,7 +52,7 @@ from .history import History, HistoryItem from .parsing import StatementParser, Statement, Macro, MacroArg, shlex_split # Set up readline -from .rl_utils import rl_type, RlType, rl_set_prompt, vt100_support, rl_make_safe_prompt +from .rl_utils import rl_type, RlType, rl_get_point, rl_set_prompt, vt100_support, rl_make_safe_prompt if rl_type == RlType.NONE: # pragma: no cover rl_warning = "Readline features including tab completion have been disabled since no \n" \ @@ -3801,7 +3801,7 @@ class Cmd(cmd.Cmd): if update_terminal: terminal_str = ansi.async_alert_str(prompt=current_prompt, line=readline.get_line_buffer(), - alert_msg=alert_msg) + cursor_offset=rl_get_point(), alert_msg=alert_msg) if rl_type == RlType.GNU: sys.stderr.write(terminal_str) elif rl_type == RlType.PYREADLINE: |