Fix color printing and verbosity.

Only apply the ANSI passthrough for child commands. Otherwise the
escape codes are printed to the console on Windows

Make Report.Write process escape codes so that forwarded output will have
its escape codes processed.

Also make verbose be inherited by all child processes.
This commit is contained in:
Austin Wise 2016-02-05 12:55:09 -08:00
parent f273ea4f7d
commit 468a0f38a5
5 changed files with 29 additions and 14 deletions

View file

@ -51,12 +51,19 @@ namespace Microsoft.DotNet.Cli.Utils
Console.ForegroundColor = (ConsoleColor)((int)Console.ForegroundColor ^ 0x08);
}
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);
_process.StartInfo.Environment[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);
_process.StartInfo.Environment[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()