Skip to content

Commit 52e86d8

Browse files
authored
Console.Unix: don't calculate cached cursor position from the last column. (#78466)
After printing in the last column, setting CursorLeft is expected to place the cursor back in that same row.
1 parent 4efdaf5 commit 52e86d8

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

src/libraries/System.Console/src/System/ConsolePal.Unix.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,16 @@ private static unsafe void UpdatedCachedCursorPosition(byte* bufPtr, int count,
10321032
byte c = bufPtr[i];
10331033
if (c < 127 && c >= 32) // ASCII/UTF-8 characters that take up a single position
10341034
{
1035-
IncrementX();
1035+
left++;
1036+
1037+
// After printing in the last column, setting CursorLeft is expected to
1038+
// place the cursor back in that same row.
1039+
// Invalidate the cursor position rather than moving it to the next row.
1040+
if (left >= width)
1041+
{
1042+
InvalidateCachedCursorPosition();
1043+
return;
1044+
}
10361045
}
10371046
else if (c == (byte)'\r')
10381047
{
@@ -1041,7 +1050,12 @@ private static unsafe void UpdatedCachedCursorPosition(byte* bufPtr, int count,
10411050
else if (c == (byte)'\n')
10421051
{
10431052
left = 0;
1044-
IncrementY();
1053+
top++;
1054+
1055+
if (top >= height)
1056+
{
1057+
top = height - 1;
1058+
}
10451059
}
10461060
else if (c == (byte)'\b')
10471061
{
@@ -1059,25 +1073,6 @@ private static unsafe void UpdatedCachedCursorPosition(byte* bufPtr, int count,
10591073

10601074
// We pass cursorVersion because it may have changed the earlier check by calling GetWindowSize.
10611075
SetCachedCursorPosition(left, top, cursorVersion);
1062-
1063-
void IncrementY()
1064-
{
1065-
top++;
1066-
if (top >= height)
1067-
{
1068-
top = height - 1;
1069-
}
1070-
}
1071-
1072-
void IncrementX()
1073-
{
1074-
left++;
1075-
if (left >= width)
1076-
{
1077-
left = 0;
1078-
IncrementY();
1079-
}
1080-
}
10811076
}
10821077
}
10831078

src/libraries/System.Console/tests/ManualTests/ManualTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ public static void EncodingTest()
323323
AssertUserExpectedResults("Pi and Sigma or question marks");
324324
}
325325

326+
[ConditionalFact(nameof(ManualTestsEnabled))]
327+
public static void CursorLeftFromLastColumn()
328+
{
329+
Console.CursorLeft = Console.BufferWidth - 1;
330+
Console.Write("2");
331+
Console.CursorLeft = 0;
332+
Console.Write("1");
333+
Console.WriteLine();
334+
AssertUserExpectedResults("single line with '1' at the start and '2' at the end.");
335+
}
336+
326337
[ConditionalFact(nameof(ManualTestsEnabled))]
327338
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
328339
public static void ResizeTest()

0 commit comments

Comments
 (0)