8000 Minimize writing ANSI escape sequences in PSReadline (#4110) · PowerShell/PowerShell@ff56097 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff56097

Browse files
authored
Minimize writing ANSI escape sequences in PSReadline (#4110)
Instead of changing Console.*groundColor on every character, only set the color when the color changes - this avoids writing out ANSI escape sequences after every character. Fixes #4075
1 parent 27e47f9 commit ff56097

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,17 +1068,33 @@ public void WriteBufferLines(BufferChar[] buffer, ref int top, bool ensureBottom
10681068

10691069
Console.SetCursorPosition(0, (top>=0) ? top : 0);
10701070

1071+
var lastForegroundColor = (ConsoleColor)(-1);
1072+
var lastBackgroundColor = (ConsoleColor)(-1);
10711073
for (int i = 0; i < buffer.Length; ++i)
10721074
{
10731075
// TODO: use escape sequences for better perf
1074-
Console.ForegroundColor = buffer[i].ForegroundColor;
1075-
Console.BackgroundColor = buffer[i].BackgroundColor;
1076+
var nextForegroundColor = buffer[i].ForegroundColor;
1077+
var nextBackgroundColor = buffer[i].BackgroundColor;
1078+
if (nextForegroundColor != lastForegroundColor)
1079+
{
1080+
Console.ForegroundColor = lastForegroundColor = nextForegroundColor;
1081+
}
1082+
if (nextBackgroundColor != lastBackgroundColor)
1083+
{
1084+
Console.BackgroundColor = lastBackgroundColor = nextBackgroundColor;
1085+
}
10761086

10771087
Console.Write(buffer[i].UnicodeChar);
10781088
}
10791089

1080-
Console.BackgroundColor = backgroundColor;
1081-
Console.ForegroundColor = foregroundColor;
1090+
if (foregroundColor != lastForegroundColor)
1091+
{
1092+
Console.ForegroundColor = foregroundColor;
1093+
}
1094+
if (backgroundColor != lastBackgroundColor)
1095+
{
1096+
Console.BackgroundColor = backgroundColor;
1097+
}
10821098
}
10831099

10841100
public void ScrollBuffer(int lines)

0 commit comments

Comments
 (0)
0