Skip to content

Redundant logic in ConsoleExtensions #1851

Closed

Description

@KalleOlaviNiemitalo

The SetTerminalForegroundRed and ResetTerminalForegroundColor methods of ConsoleExtensions can read the Platform.IsConsoleRedirectionCheckSupported and Console.IsOutputRedirected properties when deciding what to do. However, the value of Console.IsOutputRedirected has no effect in the end, because those methods execute the same code either way:

IsConsoleRedirectionCheckSupported IsOutputRedirected SetTerminalForegroundRed ResetTerminalForegroundColor
true false sets Console.ForegroundColor calls Console.ResetColor()
true true sets Console.ForegroundColor calls Console.ResetColor()
false not read does nothing does nothing

That seems unintentional. If the behavior is correct, then at least the logic could be simplified.

Previously noted in #1850 (comment).

Activity

  1. KalleOlaviNiemitalo commented on Sep 13, 2022

    @KalleOlaviNiemitalo
    Author

    The redundant logic came from #676. Before that, the methods worked like this:

    IsConsoleRedirectionCheckSupported IsOutputRedirected SetTerminalForegroundRed ResetTerminalForegroundColor
    false false sets Console.ForegroundColor calls Console.ResetColor()
    false true does nothing does nothing
    true not read sets Console.ForegroundColor calls Console.ResetColor()

    except that they also checked for an ITerminal interface by name. The IsConsoleRedirectionCheckSupported property used to return the opposite of what it returns now. Based on that, I think the correct behavior would be:

    IsConsoleRedirectionCheckSupported IsOutputRedirected SetTerminalForegroundRed ResetTerminalForegroundColor
    true false sets Console.ForegroundColor calls Console.ResetColor()
    true true does nothing does nothing
    false not read sets Console.ForegroundColor calls Console.ResetColor()
  2. added
    bug
    on Mar 6, 2023
  3. self-assigned this
    on Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions