diff --git a/scripts/Microsoft.DotNet.Cli.Build.Framework/AnsiConsole.cs b/scripts/Microsoft.DotNet.Cli.Build.Framework/AnsiConsole.cs index 6c8118bdd..7f3e26852 100644 --- a/scripts/Microsoft.DotNet.Cli.Build.Framework/AnsiConsole.cs +++ b/scripts/Microsoft.DotNet.Cli.Build.Framework/AnsiConsole.cs @@ -11,30 +11,37 @@ namespace Microsoft.DotNet.Cli.Build.Framework private AnsiConsole(TextWriter writer) { Writer = writer; + OriginalForegroundColor = Console.ForegroundColor; } - + private int _boldRecursion; - + public static AnsiConsole GetOutput() { return new AnsiConsole(Console.Out); } - + public static AnsiConsole GetError() { return new AnsiConsole(Console.Error); } - + public TextWriter Writer { get; } - + public ConsoleColor OriginalForegroundColor { get; } - + private void SetColor(ConsoleColor color) { - Console.ForegroundColor = (ConsoleColor)(((int)Console.ForegroundColor & 0x08) | ((int)color & 0x07)); - } + const int Light = 0x08; + int c = (int)color; + Console.ForegroundColor = + c < 0 ? color : // unknown, just use it + _boldRecursion > 0 ? (ConsoleColor)(c & ~Light) : // ensure color is dark + (ConsoleColor)(c | Light); // ensure color is light + } + private void SetBold(bool bold) { _boldRecursion += bold ? 1 : -1; @@ -42,11 +49,19 @@ namespace Microsoft.DotNet.Cli.Build.Framework { return; } - - Console.ForegroundColor = (ConsoleColor)((int)Console.ForegroundColor ^ 0x08); + + // switches on _boldRecursion to handle boldness + SetColor(Console.ForegroundColor); } public void WriteLine(string message) + { + Write(message); + Writer.WriteLine(); + } + + + public void Write(string message) { var escapeScan = 0; for (;;) @@ -68,14 +83,14 @@ namespace Microsoft.DotNet.Cli.Build.Framework { endIndex += 1; } - + var text = message.Substring(escapeScan, escapeIndex - escapeScan); Writer.Write(text); if (endIndex == message.Length) { break; } - + switch (message[endIndex]) { case 'm': @@ -121,11 +136,10 @@ namespace Microsoft.DotNet.Cli.Build.Framework } break; } - + escapeScan = endIndex + 1; } } - Writer.WriteLine(); } } } diff --git a/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs b/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs index 258db5dfd..f9fc34f14 100644 --- a/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs +++ b/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs @@ -33,7 +33,13 @@ namespace Microsoft.DotNet.Cli.Utils private void SetColor(ConsoleColor color) { - Console.ForegroundColor = (ConsoleColor)(((int)Console.ForegroundColor & 0x08) | ((int)color & 0x07)); + const int Light = 0x08; + int c = (int)color; + + Console.ForegroundColor = + c < 0 ? color : // unknown, just use it + _boldRecursion > 0 ? (ConsoleColor)(c & ~Light) : // ensure color is dark + (ConsoleColor)(c | Light); // ensure color is light } private void SetBold(bool bold) @@ -43,8 +49,9 @@ namespace Microsoft.DotNet.Cli.Utils { return; } - - Console.ForegroundColor = (ConsoleColor)((int)Console.ForegroundColor ^ 0x08); + + // switches on _boldRecursion to handle boldness + SetColor(Console.ForegroundColor); } public void WriteLine(string message)