@@ -470,9 +470,12 @@ int PhysicalLineCount(int columns, bool isFirstLogicalLine, out int lenLastPhysi
470
470
{
471
471
// We're in the middle of a previous logical line, we
472
472
// need to clear to the end of the line.
473
- lenToClear = bufferWidth - ( lenLastLine % bufferWidth ) ;
474
- if ( physicalLine == 1 )
475
- lenToClear -= _initialX ;
473
+ if ( lenLastLine < bufferWidth )
474
+ {
475
+ lenToClear = bufferWidth - ( lenLastLine % bufferWidth ) ;
476
+ if ( physicalLine == 1 )
477
+ lenToClear -= _initialX ;
478
+ }
476
479
}
477
480
478
481
if ( lenToClear > 0 )
@@ -486,7 +489,8 @@ int PhysicalLineCount(int columns, bool isFirstLogicalLine, out int lenLastPhysi
486
489
487
490
while ( previousPhysicalLine > physicalLine )
488
491
{
489
- _console . Write ( "\n " ) ;
492
+ _console . SetCursorPosition ( 0 , _initialY + physicalLine ) ;
493
+
490
494
physicalLine += 1 ;
491
495
var lenToClear = physicalLine == previousPhysicalLine ? lenPrevLastLine : bufferWidth ;
492
496
if ( lenToClear > 0 )
@@ -507,13 +511,27 @@ int PhysicalLineCount(int columns, bool isFirstLogicalLine, out int lenLastPhysi
507
511
// Reset the colors after we've finished all our rendering.
508
512
_console . Write ( "\x1b [0m" ) ;
509
513
510
- if ( _initialY + physicalLine >= _console . BufferHeight )
514
+ if ( _initialY + physicalLine > bufferHeight )
511
515
{
512
516
// We had to scroll to render everything, update _initialY
513
- _initialY = _console . BufferHeight - physicalLine ;
517
+ _initialY = bufferHeight - physicalLine ;
514
518
}
515
519
520
+ // Calculate the coord to place the cursor for the next input.
516
521
var point = ConvertOffsetToPoint ( _current ) ;
522
+
523
+ if ( point . Y == bufferHeight )
524
+ {
525
+ // The cursor top exceeds the buffer height, so we need to
526
+ // scroll up the buffer by 1 line.
527
+ _console . Write ( "\n " ) ;
528
+
529
+ // Adjust the initial cursor position and the to-be-set cursor position
530
+ // after scrolling up the buffer.
531
+ _initialY -= 1 ;
532
+ point . Y -= 1 ;
533
+ }
534
+
517
535
_console . SetCursorPosition ( point . X , point . Y ) ;
518
536
_console . CursorVisible = true ;
519
537
0 commit comments