update with @stephentoub 's suggested changes

This commit is contained in:
Bryan 2016-02-24 10:51:36 -08:00
parent b2fab6df02
commit a986a89dcc
2 changed files with 38 additions and 17 deletions

View file

@ -11,30 +11,37 @@ namespace Microsoft.DotNet.Cli.Build.Framework
private AnsiConsole(TextWriter writer) private AnsiConsole(TextWriter writer)
{ {
Writer = writer; Writer = writer;
OriginalForegroundColor = Console.ForegroundColor; OriginalForegroundColor = Console.ForegroundColor;
} }
private int _boldRecursion; private int _boldRecursion;
public static AnsiConsole GetOutput() public static AnsiConsole GetOutput()
{ {
return new AnsiConsole(Console.Out); return new AnsiConsole(Console.Out);
} }
public static AnsiConsole GetError() public static AnsiConsole GetError()
{ {
return new AnsiConsole(Console.Error); return new AnsiConsole(Console.Error);
} }
public TextWriter Writer { get; } public TextWriter Writer { get; }
public ConsoleColor OriginalForegroundColor { get; } public ConsoleColor OriginalForegroundColor { get; }
private void SetColor(ConsoleColor color) 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) private void SetBold(bool bold)
{ {
_boldRecursion += bold ? 1 : -1; _boldRecursion += bold ? 1 : -1;
@ -42,11 +49,19 @@ namespace Microsoft.DotNet.Cli.Build.Framework
{ {
return; return;
} }
Console.ForegroundColor = (ConsoleColor)((int)Console.ForegroundColor ^ 0x08); // switches on _boldRecursion to handle boldness
SetColor(Console.ForegroundColor);
} }
public void WriteLine(string message) public void WriteLine(string message)
{
Write(message);
Writer.WriteLine();
}
public void Write(string message)
{ {
var escapeScan = 0; var escapeScan = 0;
for (;;) for (;;)
@ -68,14 +83,14 @@ namespace Microsoft.DotNet.Cli.Build.Framework
{ {
endIndex += 1; endIndex += 1;
} }
var text = message.Substring(escapeScan, escapeIndex - escapeScan); var text = message.Substring(escapeScan, escapeIndex - escapeScan);
Writer.Write(text); Writer.Write(text);
if (endIndex == message.Length) if (endIndex == message.Length)
{ {
break; break;
} }
switch (message[endIndex]) switch (message[endIndex])
{ {
case 'm': case 'm':
@ -121,11 +136,10 @@ namespace Microsoft.DotNet.Cli.Build.Framework
} }
break; break;
} }
escapeScan = endIndex + 1; escapeScan = endIndex + 1;
} }
} }
Writer.WriteLine();
} }
} }
} }

View file

@ -33,7 +33,13 @@ namespace Microsoft.DotNet.Cli.Utils
private void SetColor(ConsoleColor color) 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) private void SetBold(bool bold)
@ -43,8 +49,9 @@ namespace Microsoft.DotNet.Cli.Utils
{ {
return; return;
} }
Console.ForegroundColor = (ConsoleColor)((int)Console.ForegroundColor ^ 0x08); // switches on _boldRecursion to handle boldness
SetColor(Console.ForegroundColor);
} }
public void WriteLine(string message) public void WriteLine(string message)