Skip to content

Commit

Permalink
Merge pull request cake-build#3094 from augustoproiete-forks/use-defa…
Browse files Browse the repository at this point in the history
…ult-background-color-of-console-when-ansi

Use default background color of console when ansi
  • Loading branch information
devlead authored Feb 13, 2021
2 parents e6172b7 + 3c60c60 commit bfb0cb4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/Cake.Core.Tests/Unit/Diagnostics/CakeBuildLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public void Should_Not_Colorize_A_Log_Message_Containg_A_Single_Token()
}

[Theory]
[InlineData(LogLevel.Warning, "\u001b[40m\u001b[33;1mHello, \u001b[0m\u001b[40m\u001b[33;1mWorld\u001b[0m")]
[InlineData(LogLevel.Information, "\u001b[40m\u001b[37;1mHello, \u001b[0m\u001b[44m\u001b[37;1mWorld\u001b[0m")]
[InlineData(LogLevel.Verbose, "\u001b[40m\u001b[37mHello, \u001b[0m\u001b[40m\u001b[37;1mWorld\u001b[0m")]
[InlineData(LogLevel.Debug, "\u001b[40m\u001b[30;1mHello, \u001b[0m\u001b[40m\u001b[37mWorld\u001b[0m")]
[InlineData(LogLevel.Warning, "\u001b[33;1mHello, \u001b[0m\u001b[33;1mWorld\u001b[0m")]
[InlineData(LogLevel.Information, "\u001b[37;1mHello, \u001b[0m\u001b[44m\u001b[37;1mWorld\u001b[0m")]
[InlineData(LogLevel.Verbose, "\u001b[37mHello, \u001b[0m\u001b[37;1mWorld\u001b[0m")]
[InlineData(LogLevel.Debug, "\u001b[30;1mHello, \u001b[0m\u001b[37mWorld\u001b[0m")]
public void Should_Colorize_Tokens_Correctly(LogLevel level, string expected)
{
// Given
Expand Down Expand Up @@ -266,7 +266,7 @@ public void Should_Colorize_Error_Tokens_Correctly(LogLevel level, string expect

[Theory]
[InlineData(false, "#[Black|DarkGray]Executing: if ($LASTEXITCODE -gt 0) { throw \"script failed with exit code $LASTEXITCODE\" }[/]")]
[InlineData(true, "\u001b[40m\u001b[30;1mExecuting: if ($LASTEXITCODE -gt 0) { throw \"script failed with exit code $LASTEXITCODE\" }\u001b[0m")]
[InlineData(true, "\u001b[30;1mExecuting: if ($LASTEXITCODE -gt 0) { throw \"script failed with exit code $LASTEXITCODE\" }\u001b[0m")]
public void Should_Output_Escaped_Tokens_Correctly(bool ansi, string expected)
{
// Given
Expand Down
2 changes: 2 additions & 0 deletions src/Cake.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Cake.Core
{
internal static class Constants
{
public const ConsoleColor DefaultConsoleColor = (ConsoleColor)(-1);

public static readonly Version LatestBreakingChange = new Version(0, 26, 0);
public static readonly Version LatestPotentialBreakingChange = new Version(1, 0, 0);

Expand Down
4 changes: 3 additions & 1 deletion src/Cake.Core/Diagnostics/Console/AnsiConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public AnsiConsoleRenderer(IConsole console)

_ansiLookup = new Dictionary<ConsoleColor, string>
{
{ ConsoleColor.DarkGray, $"\u001b[30;1m" }, // Bright black
{ Constants.DefaultConsoleColor, string.Empty }, // Default
{ ConsoleColor.DarkGray, "\u001b[30;1m" }, // Bright black
{ ConsoleColor.Red, "\u001b[31;1m" }, // Bright red
{ ConsoleColor.Green, "\u001b[32;1m" }, // Bright green
{ ConsoleColor.Yellow, "\u001b[33;1m" }, // Bright yellow
Expand All @@ -44,6 +45,7 @@ public AnsiConsoleRenderer(IConsole console)

_ansiBackgroundLookup = new Dictionary<ConsoleColor, string>
{
{ Constants.DefaultConsoleColor, string.Empty }, // Default
{ ConsoleColor.DarkGray, "\u001b[40;1m" }, // Bright black
{ ConsoleColor.Red, "\u001b[41;1m" }, // Bright red
{ ConsoleColor.Green, "\u001b[42;1m" }, // Bright green
Expand Down
4 changes: 2 additions & 2 deletions src/Cake.Core/Diagnostics/Console/ConsolePalette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public ConsolePalette(ConsoleColor background, ConsoleColor foreground,
public static IDictionary<LogLevel, ConsolePalette> CreateLookup(IConsole console)
{
var background = console.BackgroundColor;
if ((int)background < 0)
if ((int)background < 0 || console.SupportAnsiEscapeCodes)
{
background = ConsoleColor.Black;
background = Constants.DefaultConsoleColor;
}

return new Dictionary<LogLevel, ConsolePalette>
Expand Down
22 changes: 18 additions & 4 deletions src/Cake.Core/Diagnostics/Console/ConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ private void SetPalette(FormatToken token, ConsolePalette palette, bool colorize
{
if (colorize && token is PropertyToken)
{
_console.BackgroundColor = palette.ArgumentBackground;
_console.ForegroundColor = palette.ArgumentForeground;
if (palette.ArgumentBackground != Constants.DefaultConsoleColor)
{
_console.BackgroundColor = palette.ArgumentBackground;
}

if (palette.ArgumentForeground != Constants.DefaultConsoleColor)
{
_console.ForegroundColor = palette.ArgumentForeground;
}
}
else
{
_console.BackgroundColor = palette.Background;
_console.ForegroundColor = palette.Foreground;
if (palette.Background != Constants.DefaultConsoleColor)
{
_console.BackgroundColor = palette.Background;
}

if (palette.Foreground != Constants.DefaultConsoleColor)
{
_console.ForegroundColor = palette.Foreground;
}
}
}
}
Expand Down

0 comments on commit bfb0cb4

Please sign in to comment.