Skip to content

Commit 68f2910

Browse files
committed
Fix UI flicker issue by updating prompt before alert message.
While testing the new background alerter thread, I noticed a slight UI flicker issue could occur when both msg and prompt are provided in an alert: - prompt_toolkit's patch_stdout() intercepts the printed message and immediately forces a redraw of the prompt so the message displays cleanly above it. - In the branch's implementation, self.prompt was being updated after printing the message. This caused patch_stdout to redraw the old prompt, and then get_app().invalidate() would immediately redraw the new prompt, causing a flicker. I've updated cmd2/cmd2.py so that self.prompt is updated before the message is printed. This allows patch_stdout to natively draw the new prompt immediately, eliminating the flicker:
1 parent dd68571 commit 68f2910

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

cmd2/cmd2.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,20 +3367,22 @@ def _process_alerts(self) -> None:
33673367
# Get the next alert while still holding the condition lock.
33683368
alert = self._alert_queue.get()
33693369

3370-
if alert.msg:
3371-
# Print the message above the current prompt.
3372-
with patch_stdout():
3373-
print_formatted_text(pt_filter_style(alert.msg))
3374-
33753370
# Only apply prompt changes generated after the current prompt started.
3371+
prompt_updated = False
33763372
if (alert.prompt is not None and
33773373
alert.prompt != self.prompt and
33783374
alert.timestamp > self._alert_prompt_timestamp): # fmt: skip
33793375
self.prompt = alert.prompt
3376+
prompt_updated = True
3377+
3378+
if alert.msg:
3379+
# Print the message above the current prompt.
3380+
with patch_stdout():
3381+
print_formatted_text(pt_filter_style(alert.msg))
33803382

3381-
# Refresh UI immediately unless at a continuation prompt.
3382-
if not self._at_continuation_prompt:
3383-
get_app().invalidate()
3383+
# Refresh UI immediately unless at a continuation prompt.
3384+
if prompt_updated and not self._at_continuation_prompt:
3385+
get_app().invalidate()
33843386

33853387
def _read_command_line(self, prompt: str) -> str:
33863388
"""Read the next command line from the input stream.

examples/async_printing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def _generate_colored_prompt(self) -> str:
129129
"""Randomly generates a colored prompt
130130
:return: the new prompt.
131131
"""
132-
rand_num = random.randint(1, 20)
132+
rand_num = random.randint(1, 6)
133133

134134
status_color = Color.DEFAULT
135135

0 commit comments

Comments
 (0)