Skip to content

Commit ef2f65f

Browse files
authored
Fix the regression to make long lines work properly at the end of screen buffer (#895)
1 parent fdd38b9 commit ef2f65f

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

PSReadLine/Render.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,12 @@ int PhysicalLineCount(int columns, bool isFirstLogicalLine, out int lenLastPhysi
470470
{
471471
// We're in the middle of a previous logical line, we
472472
// 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+
}
476479
}
477480

478481
if (lenToClear > 0)
@@ -486,7 +489,8 @@ int PhysicalLineCount(int columns, bool isFirstLogicalLine, out int lenLastPhysi
486489

487490
while (previousPhysicalLine > physicalLine)
488491
{
489-
_console.Write("\n");
492+
_console.SetCursorPosition(0, _initialY + physicalLine);
493+
490494
physicalLine += 1;
491495
var lenToClear = physicalLine == previousPhysicalLine ? lenPrevLastLine : bufferWidth;
492496
if (lenToClear > 0)
@@ -507,13 +511,27 @@ int PhysicalLineCount(int columns, bool isFirstLogicalLine, out int lenLastPhysi
507511
// Reset the colors after we've finished all our rendering.
508512
_console.Write("\x1b[0m");
509513

510-
if (_initialY + physicalLine >= _console.BufferHeight)
514+
if (_initialY + physicalLine > bufferHeight)
511515
{
512516
// We had to scroll to render everything, update _initialY
513-
_initialY = _console.BufferHeight - physicalLine;
517+
_initialY = bufferHeight - physicalLine;
514518
}
515519

520+
// Calculate the coord to place the cursor for the next input.
516521
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+
517535
_console.SetCursorPosition(point.X, point.Y);
518536
_console.CursorVisible = true;
519537

0 commit comments

Comments
 (0)