Skip to content

Add Console.GetCursorPosition #37559

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

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libraries/System.Console/ref/System.Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static partial class Console
public static bool CursorVisible { get { throw null; } set { } }
public static System.IO.TextWriter Error { get { throw null; } }
public static System.ConsoleColor ForegroundColor { get { throw null; } set { } }
public static (int Left, int Top) GetCursorPosition() { throw null; }
public static System.IO.TextReader In { get { throw null; } }
public static System.Text.Encoding InputEncoding { get { throw null; } set { } }
public static bool IsErrorRedirected { get { throw null; } }
Expand Down
11 changes: 9 additions & 2 deletions src/libraries/System.Console/src/System/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,23 @@ public static bool CursorVisible

public static int CursorLeft
{
get { return ConsolePal.CursorLeft; }
get { return ConsolePal.GetCursorPosition().Left; }
set { SetCursorPosition(value, CursorTop); }
}

public static int CursorTop
{
get { return ConsolePal.CursorTop; }
get { return ConsolePal.GetCursorPosition().Top; }
set { SetCursorPosition(CursorLeft, value); }
}

/// <summary>Gets the position of the cursor.</summary>
/// <returns>The column and row position of the cursor.</returns>
public static (int Left, int Top) GetCursorPosition()
{
return ConsolePal.GetCursorPosition();
}

public static string Title
{
get { return ConsolePal.Title; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ public static bool CursorVisible
set => throw new PlatformNotSupportedException();
}

public static int CursorLeft => throw new PlatformNotSupportedException();

public static int CursorTop => throw new PlatformNotSupportedException();
public static (int Left, int Top) GetCursorPosition() => throw new PlatformNotSupportedException();

public static string Title
{
Expand Down
20 changes: 3 additions & 17 deletions src/libraries/System.Console/src/System/ConsolePal.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,24 +416,10 @@ public static bool CursorVisible
}
}

public static int CursorLeft
public static (int Left, int Top) GetCursorPosition()
{
get
{
int left, top;
TryGetCursorPosition(out left, out top);
return left;
}
}

public static int CursorTop
{
get
{
int left, top;
TryGetCursorPosition(out left, out top);
return top;
}
TryGetCursorPosition(out int left, out int top);
return (left, top);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ public static bool CursorVisible
set => throw new PlatformNotSupportedException();
}

public static int CursorLeft => throw new PlatformNotSupportedException();

public static int CursorTop => throw new PlatformNotSupportedException();
public static (int Left, int Top) GetCursorPosition() => throw new PlatformNotSupportedException();

public static string Title
{
Expand Down
18 changes: 3 additions & 15 deletions src/libraries/System.Console/src/System/ConsolePal.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,22 +570,10 @@ public static bool CursorVisible
}
}

public static int CursorLeft
public static (int Left, int Top) GetCursorPosition()
{
get
{
Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo();
return csbi.dwCursorPosition.X;
}
}

public static int CursorTop
{
get
{
Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo();
return csbi.dwCursorPosition.Y;
}
Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo();
return (csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y);
}

public static unsafe string Title
Expand Down
6 changes: 2 additions & 4 deletions src/libraries/System.Console/src/System/ConsolePal.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public static bool CursorVisible
set => throw new PlatformNotSupportedException();
}

public static int CursorLeft => throw new PlatformNotSupportedException();

public static int CursorTop => throw new PlatformNotSupportedException();
public static (int Left, int Top) GetCursorPosition() => throw new PlatformNotSupportedException();

public static string Title
{
Expand Down Expand Up @@ -170,4 +168,4 @@ internal sealed class ControlCHandlerRegistrar
internal void Unregister() => throw new PlatformNotSupportedException();
}
}
}
}
4 changes: 4 additions & 0 deletions src/libraries/System.Console/tests/WindowAndCursorProps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,23 @@ public static void GetCursorPosition_Invoke_ReturnsExpected()
if (!Console.IsInputRedirected && !Console.IsOutputRedirected)
{
int origLeft = Console.CursorLeft, origTop = Console.CursorTop;
(int, int) origTuple = Console.GetCursorPosition();

Console.SetCursorPosition(10, 12);
Assert.Equal(10, Console.CursorLeft);
Assert.Equal(12, Console.CursorTop);
Assert.Equal((10, 12), Console.GetCursorPosition());

Console.SetCursorPosition(origLeft, origTop);
Assert.Equal(origLeft, Console.CursorLeft);
Assert.Equal(origTop, Console.CursorTop);
Assert.Equal(origTuple, Console.GetCursorPosition());
}
else if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Equal(0, Console.CursorLeft);
Assert.Equal(0, Console.CursorTop);
Assert.Equal((0, 0), Console.GetCursorPosition());
}
}

Expand Down