Skip to content

Commit 32480f5

Browse files
committed
Use \e instead of \u001B or \x1B
1 parent 0880c28 commit 32480f5

File tree

11 files changed

+111
-111
lines changed

11 files changed

+111
-111
lines changed

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

Lines changed: 28 additions & 28 deletions
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

Lines changed: 28 additions & 28 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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
}

src/libraries/System.Console/tests/KeyParserTests.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class KeyParserTests
4242
yield return ('.', ConsoleKey.OemPeriod);
4343
yield return (',', ConsoleKey.OemComma);
4444

45-
yield return ('\u001B', ConsoleKey.Escape);
45+
yield return ('\e', ConsoleKey.Escape);
4646

4747
for (char i = '0'; i <= '9'; i++)
4848
{
@@ -212,7 +212,7 @@ public void KeysAreProperlyMapped(TerminalData terminalData, byte[] recordedByte
212212
yield return (GetString(33), ConsoleKey.F19);
213213
yield return (GetString(34), ConsoleKey.F20);
214214

215-
static string GetString(int i) => $"\u001B[{i}~";
215+
static string GetString(int i) => $"\e[{i}~";
216216
}
217217
}
218218

@@ -223,7 +223,7 @@ public static IEnumerable<object[]> VTSequencesArguments
223223
[MemberData(nameof(VTSequencesArguments))]
224224
public void VTSequencesAreProperlyMapped(TerminalData terminalData, string input, ConsoleKey expectedKey)
225225
{
226-
if (terminalData is RxvtUnicode && input == "\u001B[4~" && expectedKey == ConsoleKey.End)
226+
if (terminalData is RxvtUnicode && input == "\e[4~" && expectedKey == ConsoleKey.End)
227227
{
228228
expectedKey = ConsoleKey.Select; // rxvt binds this key to Select in Terminfo and uses "^[[8~" for End key
229229
}
@@ -239,10 +239,10 @@ public void VTSequencesAreProperlyMapped(TerminalData terminalData, string input
239239
{
240240
get
241241
{
242-
yield return ("\u001BOa", ConsoleKey.UpArrow);
243-
yield return ("\u001BOb", ConsoleKey.DownArrow);
244-
yield return ("\u001BOc", ConsoleKey.RightArrow);
245-
yield return ("\u001BOd", ConsoleKey.LeftArrow);
242+
yield return ("\eOa", ConsoleKey.UpArrow);
243+
yield return ("\eOb", ConsoleKey.DownArrow);
244+
yield return ("\eOc", ConsoleKey.RightArrow);
245+
yield return ("\eOd", ConsoleKey.LeftArrow);
246246
}
247247
}
248248

@@ -272,9 +272,9 @@ public void ExtendedStringCodePath()
272272
// Ctrl+Backspace
273273
yield return ("\b", new[] { new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, false, true) });
274274
// Alt+Backspace
275-
yield return ("\u001B\u007F", new[] { new ConsoleKeyInfo((char)0x7F, ConsoleKey.Backspace, false, true, false) });
275+
yield return ("\e\u007F", new[] { new ConsoleKeyInfo((char)0x7F, ConsoleKey.Backspace, false, true, false) });
276276
// Ctrl+Alt+Backspace
277-
yield return ("\u001B\b", new[] { new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, true, true) });
277+
yield return ("\e\b", new[] { new ConsoleKeyInfo('\b', ConsoleKey.Backspace, false, true, true) });
278278
// Enter
279279
yield return ("\r", new[] { new ConsoleKeyInfo('\r', ConsoleKey.Enter, false, false, false) });
280280
// Ctrl+Enter
@@ -283,18 +283,18 @@ public void ExtendedStringCodePath()
283283
// Escape key pressed multiple times
284284
for (int i = 1; i <= 5; i++)
285285
{
286-
yield return (new string('\u001B', i), Enumerable.Repeat(new ConsoleKeyInfo('\u001B', ConsoleKey.Escape, false, false, false), i).ToArray());
286+
yield return (new string('\e', i), Enumerable.Repeat(new ConsoleKeyInfo('\e', ConsoleKey.Escape, false, false, false), i).ToArray());
287287
}
288288

