mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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
This commit is contained in:
@@ -1068,17 +1068,33 @@ namespace Microsoft.PowerShell.Internal
|
||||
|
||||
Console.SetCursorPosition(0, (top>=0) ? top : 0);
|
||||
|
||||
var lastForegroundColor = (ConsoleColor)(-1);
|
||||
var lastBackgroundColor = (ConsoleColor)(-1);
|
||||
for (int i = 0; i < buffer.Length; ++i)
|
||||
{
|
||||
// TODO: use escape sequences for better perf
|
||||
Console.ForegroundColor = buffer[i].ForegroundColor;
|
||||
Console.BackgroundColor = buffer[i].BackgroundColor;
|
||||
var nextForegroundColor = buffer[i].ForegroundColor;
|
||||
var nextBackgroundColor = buffer[i].BackgroundColor;
|
||||
if (nextForegroundColor != lastForegroundColor)
|
||||
{
|
||||
Console.ForegroundColor = lastForegroundColor = nextForegroundColor;
|
||||
}
|
||||
if (nextBackgroundColor != lastBackgroundColor)
|
||||
{
|
||||
Console.BackgroundColor = lastBackgroundColor = nextBackgroundColor;
|
||||
}
|
||||
|
||||
Console.Write(buffer[i].UnicodeChar);
|
||||
}
|
||||
|
||||
Console.BackgroundColor = backgroundColor;
|
||||
Console.ForegroundColor = foregroundColor;
|
||||
if (foregroundColor != lastForegroundColor)
|
||||
{
|
||||
Console.ForegroundColor = foregroundColor;
|
||||
}
|
||||
if (backgroundColor != lastBackgroundColor)
|
||||
{
|
||||
Console.BackgroundColor = backgroundColor;
|
||||
}
|
||||
}
|
||||
|
||||
public void ScrollBuffer(int lines)
|
||||
|
||||
Reference in New Issue
Block a user