Skip to content

Commit

Permalink
Console.Unix: don't calculate cached cursor position from the last co…
Browse files Browse the repository at this point in the history
…lumn. (dotnet#78466)

After printing in the last column, setting CursorLeft is expected to
place the cursor back in that same row.
  • Loading branch information
tmds authored Dec 13, 2022
1 parent 4efdaf5 commit 52e86d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/libraries/System.Console/src/System/ConsolePal.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,16 @@ private static unsafe void UpdatedCachedCursorPosition(byte* bufPtr, int count,
byte c = bufPtr[i];
if (c < 127 && c >= 32) // ASCII/UTF-8 characters that take up a single position
{
IncrementX();
left++;

// After printing in the last column, setting CursorLeft is expected to
// place the cursor back in that same row.
// Invalidate the cursor position rather than moving it to the next row.
if (left >= width)
{
InvalidateCachedCursorPosition();
return;
}
}
else if (c == (byte)'\r')
{
Expand All @@ -1041,7 +1050,12 @@ private static unsafe void UpdatedCachedCursorPosition(byte* bufPtr, int count,
else if (c == (byte)'\n')
{
left = 0;
IncrementY();
top++;

if (top >= height)
{
top = height - 1;
}
}
else if (c == (byte)'\b')
{
Expand All @@ -1059,25 +1073,6 @@ private static unsafe void UpdatedCachedCursorPosition(byte* bufPtr, int count,

// We pass cursorVersion because it may have changed the earlier check by calling GetWindowSize.
SetCachedCursorPosition(left, top, cursorVersion);

void IncrementY()
{
top++;
if (top >= height)
{
top = height - 1;
}
}

void IncrementX()
{
left++;
if (left >= width)
{
left = 0;
IncrementY();
}
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/libraries/System.Console/tests/ManualTests/ManualTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ public static void EncodingTest()
AssertUserExpectedResults("Pi and Sigma or question marks");
}

[ConditionalFact(nameof(ManualTestsEnabled))]
public static void CursorLeftFromLastColumn()
{
Console.CursorLeft = Console.BufferWidth - 1;
Console.Write("2");
Console.CursorLeft = 0;
Console.Write("1");
Console.WriteLine();
AssertUserExpectedResults("single line with '1' at the start and '2' at the end.");
}

[ConditionalFact(nameof(ManualTestsEnabled))]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void ResizeTest()
Expand Down

0 comments on commit 52e86d8

Please sign in to comment.