From 1392e5ab4337b60bb6ef084d46baef25448b0268 Mon Sep 17 00:00:00 2001 From: Jason Shirk Date: Mon, 26 Jun 2017 10:54:00 -0700 Subject: [PATCH 1/2] Write fewer escape sequences 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 --- .../ConsoleLib.cs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs b/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs index 4451f95a1f3..e56d6a6bbb5 100644 --- a/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs +++ b/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs @@ -1068,17 +1068,33 @@ public void WriteBufferLines(BufferChar[] buffer, ref int top, bool ensureBottom 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 (lastForegroundColor != nextForegroundColor) + { + Console.ForegroundColor = lastForegroundColor = nextForegroundColor; + } + if (lastBackgroundColor != nextBackgroundColor) + { + Console.BackgroundColor = lastBackgroundColor = nextBackgroundColor; + } Console.Write(buffer[i].UnicodeChar); } - Console.BackgroundColor = backgroundColor; - Console.ForegroundColor = foregroundColor; + if (backgroundColor != lastBackgroundColor) + { + Console.BackgroundColor = backgroundColor; + } + if (foregroundColor != lastForegroundColor) + { + Console.ForegroundColor = foregroundColor; + } } public void ScrollBuffer(int lines) From a00206b29784dabf1a58ff4e15ac3aeb38125483 Mon Sep 17 00:00:00 2001 From: Jason Shirk Date: Mon, 26 Jun 2017 16:04:03 -0700 Subject: [PATCH 2/2] address code review feedback --- src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs b/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs index e56d6a6bbb5..462207bbad8 100644 --- a/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs +++ b/src/Microsoft.PowerShell.PSReadLine/ConsoleLib.cs @@ -1075,11 +1075,11 @@ public void WriteBufferLines(BufferChar[] buffer, ref int top, bool ensureBottom // TODO: use escape sequences for better perf var nextForegroundColor = buffer[i].ForegroundColor; var nextBackgroundColor = buffer[i].BackgroundColor; - if (lastForegroundColor != nextForegroundColor) + if (nextForegroundColor != lastForegroundColor) { Console.ForegroundColor = lastForegroundColor = nextForegroundColor; } - if (lastBackgroundColor != nextBackgroundColor) + if (nextBackgroundColor != lastBackgroundColor) { Console.BackgroundColor = lastBackgroundColor = nextBackgroundColor; } @@ -1087,14 +1087,14 @@ public void WriteBufferLines(BufferChar[] buffer, ref int top, bool ensureBottom Console.Write(buffer[i].UnicodeChar); } - if (backgroundColor != lastBackgroundColor) - { - Console.BackgroundColor = backgroundColor; - } if (foregroundColor != lastForegroundColor) { Console.ForegroundColor = foregroundColor; } + if (backgroundColor != lastBackgroundColor) + { + Console.BackgroundColor = backgroundColor; + } } public void ScrollBuffer(int lines)