289289
// Home key (^[[H) followed by H key
290-
yield return ("\u001B[HH", new[]
290+
yield return ("\e[HH", new[]
291291
{
292292
new ConsoleKeyInfo(default, ConsoleKey.Home, false, false, false),
293293
new ConsoleKeyInfo('H', ConsoleKey.H, true, false, false)
294294
});
295295

296296
// escape sequence (F12 '^[[24~') followed by an extra tylde:
297-
yield return ($"\u001B[24~~", new[]
297+
yield return ($"\e[24~~", new[]
298298
{
299299
new ConsoleKeyInfo(default, ConsoleKey.F12, false, false, false),
300300
new ConsoleKeyInfo('~', default, false, false, false),
@@ -304,9 +304,9 @@ public void ExtendedStringCodePath()
304304
// Invalid modifiers (valid values are <2, 8>)
305305
foreach (int invalidModifier in new[] { 0, 1, 9 })
306306
{
307-
yield return ($"\u001B[1;{invalidModifier}H", new[]
307+
yield return ($"\e[1;{invalidModifier}H", new[]
308308
{
309-
new ConsoleKeyInfo('\u001B', ConsoleKey.Escape, false, false, false),
309+
new ConsoleKeyInfo('\e', ConsoleKey.Escape, false, false, false),
310310
new ConsoleKeyInfo('[', default, false, false, false),
311311
new ConsoleKeyInfo('1', ConsoleKey.D1, false, false, false),
312312
new ConsoleKeyInfo(';', default, false, false, false),
@@ -317,29 +317,29 @@ public void ExtendedStringCodePath()
317317
// Invalid ID (valid values are <1, 34> except of 9, 16, 22, 27, 30 and 35)
318318
foreach (int invalidId in new[] { 16, 22, 27, 30, 35, 36, 77, 99 })
319319
{
320-
yield return ($"\u001B[{invalidId}~", new[]
320+
yield return ($"\e[{invalidId}~", new[]
321321
{
322-
new ConsoleKeyInfo('\u001B', ConsoleKey.Escape, false, false, false),
322+
new ConsoleKeyInfo('\e', ConsoleKey.Escape, false, false, false),
323323
new ConsoleKeyInfo('[', default, false, false, false),
324324
new ConsoleKeyInfo((char)('0' + invalidId / 10), ConsoleKey.D0 + invalidId / 10, false, false, false),
325325
new ConsoleKeyInfo((char)('0' + invalidId % 10), ConsoleKey.D0 + invalidId % 10, false, false, false),
326326
new ConsoleKeyInfo('~', default, false, false, false),
327327
});
328328
}
329329
// too long ID (more than 2 digits)
330-
yield return ($"\u001B[111~", new[]
330+
yield return ($"\e[111~", new[]
331331
{
332-
new ConsoleKeyInfo('\u001B', ConsoleKey.Escape, false, false, false),
332+
new ConsoleKeyInfo('\e', ConsoleKey.Escape, false, false, false),
333333
new ConsoleKeyInfo('[', default, false, false, false),
334334
new ConsoleKeyInfo('1', ConsoleKey.D1, false, false, false),
335335
new ConsoleKeyInfo('1', ConsoleKey.D1, false, false, false),
336336
new ConsoleKeyInfo('1', ConsoleKey.D1, false, false, false),
337337
new ConsoleKeyInfo('~', default, false, false, false),
338338
});
339339
// missing closing tag (tylde):
340-
yield return ($"\u001B[24", new[]
340+
yield return ($"\e[24", new[]
341341
{
342-
new ConsoleKeyInfo('\u001B', ConsoleKey.Escape, false, false, false),
342+
new ConsoleKeyInfo('\e', ConsoleKey.Escape, false, false, false),
343343
new ConsoleKeyInfo('[', default, false, false, false),
344344
new ConsoleKeyInfo('2', ConsoleKey.D2, false, false, false),
345345
new ConsoleKeyInfo('4', ConsoleKey.D4, false, false, false),
@@ -386,7 +386,7 @@ public void NewLineEscapeSequenceProducesCharacter()
386386
{
387387
XTermData xTerm = new();
388388

389-
ConsoleKeyInfo consoleKeyInfo = Parse("\u001BOM".ToCharArray(), xTerm.TerminalDb, xTerm.Verase, 3);
389+
ConsoleKeyInfo consoleKeyInfo = Parse("\eOM".ToCharArray(), xTerm.TerminalDb, xTerm.Verase, 3);
390390

391391
Assert.Equal(ConsoleKey.Enter, consoleKeyInfo.Key);
392392
Assert.Equal('\r', consoleKeyInfo.KeyChar);
@@ -398,7 +398,7 @@ public void BackTabEscapeSequence()
398398
{
399399
XTermData xTerm = new();
400400

401-
ConsoleKeyInfo consoleKeyInfo = Parse("\u001B[Z".ToCharArray(), xTerm.TerminalDb, xTerm.Verase, 3);
401+
ConsoleKeyInfo consoleKeyInfo = Parse("\e[Z".ToCharArray(), xTerm.TerminalDb, xTerm.Verase, 3);
402402

403403
Assert.Equal(ConsoleKey.Tab, consoleKeyInfo.Key);
404404
Assert.Equal(default, consoleKeyInfo.KeyChar);

0 commit comments

Comments
 (0)