Skip to content

Console.Unix: don't calculate cached cursor position from the last column. - #78466

Merged
adamsitnik merged 1 commit into
dotnet:mainfrom
tmds:cursor_invalidate
Dec 13, 2022
Merged

adamsitnik merged 1 commit into
dotnet:mainfrom
tmds:cursor_invalidate

Conversation

@tmds

@tmds tmds commented Nov 16, 2022

Copy link
Copy Markdown
Member

After printing in the last column, setting CursorLeft is expected to place the cursor back in that same row.

Fixes #77995.

@adamsitnik ptal.

cc @qt-kaneko

…lumn.

After printing in the last column, setting CursorLeft is expected to
place the cursor back in that same row.
@ghost ghost added area-System.Console community-contribution Indicates that the PR has been added by a community member labels Nov 16, 2022
@ghost

ghost commented Nov 16, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.

Issue Details

After printing in the last column, setting CursorLeft is expected to place the cursor back in that same row.

Fixes #77995.

@adamsitnik ptal.

cc @qt-kaneko

Author: tmds
Assignees: -
Labels:

area-System.Console, community-contribution

Milestone: -


// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because behavior varies and so we can't predict what the terminal is actually going to do?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to treat one position past the window width special in that setting CursorLeft from that will remain on the same line.

The position behaves special.

Console.CursorLeft = Console.BufferWidth - 3;
var positions = new (int Left, int Top)[5];
for (int i = 0; i < positions.Length; i++)
{
    Console.Write((char)0xA3);
    positions[i] = Console.GetCursorPosition();
}
Console.WriteLine();
Console.WriteLine(string.Join(" ", positions));

When I run this, I get:

                                                                                                                   £££
££
(116, 4) (117, 4) (117, 4) (1, 5) (2, 5)

Notice how X is 117 two times. I assume on Windows, you'll see a (0, 5) for the second one.

With this change the visual behavior will now be the same as on Windows.

We could try to distinguish between the two (117, 4) but that brings extra complexity to the caching. Invalidating the cache is simpler.

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the fix @tmds !

@adamsitnik
adamsitnik merged commit 52e86d8 into dotnet:main Dec 13, 2022
@adamsitnik adamsitnik added this to the 8.0.0 milestone Dec 13, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Console community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid cached console cursor position on Unix

3 participants