Skip to content

Commit

Permalink
Teach PSReadline to not force the background color during render (#1626)
Browse files Browse the repository at this point in the history
Upcoming versions of the Windows Console will be able to differentiate
a color set by the Win32 API from a color set by VT. This code
transforms a Win32 color into a VT color, and then uses that VT color
during rendering. When an enlightened console host receives this VT, it
may handle it differently (translate it differently, reverse it
differently, store it differently). Use `39;49` for the default foreground/background.

We'll also teach the various console implementations how to
handle it.
  • Loading branch information
DHowett authored Jul 3, 2020
1 parent fd99806 commit 90422ac
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions PSReadLine/PlatformWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ internal class LegacyWin32Console : VirtualTerminal
{45, () => Console.BackgroundColor = ConsoleColor.DarkMagenta},
{43, () => Console.BackgroundColor = ConsoleColor.DarkYellow},
{47, () => Console.BackgroundColor = ConsoleColor.Gray},
{49, () => Console.BackgroundColor = InitialBG},
{100, () => Console.BackgroundColor = ConsoleColor.DarkGray},
{104, () => Console.BackgroundColor = ConsoleColor.Blue},
{102, () => Console.BackgroundColor = ConsoleColor.Green},
Expand All @@ -432,6 +433,7 @@ internal class LegacyWin32Console : VirtualTerminal
{35, () => Console.ForegroundColor = ConsoleColor.DarkMagenta},
{33, () => Console.ForegroundColor = ConsoleColor.DarkYellow},
{37, () => Console.ForegroundColor = ConsoleColor.Gray},
{39, () => Console.ForegroundColor = InitialFG},
{90, () => Console.ForegroundColor = ConsoleColor.DarkGray},
{94, () => Console.ForegroundColor = ConsoleColor.Blue},
{92, () => Console.ForegroundColor = ConsoleColor.Green},
Expand Down
3 changes: 1 addition & 2 deletions PSReadLine/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ private void Render()

private void ForceRender()
{
var defaultColor = VTColorUtils.MapColorToEscapeSequence(_console.ForegroundColor, isBackground: false) +
VTColorUtils.MapColorToEscapeSequence(_console.BackgroundColor, isBackground: true);
var defaultColor = "\x1b[39;49m";

// Geneate a sequence of logical lines with escape sequences for coloring.
int logicalLineCount = GenerateRender(defaultColor);
Expand Down
2 changes: 2 additions & 0 deletions test/MockConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ private static void ToggleNegative(TestConsole c, bool b)
{"45", c => c.BackgroundColor = ConsoleColor.DarkMagenta},
{"43", c => c.BackgroundColor = ConsoleColor.DarkYellow},
{"47", c => c.BackgroundColor = ConsoleColor.Gray},
{"49", c => c.BackgroundColor = DefaultBackground},
{"100", c => c.BackgroundColor = ConsoleColor.DarkGray},
{"104", c => c.BackgroundColor = ConsoleColor.Blue},
{"102", c => c.BackgroundColor = ConsoleColor.Green},
Expand All @@ -329,6 +330,7 @@ private static void ToggleNegative(TestConsole c, bool b)
{"35", c => c.ForegroundColor = ConsoleColor.DarkMagenta},
{"33", c => c.ForegroundColor = ConsoleColor.DarkYellow},
{"37", c => c.ForegroundColor = ConsoleColor.Gray},
{"39", c => c.ForegroundColor = DefaultForeground},
{"90", c => c.ForegroundColor = ConsoleColor.DarkGray},
{"94", c => c.ForegroundColor = ConsoleColor.Blue},
{"92", c => c.ForegroundColor = ConsoleColor.Green},
Expand Down

0 comments on commit 90422ac

Please sign in to comment.