diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index 3b6cc1db73..79d49a6d07 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -102,9 +102,6 @@ System.CommandLine public static class CompletionSourceExtensions public static System.Void Add(this System.Collections.Generic.List>> completionSources, System.Func> completionsDelegate) public static System.Void Add(this System.Collections.Generic.List>> completionSources, System.String[] completions) - public static class ConsoleExtensions - public static System.Void Write(this IConsole console, System.String value) - public static System.Void WriteLine(this IConsole console, System.String value) public class Directive : Symbol .ctor(System.String name) public CliAction Action { get; set; } diff --git a/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs b/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs index 295ab476e7..ce59fdc250 100644 --- a/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs +++ b/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs @@ -69,7 +69,7 @@ private sealed class CustomCliAction : CliAction public async override Task InvokeAsync(InvocationContext context, CancellationToken cancellationToken = default) { - context.Console.WriteLine(ChildProcessWaiting); + Console.WriteLine(ChildProcessWaiting); bool infiniteDelay = context.GetValue(InfiniteDelayOption); diff --git a/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs b/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs index 0200c24157..b81cf144e7 100644 --- a/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs +++ b/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs @@ -22,8 +22,8 @@ private static int Main(string[] args) void Run(InvocationContext context) { - context.Console.WriteLine($"Bool option: {context.ParseResult.GetValue(boolOption)}"); - context.Console.WriteLine($"String option: {context.ParseResult.GetValue(stringOption)}"); + Console.WriteLine($"Bool option: {context.ParseResult.GetValue(boolOption)}"); + Console.WriteLine($"String option: {context.ParseResult.GetValue(stringOption)}"); } } } \ No newline at end of file diff --git a/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs b/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs index 1bb70d8f88..7254396fa1 100644 --- a/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs +++ b/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs @@ -11,7 +11,7 @@ command.SetAction(context => { - context.Console.Write($"The file you chose was: {context.ParseResult.GetValue(fileArgument)}"); + Console.Write($"The file you chose was: {context.ParseResult.GetValue(fileArgument)}"); }); command.Invoke(args); diff --git a/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs b/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs index ea3efd76a0..0d0933f82d 100644 --- a/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs +++ b/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs @@ -115,13 +115,13 @@ int Default(Exception exception, InvocationContext context) { if (exception is not OperationCanceledException) { - context.Console.ResetTerminalForegroundColor(); - context.Console.SetTerminalForegroundRed(); + ConsoleHelpers.ResetTerminalForegroundColor(); + ConsoleHelpers.SetTerminalForegroundRed(); context.Console.Error.Write(LocalizationResources.ExceptionHandlerHeader()); context.Console.Error.WriteLine(exception.ToString()); - context.Console.ResetTerminalForegroundColor(); + ConsoleHelpers.ResetTerminalForegroundColor(); } return errorExitCode; } diff --git a/src/System.CommandLine/ConsoleExtensions.cs b/src/System.CommandLine/ConsoleExtensions.cs deleted file mode 100644 index 2705a8e287..0000000000 --- a/src/System.CommandLine/ConsoleExtensions.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.CommandLine.IO; - -namespace System.CommandLine -{ - /// - /// Provides extension methods for . - /// - public static class ConsoleExtensions - { - /// - /// Writes the current string value to the standard output stream. - /// - /// The console to write to. - /// The value to write. - public static void Write(this IConsole console, string value) => - console.Out.Write(value); - - /// - /// Writes the current string value, followed by the current environment's line terminator, to the standard output stream. - /// - /// The console to write to. - /// The value to write. - public static void WriteLine(this IConsole console, string value) => - console.Out.WriteLine(value); - } -} \ No newline at end of file diff --git a/src/System.CommandLine/ConsoleHelpers.cs b/src/System.CommandLine/ConsoleHelpers.cs new file mode 100644 index 0000000000..2684413c89 --- /dev/null +++ b/src/System.CommandLine/ConsoleHelpers.cs @@ -0,0 +1,39 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Runtime.InteropServices; + +namespace System.CommandLine +{ + internal static class ConsoleHelpers + { + private static readonly bool ColorsAreSupported = GetColorsAreSupported(); + + private static bool GetColorsAreSupported() +#if NET7_0_OR_GREATER + => !(OperatingSystem.IsBrowser() || OperatingSystem.IsAndroid() || OperatingSystem.IsIOS() || OperatingSystem.IsTvOS()) +#else + => !(RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")) + || RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID")) + || RuntimeInformation.IsOSPlatform(OSPlatform.Create("IOS")) + || RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS"))) +#endif + && !Console.IsOutputRedirected; + + internal static void SetTerminalForegroundRed() + { + if (ColorsAreSupported) + { + Console.ForegroundColor = ConsoleColor.Red; + } + } + + internal static void ResetTerminalForegroundColor() + { + if (ColorsAreSupported) + { + Console.ResetColor(); + } + } + } +} \ No newline at end of file diff --git a/src/System.CommandLine/IO/ConsoleExtensions.cs b/src/System.CommandLine/IO/ConsoleExtensions.cs deleted file mode 100644 index 7ec5ab26b4..0000000000 --- a/src/System.CommandLine/IO/ConsoleExtensions.cs +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace System.CommandLine.IO -{ - internal static class ConsoleExtensions - { - private static bool? _isConsoleRedirectionCheckSupported; - - private static bool IsConsoleRedirectionCheckSupported - { - get - { - if (_isConsoleRedirectionCheckSupported is null) - { - try - { - var check = Console.IsOutputRedirected; - _isConsoleRedirectionCheckSupported = true; - } - - catch (PlatformNotSupportedException) - { - _isConsoleRedirectionCheckSupported = false; - } - } - - return _isConsoleRedirectionCheckSupported.Value; - } - } - - internal static void SetTerminalForegroundRed(this IConsole console) - { - if (IsConsoleRedirectionCheckSupported && - !Console.IsOutputRedirected) - { - Console.ForegroundColor = ConsoleColor.Red; - } - else if (IsConsoleRedirectionCheckSupported) - { - Console.ForegroundColor = ConsoleColor.Red; - } - } - - internal static void ResetTerminalForegroundColor(this IConsole console) - { - if (IsConsoleRedirectionCheckSupported && - !Console.IsOutputRedirected) - { - Console.ResetColor(); - } - else if (IsConsoleRedirectionCheckSupported) - { - Console.ResetColor(); - } - } - } -} \ No newline at end of file diff --git a/src/System.CommandLine/Invocation/ParseErrorResult.cs b/src/System.CommandLine/Invocation/ParseErrorResult.cs index 29367c1754..edbd513666 100644 --- a/src/System.CommandLine/Invocation/ParseErrorResult.cs +++ b/src/System.CommandLine/Invocation/ParseErrorResult.cs @@ -12,8 +12,8 @@ internal sealed class ParseErrorResultAction : CliAction { public override int Invoke(InvocationContext context) { - context.Console.ResetTerminalForegroundColor(); - context.Console.SetTerminalForegroundRed(); + ConsoleHelpers.ResetTerminalForegroundColor(); + ConsoleHelpers.SetTerminalForegroundRed(); foreach (var error in context.ParseResult.Errors) { @@ -22,7 +22,7 @@ public override int Invoke(InvocationContext context) context.Console.Error.WriteLine(); - context.Console.ResetTerminalForegroundColor(); + ConsoleHelpers.ResetTerminalForegroundColor(); new HelpOption().Action!.Invoke(context);