Skip to content

Commit d3a2432

Browse files
committed
Use \e instead of \u001B or \x1B
1 parent cbca508 commit d3a2432

File tree

15 files changed

+123
-123
lines changed

15 files changed

+123
-123
lines changed

eng/Versions.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
-->
4545
<MicrosoftCodeAnalysisCSharpVersion>4.10.0-2.24114.13</MicrosoftCodeAnalysisCSharpVersion>
4646
<MicrosoftCodeAnalysisVersion>4.10.0-2.24114.13</MicrosoftCodeAnalysisVersion>
47-
<MicrosoftNetCompilersToolsetVersion>4.10.0-2.24114.13</MicrosoftNetCompilersToolsetVersion>
47+
<MicrosoftNetCompilersToolsetVersion>4.10.0-3.24162.9</MicrosoftNetCompilersToolsetVersion>
4848
</PropertyGroup>
4949
<!--
5050
For source generator support we need to target multiple versions of Roslyn in order to be able to run on older versions of Roslyn.

src/installer/tests/TestUtils/AnsiColorExtensions.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,46 @@ public static class AnsiColorExtensions
77
{
88
public static string Black(this string text)
99
{
10-
return "\x1B[30m" + text + "\x1B[39m";
10+
return "\e[30m" + text + "\e[39m";
1111
}
1212

1313
public static string Red(this string text)
1414
{
15-
return "\x1B[31m" + text + "\x1B[39m";
15+
return "\e[31m" + text + "\e[39m";
1616
}
1717
public static string Green(this string text)
1818
{
19-
return "\x1B[32m" + text + "\x1B[39m";
19+
return "\e[32m" + text + "\e[39m";
2020
}
2121

2222
public static string Yellow(this string text)
2323
{
24-
return "\x1B[33m" + text + "\x1B[39m";
24+
return "\e[33m" + text + "\e[39m";
2525
}
2626

2727
public static string Blue(this string text)
2828
{
29-
return "\x1B[34m" + text + "\x1B[39m";
29+
return "\e[34m" + text + "\e[39m";
3030
}
3131

3232
public static string Magenta(this string text)
3333
{
34-
return "\x1B[35m" + text + "\x1B[39m";
34+
return "\e[35m" + text + "\e[39m";
3535
}
3636

3737
public static string Cyan(this string text)
3838
{
39-
return "\x1B[36m" + text + "\x1B[39m";
39+
return "\e[36m" + text + "\e[39m";
4040
}
4141

4242
public static string White(this string text)
4343
{
44-
return "\x1B[37m" + text + "\x1B[39m";
44+
return "\e[37m" + text + "\e[39m";
4545
}
4646

4747
public static string Bold(this string text)
4848
{
49-
return "\x1B[1m" + text + "\x1B[22m";
49+
return "\e[1m" + text + "\e[22m";
5050
}
5151
}
5252
}

