Skip to content

Commit 9ebcd90

Browse files
authored
Removing almost unused ConsoleExtensions (#2103)
* merge two ConsoleExtensions types into one, remove unused IConsole argument * simplify the logic * remove almost unused public methods from ConsoleExtensions, rename it to ConsoleHelpers as there are no extensions anymore * address code review feedback: use more reliable way of checking whether colors are supported: check OS first (in non-throwing way), then check IsOutputRedirected which won't throw at this point
1 parent 3b5c4be commit 9ebcd90

File tree

9 files changed

+49
-100
lines changed

9 files changed

+49
-100
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ System.CommandLine
102102
public static class CompletionSourceExtensions
103103
public static System.Void Add(this System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
104104
public static System.Void Add(this System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.String[] completions)
105-
public static class ConsoleExtensions
106-
public static System.Void Write(this IConsole console, System.String value)
107-
public static System.Void WriteLine(this IConsole console, System.String value)
108105
public class Directive : Symbol
109106
.ctor(System.String name)
110107
public CliAction Action { get; set; }

src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private sealed class CustomCliAction : CliAction
6969

7070
public async override Task<int> InvokeAsync(InvocationContext context, CancellationToken cancellationToken = default)
7171
{
72-
context.Console.WriteLine(ChildProcessWaiting);
72+
Console.WriteLine(ChildProcessWaiting);
7373

7474
bool infiniteDelay = context.GetValue(InfiniteDelayOption);
7575

src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ private static int Main(string[] args)
2222

2323
void Run(InvocationContext context)
2424
{
25-
context.Console.WriteLine($"Bool option: {context.ParseResult.GetValue(boolOption)}");
26-
context.Console.WriteLine($"String option: {context.ParseResult.GetValue(stringOption)}");
25+
Console.WriteLine($"Bool option: {context.ParseResult.GetValue(boolOption)}");
26+
Console.WriteLine($"String option: {context.ParseResult.GetValue(stringOption)}");
2727
}
2828
}
2929
}

src/System.CommandLine.Tests/TestApps/Trimming/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
command.SetAction(context =>
1313
{
14-
context.Console.Write($"The file you chose was: {context.ParseResult.GetValue(fileArgument)}");
14+
Console.Write($"The file you chose was: {context.ParseResult.GetValue(fileArgument)}");
1515
});
1616

1717
command.Invoke(args);

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ int Default(Exception exception, InvocationContext context)
115115
{
116116
if (exception is not OperationCanceledException)
117117
{
118-
context.Console.ResetTerminalForegroundColor();
119-
context.Console.SetTerminalForegroundRed();
118+
ConsoleHelpers.ResetTerminalForegroundColor();
119+
ConsoleHelpers.SetTerminalForegroundRed();
120120

121121
context.Console.Error.Write(LocalizationResources.ExceptionHandlerHeader());
122122
context.Console.Error.WriteLine(exception.ToString());
123123

124-
context.Console.ResetTerminalForegroundColor();
124+
ConsoleHelpers.ResetTerminalForegroundColor();
125125
}
126126
return errorExitCode;
127127
}

src/System.CommandLine/ConsoleExtensions.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.InteropServices;
5+
6+
namespace System.CommandLine
7+
{
8+
internal static class ConsoleHelpers
9+
{
10+
private static readonly bool ColorsAreSupported = GetColorsAreSupported();
11+
12+
private static bool GetColorsAreSupported()
13+
#if NET7_0_OR_GREATER
14+
=> !(OperatingSystem.IsBrowser() || OperatingSystem.IsAndroid() || OperatingSystem.IsIOS() || OperatingSystem.IsTvOS())
15+
#else
16+
=> !(RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"))
17+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"))
18+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Create("IOS"))
19+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS")))
20+
#endif
21+
&& !Console.IsOutputRedirected;
22+
23+
internal static void SetTerminalForegroundRed()
24+
{
25+
if (ColorsAreSupported)
26+
{
27+
Console.ForegroundColor = ConsoleColor.Red;
28+
}
29+
}
30+
31+
internal static void ResetTerminalForegroundColor()
32+
{
33+
if (ColorsAreSupported)
34+
{
35+
Console.ResetColor();
36+
}
37+
}
38+
}
39+
}

src/System.CommandLine/IO/ConsoleExtensions.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/System.CommandLine/Invocation/ParseErrorResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ internal sealed class ParseErrorResultAction : CliAction
1212
{
1313
public override int Invoke(InvocationContext context)
1414
{
15-
context.Console.ResetTerminalForegroundColor();
16-
context.Console.SetTerminalForegroundRed();
15+
ConsoleHelpers.ResetTerminalForegroundColor();
16+
ConsoleHelpers.SetTerminalForegroundRed();
1717

1818
foreach (var error in context.ParseResult.Errors)
1919
{
@@ -22,7 +22,7 @@ public override int Invoke(InvocationContext context)
2222

2323
context.Console.Error.WriteLine();
2424

25-
context.Console.ResetTerminalForegroundColor();
25+
ConsoleHelpers.ResetTerminalForegroundColor();
2626

2727
new HelpOption().Action!.Invoke(context);
2828

0 commit comments

Comments
 (0)