Skip to content

Commit a6b0e76

Browse files
authored
Use const strings for some often used ANSI control sequences (#1809)
1 parent a45ab81 commit a6b0e76

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ public string[] PromptText
358358
for (int i = 0; i < minLength; i ++)
359359
{
360360
var text = _promptText[i];
361-
if (text.Contains('\x1b') && !text.EndsWith("\x1b[0m", StringComparison.Ordinal))
361+
if (text.Contains('\x1b') && !text.EndsWith(VTColorUtils.AnsiReset, StringComparison.Ordinal))
362362
{
363-
_promptText[i] = string.Concat(text, "\x1b[0m");
363+
_promptText[i] = string.Concat(text, VTColorUtils.AnsiReset);
364364
}
365365
}
366366
}
@@ -946,6 +946,11 @@ protected override void EndProcessing()
946946

947947
public static class VTColorUtils
948948
{
949+
internal const string AnsiReset = "\x1b[0m";
950+
internal const string DefaultForeground = "\x1b[39m";
951+
internal const string DefaultBackground = "\x1b[49m";
952+
internal const string DefaultColor = "\x1b[39;49m";
953+
949954
public const ConsoleColor UnknownColor = (ConsoleColor) (-1);
950955
private static readonly Dictionary<string, ConsoleColor> ConsoleColors =
951956
new Dictionary<string, ConsoleColor>(StringComparer.OrdinalIgnoreCase)
@@ -1138,7 +1143,7 @@ public static string FormatColor(object seq)
11381143
var result = seq.ToString();
11391144
if (seq is ConsoleColor) return result;
11401145

1141-
result = result + "\"" + FormatEscape(result) + "\"" + "\x1b[0m";
1146+
result = result + "\"" + FormatEscape(result) + "\"" + AnsiReset;
11421147
return result;
11431148
}
11441149
}

PSReadLine/Completion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public void UpdateMenuSelection(int selectedItem, bool select, bool showTooltips
603603

604604
if (select) console.Write(Singleton.Options._selectionColor);
605605
console.Write(GetMenuItem(listItem, ColumnWidth));
606-
if (select) console.Write("\x1b[0m");
606+
if (select) console.Write(VTColorUtils.AnsiReset);
607607

608608
ToolTipLines = 0;
609609
if (showTooltips)
@@ -617,7 +617,7 @@ public void UpdateMenuSelection(int selectedItem, bool select, bool showTooltips
617617
console.Write(toolTip);
618618
ToolTipLines = toolTipLines;
619619

620-
console.Write("\x1b[0m");
620+
console.Write(VTColorUtils.AnsiReset);
621621
}
622622

623623
RestoreCursor();

PSReadLine/Render.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void Render()
122122

123123
private void ForceRender()
124124
{
125-
var defaultColor = "\x1b[39;49m";
125+
var defaultColor = VTColorUtils.DefaultColor;
126126

127127
// Geneate a sequence of logical lines with escape sequences for coloring.
128128
int logicalLineCount = GenerateRender(defaultColor);
@@ -179,7 +179,7 @@ void RenderOneChar(char charToRender, bool toEmphasize)
179179
if (inSelectedRegion)
180180
{
181181
// Turn off inverse before end of line, turn on after continuation prompt
182-
_consoleBufferLines[currentLogicalLine].Append("\x1b[0m");
182+
_consoleBufferLines[currentLogicalLine].Append(VTColorUtils.AnsiReset);
183183
}
184184

185185
currentLogicalLine += 1;
@@ -254,7 +254,7 @@ void RenderOneChar(char charToRender, bool toEmphasize)
254254
}
255255
else if (i == selectionEnd)
256256
{
257-
_consoleBufferLines[currentLogicalLine].Append("\x1b[0m");
257+
_consoleBufferLines[currentLogicalLine].Append(VTColorUtils.AnsiReset);
258258
_consoleBufferLines[currentLogicalLine].Append(activeColor);
259259
inSelectedRegion = false;
260260
}
@@ -334,7 +334,7 @@ void RenderOneChar(char charToRender, bool toEmphasize)
334334

335335
if (inSelectedRegion)
336336
{
337-
_consoleBufferLines[currentLogicalLine].Append("\x1b[0m");
337+
_consoleBufferLines[currentLogicalLine].Append(VTColorUtils.AnsiReset);
338338
inSelectedRegion = false;
339339
}
340340

@@ -444,7 +444,7 @@ private bool RenderErrorPrompt(RenderData renderData, string defaultColor)
444444
string color = renderData.errorPrompt ? _options._errorColor : defaultColor;
445445
_console.Write(color);
446446
_console.Write(promptText);
447-
_console.Write("\x1b[0m");
447+
_console.Write(VTColorUtils.AnsiReset);
448448
}
449449
else
450450
{
@@ -772,7 +772,7 @@ void UpdateColorsIfNecessary(string newColor)
772772
physicalLine -= pseudoPhysicalLineOffset;
773773

774774
// Reset the colors after we've finished all our rendering.
775-
_console.Write("\x1b[0m");
775+
_console.Write(VTColorUtils.AnsiReset);
776776

777777
if (_initialY + physicalLine > bufferHeight)
778778
{

0 commit comments

Comments
 (0)