src/installer/tests/TestUtils/AnsiConsole.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void Write(string message)
6666
var escapeScan = 0;
6767
for (;;)
6868
{
69-
var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal);
69+
var escapeIndex = message.IndexOf("\e[", escapeScan, StringComparison.Ordinal);
7070
if (escapeIndex == -1)
7171
{
7272
var text = message.Substring(escapeScan);

src/libraries/Microsoft.Extensions.Logging.Console/src/AnsiParser.cs

+28-28
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void Parse(string message)
5050
ConsoleColor? foreground = null;
5151
ConsoleColor? background = null;
5252
var span = message.AsSpan();
53-
const char EscapeChar = '\x1B';
53+
const char EscapeChar = '\e';
5454
ConsoleColor? color = null;
5555
bool isBright = false;
5656
for (int i = 0; i < span.Length; i++)
@@ -59,7 +59,7 @@ public void Parse(string message)
5959
{
6060
if (span[i + 3] == 'm')
6161
{
62-
// Example: \x1B[1m
62+
// Example: \e[1m
6363
if (IsDigit(span[i + 2]))
6464
{
6565
escapeCode = (int)(span[i + 2] - '0');
@@ -77,7 +77,7 @@ public void Parse(string message)
7777
}
7878
else if (span.Length >= i + 5 && span[i + 4] == 'm')
7979
{
80-
// Example: \x1B[40m
80+
// Example: \e[40m
8181
if (IsDigit(span[i + 2]) && IsDigit(span[i + 3]))
8282
{
8383
escapeCode = (int)(span[i + 2] - '0') * 10 + (int)(span[i + 3] - '0');
@@ -127,28 +127,28 @@ public void Parse(string message)
127127
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128128
private static bool IsDigit(char c) => (uint)(c - '0') <= ('9' - '0');
129129

130-
internal const string DefaultForegroundColor = "\x1B[39m\x1B[22m"; // reset to default foreground color
131-
internal const string DefaultBackgroundColor = "\x1B[49m"; // reset to the background color
130+
internal const string DefaultForegroundColor = "\e[39m\e[22m"; // reset to default foreground color
131+
internal const string DefaultBackgroundColor = "\e[49m"; // reset to the background color
132132

133133
internal static string GetForegroundColorEscapeCode(ConsoleColor color)
134134
{
135135
return color switch
136136
{
137-
ConsoleColor.Black => "\x1B[30m",
138-
ConsoleColor.DarkRed => "\x1B[31m",
139-
ConsoleColor.DarkGreen => "\x1B[32m",
140-
ConsoleColor.DarkYellow => "\x1B[33m",
141-
ConsoleColor.DarkBlue => "\x1B[34m",
142-
ConsoleColor.DarkMagenta => "\x1B[35m",
143-
ConsoleColor.DarkCyan => "\x1B[36m",
144-
ConsoleColor.Gray => "\x1B[37m",
145-
ConsoleColor.Red => "\x1B[1m\x1B[31m",
146-
ConsoleColor.Green => "\x1B[1m\x1B[32m",
147-
ConsoleColor.Yellow => "\x1B[1m\x1B[33m",
148-
ConsoleColor.Blue => "\x1B[1m\x1B[34m",
149-
ConsoleColor.Magenta => "\x1B[1m\x1B[35m",
150-
ConsoleColor.Cyan => "\x1B[1m\x1B[36m",
151-
ConsoleColor.White => "\x1B[1m\x1B[37m",
137+
ConsoleColor.Black => "\e[30m",
138+
ConsoleColor.DarkRed => "\e[31m",
139+
ConsoleColor.DarkGreen => "\e[32m",
140+
ConsoleColor.DarkYellow => "\e[33m",
141+
ConsoleColor.DarkBlue => "\e[34m",
142+
ConsoleColor.DarkMagenta => "\e[35m",
143+
ConsoleColor.DarkCyan => "\e[36m",
144+
ConsoleColor.Gray => "\e[37m",
145+
ConsoleColor.Red => "\e[1m\e[31m",
146+
ConsoleColor.Green => "\e[1m\e[32m",
147+
ConsoleColor.Yellow => "\e[1m\e[33m",
148+
ConsoleColor.Blue => "\e[1m\e[34m",
149+
ConsoleColor.Magenta => "\e[1m\e[35m",
150+
ConsoleColor.Cyan => "\e[1m\e[36m",
151+
ConsoleColor.White => "\e[1m\e[37m",
152152
_ => DefaultForegroundColor // default foreground color
153153
};
154154
}
@@ -157,14 +157,14 @@ internal static string GetBackgroundColorEscapeCode(ConsoleColor color)
157157
{
158158
return color switch
159159
{
160-
ConsoleColor.Black => "\x1B[40m",
161-
ConsoleColor.DarkRed => "\x1B[41m",
162-
ConsoleColor.DarkGreen => "\x1B[42m",
163-
ConsoleColor.DarkYellow => "\x1B[43m",
164-
ConsoleColor.DarkBlue => "\x1B[44m",
165-
ConsoleColor.DarkMagenta => "\x1B[45m",
166-
ConsoleColor.DarkCyan => "\x1B[46m",
167-
ConsoleColor.Gray => "\x1B[47m",
160+
ConsoleColor.Black => "\e[40m",
161+
ConsoleColor.DarkRed => "\e[41m",
162+
ConsoleColor.DarkGreen => "\e[42m",
163+
ConsoleColor.DarkYellow => "\e[43m",
164+
ConsoleColor.DarkBlue => "\e[44m",
165+
ConsoleColor.DarkMagenta => "\e[45m",
166+
ConsoleColor.DarkCyan => "\e[46m",
167+
ConsoleColor.Gray => "\e[47m",
168168
_ => DefaultBackgroundColor // Use default background color
169169
};
170170
}

src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/AnsiParserTests.cs

+28-28
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ namespace Microsoft.Extensions.Logging.Console.Test
1111
{
1212
public class AnsiParserTests
1313
{
14-
private const char EscapeChar = '\x1B';
14+
private const char EscapeChar = '\e';
1515

1616
[Theory]
1717
[InlineData(1, "No Color", "No Color")]
18-
[InlineData(2, "\x1B[41mColored\x1B[49mNo Color", "No Color")]
19-
[InlineData(2, "\x1B[41m\x1B[1m\x1B[31mmColored\x1B[39m\x1B[49mNo Color", "No Color")]
18+
[InlineData(2, "\e[41mColored\e[49mNo Color", "No Color")]
19+
[InlineData(2, "\e[41m\e[1m\e[31mmColored\e[39m\e[49mNo Color", "No Color")]
2020
public void Parse_CheckTimesWrittenToConsole(int numSegments, string message, string lastSegment)
2121
{
2222
// Arrange
@@ -151,33 +151,33 @@ public void Parse_RepeatedColorChange_PicksLastSet()
151151

152152
[Theory]
153153
// supported
154-
[InlineData("\x1B[77mInfo", "Info")]
155-
[InlineData("\x1B[77m\x1B[1m\x1B[2m\x1B[0mInfo\x1B[1m", "Info")]
156-
[InlineData("\x1B[7mInfo", "Info")]
157-
[InlineData("\x1B[40m\x1B[1m\x1B[33mwarn\x1B[39m\x1B[22m\x1B[49m:", "warn", ":")]
154+
[InlineData("\e[77mInfo", "Info")]
155+
[InlineData("\e[77m\e[1m\e[2m\e[0mInfo\e[1m", "Info")]
156+
[InlineData("\e[7mInfo", "Info")]
157+
[InlineData("\e[40m\e[1m\e[33mwarn\e[39m\e[22m\e[49m:", "warn", ":")]
158158
// unsupported: skips
159-
[InlineData("Info\x1B[77m:", "Info", ":")]
160-
[InlineData("Info\x1B[7m:", "Info", ":")]
159+
[InlineData("Info\e[77m:", "Info", ":")]
160+
[InlineData("Info\e[7m:", "Info", ":")]
161161
// treats as content
162-
[InlineData("\x1B", "\x1B")]
163-
[InlineData("\x1B ", "\x1B ")]
164-
[InlineData("\x1Bm", "\x1Bm")]
165-
[InlineData("\x1B m", "\x1B m")]
166-
[InlineData("\x1Bxym", "\x1Bxym")]
167-
[InlineData("\x1B[", "\x1B[")]
168-
[InlineData("\x1B[m", "\x1B[m")]
169-
[InlineData("\x1B[ ", "\x1B[ ")]
170-
[InlineData("\x1B[ m", "\x1B[ m")]
171-
[InlineData("\x1B[xym", "\x1B[xym")]
172-
[InlineData("\x1B[7777m", "\x1B[7777m")]
173-
[InlineData("\x1B\x1B\x1B", "\x1B\x1B\x1B")]
174-
[InlineData("Message\x1B\x1B\x1B", "Message\x1B\x1B\x1B")]
175-
[InlineData("\x1B\x1BMessage\x1B", "\x1B\x1BMessage\x1B")]
176-
[InlineData("\x1B\x1B\x1BMessage", "\x1B\x1B\x1BMessage")]
177-
[InlineData("Message\x1B ", "Message\x1B ")]
178-
[InlineData("\x1BmMessage", "\x1BmMessage")]
179-
[InlineData("\x1B[77m\x1B m\x1B[40m", "\x1B m")]
180-
[InlineData("\x1B mMessage\x1Bxym", "\x1B mMessage\x1Bxym")]
162+
[InlineData("\e", "\e")]
163+
[InlineData("\e ", "\e ")]
164+
[InlineData("\em", "\em")]
165+
[InlineData("\e m", "\e m")]
166+
[InlineData("\exym", "\exym")]
167+
[InlineData("\e[", "\e[")]
168+
[InlineData("\e[m", "\e[m")]
169+
[InlineData("\e[ ", "\e[ ")]
170+
[InlineData("\e[ m", "\e[ m")]
171+
[InlineData("\e[xym", "\e[xym")]
172+
[InlineData("\e[7777m", "\e[7777m")]
173+
[InlineData("\e\e\e", "\e\e\e")]
174+
[InlineData("Message\e\e\e", "Message\e\e\e")]
175+
[InlineData("\e\eMessage\e", "\e\eMessage\e")]
176+
[InlineData("\e\e\eMessage", "\e\e\eMessage")]
177+
[InlineData("Message\e ", "Message\e ")]
178+
[InlineData("\emMessage", "\emMessage")]
179+
[InlineData("\e[77m\e m\e[40m", "\e m")]
180+
[InlineData("\e mMessage\exym", "\e mMessage\exym")]
181181
public void Parse_ValidSupportedOrUnsupportedCodesInMessage_MessageParsedSuccessfully(string messageWithUnsupportedCode, params string[] output)
182182
{
183183
// Arrange

src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/TextWriterExtensionsTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void WriteColoredMessage_WithForegroundEscapeCode_AndNoBackgroundColorSpe
1616
var message = "Request received";
1717
var expectedMessage = AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.DarkGreen)
1818
+ message
19-
+ "\x1B[39m\x1B[22m"; //resets foreground color
19+
+ "\e[39m\e[22m"; //resets foreground color
2020
var textWriter = new StringWriter();
2121

2222
// Act
@@ -33,7 +33,7 @@ public void WriteColoredMessage_WithBackgroundEscapeCode_AndNoForegroundColorSpe
3333
var message = "Request received";
3434
var expectedMessage = AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.Red)
3535
+ message
36-
+ "\x1B[49m"; //resets background color
36+
+ "\e[49m"; //resets background color
3737
var textWriter = new StringWriter();
3838

3939
// Act
@@ -51,8 +51,8 @@ public void WriteColoredMessage_InOrder_WhenBothForegroundOrBackgroundColorsSpec
5151
var expectedMessage = AnsiParser.GetBackgroundColorEscapeCode(ConsoleColor.Red)
5252
+ AnsiParser.GetForegroundColorEscapeCode(ConsoleColor.DarkGreen)
5353
+ "Request received"
54-
+ "\x1B[39m\x1B[22m" //resets foreground color
55-
+ "\x1B[49m"; //resets background color
54+
+ "\e[39m\e[22m" //resets foreground color
55+
+ "\e[49m"; //resets background color
5656
var textWriter = new StringWriter();
5757

5858
// Act

src/libraries/System.Console/src/System/IO/KeyParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.IO;
88

99
internal static class KeyParser
1010
{
11-
private const char Escape = '\u001B';
11+
private const char Escape = '\e';
1212
private const char Delete = '\u007F';
1313
private const char VtSequenceEndTag = '~';
1414
private const char ModifierSeparator = ';';

src/libraries/System.Console/src/System/TerminalFormatStrings.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal sealed class TerminalFormatStrings
4646
/// doesn't contain it (as appears to be the case with e.g. screen and tmux on Ubuntu), at the risk
4747
/// of outputting the sequence on some terminal that's not compatible.
4848
/// </remarks>
49-
public const string CursorPositionReport = "\x1B[6n";
49+
public const string CursorPositionReport = "\e[6n";
5050
/// <summary>
5151
/// The dictionary of keystring to ConsoleKeyInfo.
5252
/// Only some members of the ConsoleKeyInfo are used; in particular, the actual char is ignored.
@@ -210,13 +210,13 @@ private static string GetTitle(TermInfo.Database db)
210210
case "linux":
211211
case "rxvt":
212212
case "xterm":
213-
return "\x1B]0;%p1%s\x07";
213+
return "\e]0;%p1%s\x07";
214214
case "cygwin":
215-
return "\x1B];%p1%s\x07";
215+
return "\e];%p1%s\x07";
216216
case "konsole":
217-
return "\x1B]30;%p1%s\x07";
217+
return "\e]30;%p1%s\x07";
218218
case "screen":
219-
return "\x1Bk%p1%s\x1B\\";
219+
return "\ek%p1%s\e\\";
220220
default:
221221
return string.Empty;
222222
}

0 commit comments

Comments
 (0)