-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Console should provide a GetCursorPosition property to get the position atomically #28535
Comments
Could you please format like an API proposal (info in docs folder)? That will make API review meeting quicker. |
@wtgodbe owns this area. If on at least one platform we can get the value atomically, this seems like a reasonable idea to me. |
I am not sure the best type as we seem to avoid adding API with tuples (based on a discussion I can't find right now). Point would be nice but it's in winforms. |
It's in System.Drawing.Primitives in corefx. |
@danmosemsft Will do so (docs). Currently both |
@stephentoub thank you, maybe I should not comment on phone where I can't look. |
😄 |
This API should return a struct to not force the caller to accept side-effects with those |
@GSPP I know what you are talking about, but I thought that it would be slightly weird to have an impedance mismatch between the Do you feel I should re-write this into TWO NEW methods accepting a single struct? |
What side-effects are you referring to? The overall behavior (and work) for whether you return a struct or two ints is effectively the same. Returning a struct requires exposing additional public surface area, however. |
@tannergooding I can't find the exact link and therefore the exact name for "this", but I think @GSPP is referring to the fact that I also might be completely wrong in my interpretation... :) |
Before C# got For example, I also would argue that creating a small Maybe this illustrates the point: It is suboptimal API design that
Hope these thoughts make sense. |
Approved with modifications namespace System
{
public static class Console
{
public static (int Left, int Top) GetCursorPosition();
}
} |
Currently, the
System.Console
class in provides two separate properties,CursorLeft
andCursorTop
that allow the user to change / read the position of the console cursor.Unfortunately, there seems to be no public API that allows reading the cursor position "atomically" (e.g. both row and column position) although the underlying implementation does get/set the actual position atomically.
Conversly, there does exist a
SetCursorPosition
public method:https://github.com/dotnet/corefx/blob/eec001d96a68376c0e504eb7635c8edec196f90f/src/System.Console/src/System/Console.cs#L306
Rational and Usage
The idea would be to be able to read the cursor position on its associated terminal/console in one atomic API call, unlike today, where users of the
Console
class often write code like:The proposed API would allow reading/setting the position in one API call that would also translate as one atomic system call / write to the underlying OS:
The proposed change makes reading the console cursor position more readable, efficient (more on that later) and most importantly atomic, in the sense that the API will allow to ensure that the console cursor is/was in the reported position, unlike today, where the console cursor position can change in between calls to the getters/setters of
CursorLeft
andCursorTop
.Proposed API
Details
ConsolePal
classes:https://github.com/dotnet/corefx/blob/ec7a4c6e09d655421195580b7eea887000afc367/src/System.Console/src/System/ConsolePal.Windows.cs#L1078
https://github.com/dotnet/corefx/blob/ec7a4c6e09d655421195580b7eea887000afc367/src/System.Console/src/System/ConsolePal.Unix.cs#L348
GetCursorPosition
inConsolePal.Unix.cs
linked above makes that abundantly clear).The text was updated successfully, but these errors were encountered: