From 468a0f38a53f9b077e0c31c2747448b5e4414ddb Mon Sep 17 00:00:00 2001 From: Austin Wise Date: Fri, 5 Feb 2016 12:55:09 -0800 Subject: [PATCH 1/2] 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. --- src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs | 12 +++++++++--- src/Microsoft.DotNet.Cli.Utils/Command.cs | 6 ++++-- src/Microsoft.DotNet.Cli.Utils/ICommand.cs | 4 ++-- src/Microsoft.DotNet.Cli.Utils/Reporter.cs | 9 ++++++++- src/dotnet/Program.cs | 12 ++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs b/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs index 7d5485095..7401af673 100644 --- a/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs +++ b/src/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs @@ -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(); } } } diff --git a/src/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Microsoft.DotNet.Cli.Utils/Command.cs index 1480c2c7f..fdfd79833 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Command.cs @@ -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 { diff --git a/src/Microsoft.DotNet.Cli.Utils/ICommand.cs b/src/Microsoft.DotNet.Cli.Utils/ICommand.cs index 1b7cde7b6..9f1cfe3a2 100644 --- a/src/Microsoft.DotNet.Cli.Utils/ICommand.cs +++ b/src/Microsoft.DotNet.Cli.Utils/ICommand.cs @@ -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 handler); diff --git a/src/Microsoft.DotNet.Cli.Utils/Reporter.cs b/src/Microsoft.DotNet.Cli.Utils/Reporter.cs index c27314fc5..67d6220a3 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Reporter.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Reporter.cs @@ -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); + } } } } diff --git a/src/dotnet/Program.cs b/src/dotnet/Program.cs index 4d4e0acff..e2de0504c 100644 --- a/src/dotnet/Program.cs +++ b/src/dotnet/Program.cs @@ -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() : 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 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() From 6957303a13313448fbb211bcdaa6a337d633424f Mon Sep 17 00:00:00 2001 From: Austin Wise Date: Fri, 12 Feb 2016 15:58:35 -0800 Subject: [PATCH 2/2] Fix build break on Windows. --- src/Microsoft.DotNet.Cli.Utils/Command.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Microsoft.DotNet.Cli.Utils/Command.cs index fdfd79833..27760424f 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Command.cs @@ -168,7 +168,7 @@ namespace Microsoft.DotNet.Cli.Utils if (to == null) { _stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine); - _process.StartInfo.Environment[CommandContext.Variables.AnsiPassThru] = ansiPassThrough.ToString(); + EnvironmentVariable(CommandContext.Variables.AnsiPassThru, ansiPassThrough.ToString()); } else { @@ -186,7 +186,7 @@ namespace Microsoft.DotNet.Cli.Utils if (to == null) { _stdErr.ForwardTo(writeLine: Reporter.Error.WriteLine); - _process.StartInfo.Environment[CommandContext.Variables.AnsiPassThru] = ansiPassThrough.ToString(); + EnvironmentVariable(CommandContext.Variables.AnsiPassThru, ansiPassThrough.ToString()); } else {