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..27760424f 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); + 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 { 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()