@@ -385,6 +385,16 @@ def cursor_up(self, amount: int) -> None:
385385 "z" * (24 * 80 + 1 ) + "\r \n END\n " ,
386386 id = "a line longer than the screen stays ended" ,
387387 ),
388+ pytest .param (
389+ ["z" * (23 * 80 + 1 ), "END\n " ],
390+ "z" * (23 * 80 + 1 ) + "\r \n END\n " ,
391+ id = "a line which reaches the last row stays ended" ,
392+ ),
393+ pytest .param (
394+ ["z" * (23 * 80 ), "END\n " ],
395+ "z" * (23 * 80 ) + "\r \n <up 23>" + "z" * (23 * 80 ) + "END\n " ,
396+ id = "a line which stops above the last row is continued" ,
397+ ),
388398 ],
389399)
390400def test_stdout_proxy_continues_unfinished_lines (writes , expected ) -> None :
@@ -402,6 +412,115 @@ def test_stdout_proxy_continues_unfinished_lines(writes, expected) -> None:
402412 assert stream .getvalue () == expected
403413
404414
415+ @pytest .mark .parametrize (
416+ ("width" , "expected" ),
417+ [
418+ pytest .param (22 * 80 , "\r \n <up 22>" + "z" * (22 * 80 ) + "END\n " , id = "continued while the toolbar fits below it" ),
419+ pytest .param (22 * 80 + 1 , "\r \n END\n " , id = "stays ended once the toolbar would scroll its first row off" ),
420+ ],
421+ )
422+ def test_stdout_proxy_leaves_room_for_every_row_of_the_toolbar (width , expected ) -> None :
423+ """A toolbar of several rows scrolls the screen sooner than one row does."""
424+ stream = Terminal ()
425+ with create_pipe_input () as pipe , create_app_session (input = pipe , output = CursorOutput (stream )):
426+ proxy = command_toolbar ._ContextStdoutProxy (lambda : 2 )
427+ try :
428+ proxy ._write_and_flush (None , "z" * width )
429+ proxy ._write_and_flush (None , "END\n " )
430+ finally :
431+ proxy .close ()
432+
433+ assert stream .getvalue () == "z" * width + expected
434+
435+
436+ def test_command_toolbar_tells_the_proxy_how_many_rows_it_takes (toolbar_app ) -> None :
437+ app , _ , _ = toolbar_app
438+ app .main_session .bottom_toolbar = "one\n two"
439+ # Nothing has been drawn yet.
440+ assert command_toolbar .CommandToolbar (app )._toolbar_rows () == 0
441+ with app ._command_toolbar_context ():
442+ assert app ._command_toolbar ._toolbar_rows () == 2
443+ assert app ._command_toolbar ._proxy ._toolbar_rows () == 2
444+
445+
446+ class LegacyConsoleOutput (CursorOutput ):
447+ """Report a size the way prompt-toolkit's Windows outputs do, one column short of the console's.
448+
449+ Like prompt-toolkit's Win32Output, which a legacy console gets, it has no vt100 output.
450+ """
451+
452+ def __init__ (self , stream : Terminal ) -> None :
453+ super ().__init__ (stream )
454+ self .size = Size (rows = 24 , columns = 79 )
455+
456+ def get_win32_screen_buffer_info (self ) -> SimpleNamespace :
457+ return SimpleNamespace (dwSize = SimpleNamespace (X = 80 , Y = 24 ))
458+
459+
460+ class WindowsConsoleOutput (LegacyConsoleOutput ):
461+ """A console which understands escape sequences, as prompt-toolkit's Windows10_Output writes to."""
462+
463+ def __init__ (self , stream : Terminal ) -> None :
464+ super ().__init__ (stream )
465+ self .vt100_output = self
466+
467+
468+ @pytest .mark .parametrize (
469+ ("width" , "rows" ),
470+ [
471+ pytest .param (80 , 1 , id = "a line as wide as the console" ),
472+ pytest .param (159 , 2 , id = "a line one column short of two rows" ),
473+ pytest .param (160 , 2 , id = "a line which fills two rows" ),
474+ pytest .param (161 , 3 , id = "a line which reaches a third row" ),
475+ ],
476+ )
477+ def test_stdout_proxy_counts_rows_by_the_width_a_windows_console_wraps_at (width , rows ) -> None :
478+ """A line which fills the last column of a Windows console is continued from its own row.
479+
480+ prompt-toolkit reports such a console as one column narrower than the width it wraps at.
481+ Counting rows by that size went up one row too many, and wrote over the line above.
482+ """
483+ stream = Terminal ()
484+ with create_pipe_input () as pipe , create_app_session (input = pipe , output = WindowsConsoleOutput (stream )):
485+ proxy = command_toolbar ._ContextStdoutProxy ()
486+ try :
487+ proxy ._write_and_flush (None , "x" * width )
488+ proxy ._write_and_flush (None , "END\n " )
489+ finally :
490+ proxy .close ()
491+
492+ assert stream .getvalue () == "x" * width + f"\r \n <up { rows } >" + "x" * width + "END\n "
493+
494+
495+ @pytest .mark .parametrize (
496+ ("width" , "rows" ),
497+ [
498+ pytest .param (79 , 1 , id = "a line one column short of the console's width" ),
499+ pytest .param (80 , 2 , id = "a line as wide as the console has already wrapped" ),
500+ pytest .param (81 , 2 , id = "a line which reaches a second row" ),
501+ pytest .param (159 , 2 , id = "a line one column short of two rows" ),
502+ pytest .param (160 , 3 , id = "a line which fills two rows has already wrapped" ),
503+ ],
504+ )
505+ def test_stdout_proxy_counts_the_row_a_legacy_windows_console_has_wrapped_to (width , rows ) -> None :
506+ """A legacy console moves to the next row as soon as a row is full, not at the next character.
507+
508+ Ending a line which exactly fills its rows therefore leaves the cursor a row lower than
509+ on a terminal which waits. Going up one row too few wrote the line again a row below
510+ itself, which showed its last row twice.
511+ """
512+ stream = Terminal ()
513+ with create_pipe_input () as pipe , create_app_session (input = pipe , output = LegacyConsoleOutput (stream )):
514+ proxy = command_toolbar ._ContextStdoutProxy ()
515+ try :
516+ proxy ._write_and_flush (None , "x" * width )
517+ proxy ._write_and_flush (None , "END\n " )
518+ finally :
519+ proxy .close ()
520+
521+ assert stream .getvalue () == "x" * width + f"\r \n <up { rows } >" + "x" * width + "END\n "
522+
523+
405524@pytest .mark .parametrize ("flushed" , [True , False ])
406525def test_command_toolbar_keeps_an_unfinished_line (toolbar_app , flushed ) -> None :
407526 """An unfinished line is continued by later output, and ended when the toolbar stops."""
@@ -471,6 +590,33 @@ async def expire_cpr() -> None:
471590 assert "last words\n " in output .getvalue ()
472591
473592
593+ def test_command_toolbar_remembers_the_rows_its_toolbar_took_before_it_is_drawn_again () -> None :
594+ """A command's first output can arrive before the toolbar has been drawn for that command.
595+
596+ Each run of the application forgets what it drew, and prompt-toolkit leaves the toolbar
597+ out until the terminal has reported where its cursor is. Until then the toolbar counts
598+ for the rows the main prompt drew it with, which are the rows it is about to take again.
599+ """
600+ app = Cmd (allow_cli_args = False )
601+ output = Terminal ()
602+ app .stdout = output
603+
604+ with create_pipe_input () as pipe :
605+ app .main_session = PromptSession (
606+ input = pipe ,
607+ output = CprOutput (output ),
608+ bottom_toolbar = "one\n two" ,
609+ refresh_interval = 0.01 ,
610+ )
611+ window = app .main_session .layout .container .children [- 1 ].content
612+ # What the main prompt leaves behind after drawing a toolbar of two rows.
613+ window .render_info = SimpleNamespace (window_height = 2 )
614+ with app ._command_toolbar_context ():
615+ assert window .render_info is None
616+ assert app ._command_toolbar ._toolbar_rows () == 2
617+ assert app ._command_toolbar ._proxy ._toolbar_rows () == 2
618+
619+
474620def test_command_toolbar_suspension_waits_for_in_flight_writes (toolbar_app , monkeypatch ) -> None :
475621 app , _ , output = toolbar_app
476622 writing = threading .Event ()
0 commit comments