-
Notifications
You must be signed in to change notification settings - Fork 431
Removing almost unused ConsoleExtensions #2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f9986d7
merge two ConsoleExtensions types into one, remove unused IConsole ar…
adamsitnik 0b9b7f8
simplify the logic
adamsitnik adba90d
remove almost unused public methods from ConsoleExtensions, rename it…
adamsitnik 6de8717
address code review feedback: use more reliable way of checking wheth…
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an application is designed to parse and invoke commands from custom sources like network sockets, and it should not modify System.Console.ForegroundColor during these operations (because such changes might mess up output from a different thread), then it apparently needs to avoid UseParseErrorReporting entirely and avoid UseExceptionHandler with the default handler. That's tolerable but not quite obvious.
If you're eventually going to have
TextWriter Error, then perhaps you can compareError == System.Console.Errorand change Console.ForegroundColor only in that matches. Or have somebool AllowConsoleColorproperty in CommandLineConfiguration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking on — if class CliConfiguration is going to have
TextWriter Error, then it can havebool UseErrorColor { get; set; }such that…UseErrorColorhas not been set, then its getter computes the value fromthis.Error == System.Console.Error && !System.Console.IsErrorRedirected, defaulting to false on PlatformNotSupportedException. Does not cache the result into a field of CliConfiguration because it's going to be used only once per error. Instead, each method that reads this property should save the result to a local so that it restores the colours if and only if it has set them.UseErrorColorhas been set, then trust the application-assigned value when deciding whether to set the foreground color, and do not check whether error has been redirected.Not sure about IsErrorRedirected vs. IsOutputRedirected. The error text should go to the CliConfiguration.Error TextWriter, but if System.Console.ForegroundColor outputs ECMA-48 control sequences instead of using the Windows console API, I guess it cannot be told whether those should go to Console.Error or Console.Out. So perhaps the UseErrorColor getter should check
this.Error == System.Console.Error && !System.Console.IsErrorRedirected && !System.Console.IsOutputRedirected. I'm omittingthis.Out == System.Console.Outbecause errors should never go tothis.Out.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When https://github.com/dotnet/runtime/blob/5edef4b20babd4c3ddac7460e536f86fd0f2d724/src/libraries/System.Console/src/System/ConsolePal.Unix.cs sets colors, it does
lock (Console.Out)but writes toSTDOUT_FILENO. So Console.SetOut(TextWriter) affects the locking but does not affect where the SGR control sequence goes.