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)
{
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();
}
}
}