@@ -404,6 +404,16 @@ def cursor_up(self, amount: int) -> None:
404404 "z" * (24 * 80 + 1 ) + "\r \n END\n " ,
405405 id = "a line longer than the screen stays ended" ,
406406 ),
407+ pytest .param (
408+ ["z" * (23 * 80 + 1 ), "END\n " ],
409+ "z" * (23 * 80 + 1 ) + "\r \n END\n " ,
410+ id = "a line which reaches the last row stays ended" ,
411+ ),
412+ pytest .param (
413+ ["z" * (23 * 80 ), "END\n " ],
414+ "z" * (23 * 80 ) + "\r \n <up 23>" + "z" * (23 * 80 ) + "END\n " ,
415+ id = "a line which stops above the last row is continued" ,
416+ ),
407417 ],
408418)
409419def test_stdout_proxy_continues_unfinished_lines (writes , expected ) -> None :
@@ -421,6 +431,115 @@ def test_stdout_proxy_continues_unfinished_lines(writes, expected) -> None:
421431 assert stream .getvalue () == expected
422432
423433
434+ @pytest .mark .parametrize (
435+ ("width" , "expected" ),
436+ [
437+ pytest .param (22 * 80 , "\r \n <up 22>" + "z" * (22 * 80 ) + "END\n " , id = "continued while the toolbar fits below it" ),
438+ pytest .param (22 * 80 + 1 , "\r \n END\n " , id = "stays ended once the toolbar would scroll its first row off" ),
439+ ],
440+ )
441+ def test_stdout_proxy_leaves_room_for_every_row_of_the_toolbar (width , expected ) -> None :
442+ """A toolbar of several rows scrolls the screen sooner than one row does."""
443+ stream = Terminal ()
444+ with create_pipe_input () as pipe , create_app_session (input = pipe , output = CursorOutput (stream )):
445+ proxy = command_toolbar ._ContextStdoutProxy (lambda : 2 )
446+ try :
447+ proxy ._write_and_flush (None , "z" * width )
448+ proxy ._write_and_flush (None , "END\n " )
449+ finally :
450+ proxy .close ()
451+
452+ assert stream .getvalue () == "z" * width + expected
453+
454+
455+ def test_command_toolbar_tells_the_proxy_how_many_rows_it_takes (toolbar_app ) -> None :
456+ app , _ , _ = toolbar_app
457+ app .main_session .bottom_toolbar = "one\n two"
458+ # Nothing has been drawn yet.
459+ assert command_toolbar .CommandToolbar (app )._toolbar_rows () == 0
460+ with app ._command_toolbar_context ():
461+ assert app ._command_toolbar ._toolbar_rows () == 2
462+ assert app ._command_toolbar ._proxy ._toolbar_rows () == 2
463+
464+
465+ class LegacyConsoleOutput (CursorOutput ):
466+ """Report a size the way prompt-toolkit's Windows outputs do, one column short of the console's.
467+
468+ Like prompt-toolkit's Win32Output, which a legacy console gets, it has no vt100 output.
469+ """
470+
471+ def __init__ (self , stream : Terminal ) -> None :
472+ super ().__init__ (stream )
473+ self .size = Size (rows = 24 , columns = 79 )
474+
475+ def get_win32_screen_buffer_info (self ) -> SimpleNamespace :
476+ return SimpleNamespace (dwSize = SimpleNamespace (X = 80 , Y = 24 ))
477+
478+
479+ class WindowsConsoleOutput (LegacyConsoleOutput ):
480+ """A console which understands escape sequences, as prompt-toolkit's Windows10_Output writes to."""
481+
482+ def __init__ (self , stream : Terminal ) -> None :
483+ super ().__init__ (stream )
484+ self .vt100_output = self
485+
486+
487+ @pytest .mark .parametrize (
488+ ("width" , "rows" ),
489+ [
490+ pytest .param (80 , 1 , id = "a line as wide as the console" ),
491+ pytest .param (159 , 2 , id = "a line one column short of two rows" ),
492+ pytest .param (160 , 2 , id = "a line which fills two rows" ),
493+ pytest .param (161 , 3 , id = "a line which reaches a third row" ),
494+ ],
495+ )
496+ def test_stdout_proxy_counts_rows_by_the_width_a_windows_console_wraps_at (width , rows ) -> None :
497+ """A line which fills the last column of a Windows console is continued from its own row.
498+
499+ prompt-toolkit reports such a console as one column narrower than the width it wraps at.
500+ Counting rows by that size went up one row too many, and wrote over the line above.
501+ """
502+ stream = Terminal ()
503+ with create_pipe_input () as pipe , create_app_session (input = pipe , output = WindowsConsoleOutput (stream )):
504+ proxy = command_toolbar ._ContextStdoutProxy ()
505+ try :
506+ proxy ._write_and_flush (None , "x" * width )
507+ proxy ._write_and_flush (None , "END\n " )
508+ finally :
509+ proxy .close ()
510+
511+ assert stream .getvalue () == "x" * width + f"\r \n <up { rows } >" + "x" * width + "END\n "
512+
513+
514+ @pytest .mark .parametrize (
515+ ("width" , "rows" ),
516+ [
517+ pytest .param (79 , 1 , id = "a line one column short of the console's width" ),
518+ pytest .param (80 , 2 , id = "a line as wide as the console has already wrapped" ),
519+ pytest .param (81 , 2 , id = "a line which reaches a second row" ),
520+ pytest .param (159 , 2 , id = "a line one column short of two rows" ),
521+ pytest .param (160 , 3 , id = "a line which fills two rows has already wrapped" ),
522+ ],
523+ )
524+ def test_stdout_proxy_counts_the_row_a_legacy_windows_console_has_wrapped_to (width , rows ) -> None :
525+ """A legacy console moves to the next row as soon as a row is full, not at the next character.
526+
527+ Ending a line which exactly fills its rows therefore leaves the cursor a row lower than
528+ on a terminal which waits. Going up one row too few wrote the line again a row below
529+ itself, which showed its last row twice.
530+ """
531+ stream = Terminal ()
532+ with create_pipe_input () as pipe , create_app_session (input = pipe , output = LegacyConsoleOutput (stream )):
533+ proxy = command_toolbar ._ContextStdoutProxy ()
534+ try :
535+ proxy ._write_and_flush (None , "x" * width )
536+ proxy ._write_and_flush (None , "END\n " )
537+ finally :
538+ proxy .close ()
539+
540+ assert stream .getvalue () == "x" * width + f"\r \n <up { rows } >" + "x" * width + "END\n "
541+
542+
424543@pytest .mark .parametrize ("flushed" , [True , False ])
425544def test_command_toolbar_keeps_an_unfinished_line (toolbar_app , flushed ) -> None :
426545 """An unfinished line is continued by later output, and ended when the toolbar stops."""
@@ -490,6 +609,33 @@ async def expire_cpr() -> None:
490609 assert "last words\n " in output .getvalue ()
491610
492611
612+ def test_command_toolbar_remembers_the_rows_its_toolbar_took_before_it_is_drawn_again () -> None :
613+ """A command's first output can arrive before the toolbar has been drawn for that command.
614+
615+ Each run of the application forgets what it drew, and prompt-toolkit leaves the toolbar
616+ out until the terminal has reported where its cursor is. Until then the toolbar counts
617+ for the rows the main prompt drew it with, which are the rows it is about to take again.
618+ """
619+ app = Cmd (allow_cli_args = False )
620+ output = Terminal ()
621+ app .stdout = output
622+
623+ with create_pipe_input () as pipe :
624+ app .main_session = PromptSession (
625+ input = pipe ,
626+ output = CprOutput (output ),
627+ bottom_toolbar = "one\n two" ,
628+ refresh_interval = 0.01 ,
629+ )
630+ window = app .main_session .layout .container .children [- 1 ].content
631+ # What the main prompt leaves behind after drawing a toolbar of two rows.
632+ window .render_info = SimpleNamespace (window_height = 2 )
633+ with app ._command_toolbar_context ():
634+ assert window .render_info is None
635+ assert app ._command_toolbar ._toolbar_rows () == 2
636+ assert app ._command_toolbar ._proxy ._toolbar_rows () == 2
637+
638+
493639def test_command_toolbar_suspension_waits_for_in_flight_writes (toolbar_app , monkeypatch ) -> None :
494640 app , _ , output = toolbar_app
495641 writing = threading .Event ()
0 commit comments