Skip to content

Commit

Permalink
(cake-buildGH-2852) Use the default background color when writing to …
Browse files Browse the repository at this point in the history
…the Console
  • Loading branch information
augustoproiete committed Feb 7, 2021
1 parent 6641a9f commit c7f1ee1
Show file tree
Hide file tree
Showing 5 changed files with 40 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
5 changes: 4 additions & 1 deletion src/Cake.Core/Diagnostics/Console/AnsiConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using Cake.Core.Diagnostics.Console;
using Cake.Core.Diagnostics.Formatting;

namespace Cake.Core.Diagnostics
Expand All @@ -24,7 +25,8 @@ public AnsiConsoleRenderer(IConsole console)

_ansiLookup = new Dictionary<ConsoleColor, string>
{
{ ConsoleColor.DarkGray, $"\u001b[30;1m" }, // Bright black
{ ConsoleColorEx.Default, 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 +46,7 @@ public AnsiConsoleRenderer(IConsole console)

_ansiBackgroundLookup = new Dictionary<ConsoleColor, string>
{
{ ConsoleColorEx.Default, 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
9 changes: 9 additions & 0 deletions src/Cake.Core/Diagnostics/Console/ConsoleColorEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Cake.Core.Diagnostics.Console
{
internal static class ConsoleColorEx
{
public const ConsoleColor Default = (ConsoleColor)(-1);
}
}
5 changes: 3 additions & 2 deletions src/Cake.Core/Diagnostics/Console/ConsolePalette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using Cake.Core.Diagnostics.Console;

namespace Cake.Core.Diagnostics
{
Expand All @@ -26,9 +27,9 @@ internal sealed class ConsolePalette
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 = ConsoleColorEx.Default;
}

return new Dictionary<LogLevel, ConsolePalette>
Expand Down
23 changes: 19 additions & 4 deletions src/Cake.Core/Diagnostics/Console/ConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using Cake.Core.Diagnostics.Console;
using Cake.Core.Diagnostics.Formatting;

namespace Cake.Core.Diagnostics
Expand Down Expand Up @@ -62,13 +63,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 != ConsoleColorEx.Default)
{
_console.BackgroundColor = palette.ArgumentBackground;
}

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

if (palette.Foreground != ConsoleColorEx.Default)
{
_console.ForegroundColor = palette.Foreground;
}
}
}
}
Expand Down

0 comments on commit c7f1ee1

Please sign in to comment.