Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Use standard ansi color codes #456

Merged
merged 1 commit into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/Microsoft.Extensions.Logging.Console/Internal/AnsiLogConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public void Write(string message, ConsoleColor? background, ConsoleColor? foregr

if (foreground.HasValue)
{
_outputBuilder.Append("\x1B[39m"); // reset to default foreground color
_outputBuilder.Append("\x1B[39m\x1B[22m"); // reset to default foreground color
}

if (background.HasValue)
{
_outputBuilder.Append("\x1B[0m"); // reset to the background color
_outputBuilder.Append("\x1B[49m"); // reset to the background color
}
}

Expand Down Expand Up @@ -79,21 +79,21 @@ private static string GetForegroundColorEscapeCode(ConsoleColor color)
case ConsoleColor.Gray:
return "\x1B[37m";
case ConsoleColor.Red:
return "\x1B[91m";
return "\x1B[1m\x1B[31m";
case ConsoleColor.Green:
return "\x1B[92m";
return "\x1B[1m\x1B[32m";
case ConsoleColor.Yellow:
return "\x1B[93m";
return "\x1B[1m\x1B[33m";
case ConsoleColor.Blue:
return "\x1B[94m";
return "\x1B[1m\x1B[34m";
case ConsoleColor.Magenta:
return "\x1B[95m";
return "\x1B[1m\x1B[35m";
case ConsoleColor.Cyan:
return "\x1B[96m";
return "\x1B[1m\x1B[36m";
case ConsoleColor.White:
return "\x1B[97m";
return "\x1B[1m\x1B[37m";
default:
return "\x1B[39m"; // default foreground color
return "\x1B[39m\x1B[22m"; // default foreground color
}
}

Expand All @@ -102,23 +102,23 @@ private static string GetBackgroundColorEscapeCode(ConsoleColor color)
switch (color)
{
case ConsoleColor.Black:
return "\x1B[100m";
return "\x1B[40m";
case ConsoleColor.Red:
return "\x1B[101m";
return "\x1B[41m";
case ConsoleColor.Green:
return "\x1B[102m";
return "\x1B[42m";
case ConsoleColor.Yellow:
return "\x1B[103m";
return "\x1B[43m";
case ConsoleColor.Blue:
return "\x1B[104m";
return "\x1B[44m";
case ConsoleColor.Magenta:
return "\x1B[105m";
return "\x1B[45m";
case ConsoleColor.Cyan:
return "\x1B[106m";
return "\x1B[46m";
case ConsoleColor.White:
return "\x1B[107m";
return "\x1B[47m";
default:
return "\x1B[0m"; // Use default background color
return "\x1B[49m"; // Use default background color
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions test/Microsoft.Extensions.Logging.Test/AnsiLogConsoleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void WritesMessage_WithForegroundEscapeCode_AndNoBackgroundColorSpecified
var message = "Request received";
var expectedMessage = GetForegroundColorEscapeCode(ConsoleColor.DarkGreen)
+ message
+ "\x1B[39m"; //resets foreground color
+ "\x1B[39m\x1B[22m"; //resets foreground color

// Act
console.WriteLine(message, background: null, foreground: ConsoleColor.DarkGreen);
Expand All @@ -106,7 +106,7 @@ public void WritesMessage_WithBackgroundEscapeCode_AndNoForegroundColorSpecified
var message = "Request received";
var expectedMessage = GetBackgroundColorEscapeCode(ConsoleColor.Red)
+ message
+ "\x1B[0m"; //resets background color
+ "\x1B[49m"; //resets background color

// Act
console.WriteLine(message, background: ConsoleColor.Red, foreground: null);
Expand All @@ -126,8 +126,8 @@ public void WriteMessage_InOrder_WhenBothForegroundOrBackgroundColorsSpecified()
var expectedMessage = GetBackgroundColorEscapeCode(ConsoleColor.Red)
+ GetForegroundColorEscapeCode(ConsoleColor.DarkGreen)
+ "Request received"
+ "\x1B[39m" //resets foreground color
+ "\x1B[0m" //resets background color
+ "\x1B[39m\x1B[22m" //resets foreground color
+ "\x1B[49m" //resets background color
+ Environment.NewLine;

// Act
Expand Down Expand Up @@ -166,10 +166,8 @@ private static string GetForegroundColorEscapeCode(ConsoleColor color)
return "\x1B[33m";
case ConsoleColor.Gray:
return "\x1B[37m";
case ConsoleColor.White:
return "\x1B[97m";
default:
return "\x1B[37m";
return "\x1B[39m";
}
}

Expand All @@ -178,7 +176,7 @@ private static string GetBackgroundColorEscapeCode(ConsoleColor color)
switch (color)
{
case ConsoleColor.Red:
return "\x1B[101m";
return "\x1B[41m";
default:
return "\x1B[49m";
}
Expand Down