Skip to content

Commit 2f4b5dd

Browse files
committed
Remember the toolbar's rows until it has been drawn for the command
An unfinished line is continued only while it and the toolbar fit on the screen together, and the display reported the toolbar's rows from the frame it last drew. Each run of the application forgets what it drew, and prompt-toolkit leaves the toolbar out until the terminal has reported where its cursor is. A command which writes at once does so before that report arrives, so the toolbar counted for no rows and a toolbar of two rows was allowed for as one: a line one row too tall for it was continued, and its first row was shown twice. Windows knows the height of its console without asking, so this showed only on terminals which answer a cursor position request. Keep the rows from the last frame which drew the toolbar, starting with the main prompt's, which are the rows it is about to take again.
1 parent 939ce21 commit 2f4b5dd

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎cmd2/command_toolbar.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ def __init__(self, cmd: "Cmd") -> None:
302302
):
303303
raise RuntimeError("Cannot locate PromptSession bottom toolbar")
304304
self._toolbar_window = self.toolbar.content
305+
# The rows the toolbar took when it was last drawn, to begin with by the main prompt.
306+
# Each run of the application forgets what it drew, and prompt-toolkit leaves the
307+
# toolbar out until the terminal has reported where its cursor is, which can be after
308+
# a command's first output. The toolbar is about to take these rows again.
309+
self._toolbar_height = 0
310+
self._note_toolbar_height()
305311
self._layout = Layout(HSplit([Window(height=0), Window(), self.toolbar]))
306312
self._display_stack: contextlib.ExitStack | None = None
307313
bindings = KeyBindings()
@@ -349,12 +355,18 @@ def _suspend_to_background(suspend_group: bool = True) -> None:
349355
run_in_terminal(functools.partial(suspend_process_group, suspend_group))
350356

351357
def _after_render(self, app: Application[str]) -> None: # noqa: ARG002
358+
self._note_toolbar_height()
352359
self._ready.set()
353360

361+
def _note_toolbar_height(self) -> None:
362+
"""Remember how many rows the toolbar took, if the last frame drew it."""
363+
info = self._toolbar_window.render_info
364+
if info is not None:
365+
self._toolbar_height = info.window_height
366+
354367
def _toolbar_rows(self) -> int:
355368
"""Return how many rows the toolbar took when it was last drawn."""
356-
info = self._toolbar_window.render_info
357-
return info.window_height if info is not None else 0
369+
return self._toolbar_height
358370

359371
def start(self) -> None:
360372
"""Start rendering and protect terminal output."""

‎tests/test_command_toolbar.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,33 @@ def test_command_toolbar_flushes_writes_waiting_on_cursor_reports() -> None:
568568
assert "last words\n" in output.getvalue()
569569

570570

571+
def test_command_toolbar_remembers_the_rows_its_toolbar_took_before_it_is_drawn_again() -> None:
572+
"""A command's first output can arrive before the toolbar has been drawn for that command.
573+
574+
Each run of the application forgets what it drew, and prompt-toolkit leaves the toolbar
575+
out until the terminal has reported where its cursor is. Until then the toolbar counts
576+
for the rows the main prompt drew it with, which are the rows it is about to take again.
577+
"""
578+
app = Cmd(allow_cli_args=False)
579+
output = Terminal()
580+
app.stdout = output
581+
582+
with create_pipe_input() as pipe:
583+
app.main_session = PromptSession(
584+
input=pipe,
585+
output=CprOutput(output),
586+
bottom_toolbar="one\ntwo",
587+
refresh_interval=0.01,
588+
)
589+
window = app.main_session.layout.container.children[-1].content
590+
# What the main prompt leaves behind after drawing a toolbar of two rows.
591+
window.render_info = SimpleNamespace(window_height=2)
592+
with app._command_toolbar_context():
593+
assert window.render_info is None
594+
assert app._command_toolbar._toolbar_rows() == 2
595+
assert app._command_toolbar._proxy._toolbar_rows() == 2
596+
597+
571598
def test_command_toolbar_suspension_waits_for_in_flight_writes(toolbar_app) -> None:
572599
app, _, output = toolbar_app
573600
writing = threading.Event()

0 commit comments

Comments
 (0)