Merge pull request #1212 from AustinWise/fixColors

Fix color printing and --verbose
This commit is contained in:
Piotr Puszkiewicz 2016-02-12 23:39:49 -08:00
commit e8ef94e93f
5 changed files with 29 additions and 14 deletions

View file

@ -53,10 +53,17 @@ namespace Microsoft.DotNet.Cli.Utils
}
public void WriteLine(string message)
{
Write(message);
Writer.WriteLine();
}
public void Write(string message)
{
if (!_useConsoleColor)
{
Writer.WriteLine(message);
Writer.Write(message);
return;
}
@ -137,7 +144,6 @@ namespace Microsoft.DotNet.Cli.Utils
escapeScan = endIndex + 1;
}
}
Writer.WriteLine();
}
}
}

View file

@ -160,7 +160,7 @@ namespace Microsoft.DotNet.Cli.Utils
return this;
}
public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false)
public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
ThrowIfRunning();
if (!onlyIfVerbose || CommandContext.IsVerbose())
@ -168,6 +168,7 @@ namespace Microsoft.DotNet.Cli.Utils
if (to == null)
{
_stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine);
EnvironmentVariable(CommandContext.Variables.AnsiPassThru, ansiPassThrough.ToString());
}
else
{
@ -177,7 +178,7 @@ namespace Microsoft.DotNet.Cli.Utils
return this;
}
public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false)
public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
ThrowIfRunning();
if (!onlyIfVerbose || CommandContext.IsVerbose())
@ -185,6 +186,7 @@ namespace Microsoft.DotNet.Cli.Utils
if (to == null)
{
_stdErr.ForwardTo(writeLine: Reporter.Error.WriteLine);
EnvironmentVariable(CommandContext.Variables.AnsiPassThru, ansiPassThrough.ToString());
}
else
{

View file

@ -18,9 +18,9 @@ namespace Microsoft.DotNet.Cli.Utils
ICommand CaptureStdErr();
ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false);
ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true);
ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false);
ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true);
ICommand OnOutputLine(Action<string> handler);

View file

@ -55,7 +55,14 @@ namespace Microsoft.DotNet.Cli.Utils
{
lock (_lock)
{
_console?.Writer?.Write(message);
if (CommandContext.ShouldPassAnsiCodesThrough())
{
_console?.Writer?.Write(message);
}
else
{
_console?.Write(message);
}
}
}
}

View file

@ -74,7 +74,7 @@ Common Commands:
{
// CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.
var verbose = false;
bool? verbose = null;
var success = true;
var command = string.Empty;
var lastArg = 0;
@ -114,6 +114,11 @@ Common Commands:
var appArgs = (lastArg + 1) >= args.Length ? Enumerable.Empty<string>() : args.Skip(lastArg + 1).ToArray();
if (verbose.HasValue)
{
Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString());
}
if (string.IsNullOrEmpty(command) || command.Equals("help", StringComparison.OrdinalIgnoreCase))
{
return RunHelpCommand(appArgs);
@ -140,15 +145,10 @@ Common Commands:
Func<string[], int> builtIn;
if (builtIns.TryGetValue(command, out builtIn))
{
// mimic the env variable
Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString());
Environment.SetEnvironmentVariable(CommandContext.Variables.AnsiPassThru, bool.TrueString);
return builtIn(appArgs.ToArray());
}
return Command.Create("dotnet-" + command, appArgs, FrameworkConstants.CommonFrameworks.DnxCore50)
.EnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString())
.EnvironmentVariable(CommandContext.Variables.AnsiPassThru, bool.TrueString)
.ForwardStdErr()
.ForwardStdOut()
.Execute()