Skip to content
Merged
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
12 changes: 9 additions & 3 deletions PSReadLine/ConsoleLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int CursorTop
set => Console.CursorTop = value;
}

// .NET doesn't implement this API, so we fake it with a commonly supported escape sequence.
// .NET doesn't fully implement this API on all platforms, so we fake it with a commonly supported escape sequence.
protected int _unixCursorSize = 25;
public virtual int CursorSize
{
Expand All @@ -45,9 +45,15 @@ public virtual int CursorSize
else
{
_unixCursorSize = value;
// Solid blinking block or blinking vertical bar
Write(value > 50 ? "\x1b[2 q" : "\x1b[5 q");
}

// See the cursor ANSI codes at https://www.real-world-systems.com/docs/ANSIcode.html, searching for 'blinking block'.
// We write out the ANSI escape sequence even if we are on Windows, where the 'Console.CursorSize' API is supported by .NET.
// This is because this API works fine in console host, but not in Windows Terminal. The escape sequence will configure the
// cursor as expected in Windows Terminal, while in console host, the escape sequence works after it's written out, but then
// will be overwritten by 'CursorSize' when the user continues typing.
Comment on lines +50 to +54
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean there's a bit of a race condition where it can look unexpectedly different before the user continues typing?

Copy link
Member Author

@daxian-dbw daxian-dbw Jul 8, 2022

Choose a reason for hiding this comment

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

There could be. The blinking underscore shape from Ansi code is thinner that "Console.CursorSize = 25" in console host, but not different too much. In Windows Terminal, there is no race condition, since we can change the cursor shape only through the Ansi sequences.

// We use blinking block and blinking underscore, so as to mimic the cursor size 100 and 25 in console host.
Write(value > 50 ? "\x1b[1 q" : "\x1b[3 q");
}
}

Expand Down