Skip to content

Overload Console.Write and Console.WriteLine to take ConsoleColor as a parameter #39746

Open

Description

@abhaycashikar

(I was directed to repost this feature request here.)

Background and Motivation

I'm trying to create a user-friendly command-line application, and as such I wanted to color my output to the console. However, I found myself frequently writing code like this:

Console.WriteLine("This output is white on black.");
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine("This output is red on green.");
Console.ResetColor();
Console.WriteLine("This output is white on black.");

Proposed API

I thought it might be a lot easier for Console to have an overload on Write and WriteLine that takes in two ConsoleColor elements as arguments for the ForegroundColor and BackgroundColor variables and abstracts this process away from the developer. This way, the process of setting the ForegroundColor and BackgroundColor variables, writing to the console, and resetting the color can be written as one line in code. I think something like the below would work:

+ public static void WriteLine(ConsoleColor foreground, ConsoleColor background, string value) { throw null; }

Usage Examples

Console.WriteLine("This output is white on black.");
Console.WriteLine(ConsoleColor.Red, ConsoleColor.Green, "This output is red on green.");
Console.WriteLine("This output is white on black.");

would be equivalent to the code in the Background and Motivation section.

Alternative Designs

If overloading is not possible, additional methods like WriteColor and WriteLineColor would still be very useful in my opinion.

Risks

I'm not really sure if there are any risks that come with making this change. I don't think there would be because the functionality would just use the existing methods in Console and the existing ConsoleColor enum.

Activity

  1. added
    untriaged
    on Jul 21, 2020
  2. added
    api-suggestion
    on Jul 22, 2020
  3. danmoseley commented on Jul 22, 2020

    @danmoseley
    Contributor

    @abhaycashikar thanks for the suggestion. Could you please edit your suggestion above to follow the format here: https://github.com/dotnet/runtime/issues/new?assignees=&labels=api-suggestion&template=02_api_proposal.md&title=
    (you don't need to create a new issue)

    Making it explicit and formatted like that will help us review it.

  4. abhaycashikar commented on Jul 22, 2020

    @abhaycashikar
    Author

    Hi @danmosemsft, I apologize for not using that formatting initially! I've edited the issue to match it now, with the exception of the diff block in Proposed API (I thought it didn't really make sense since it would just be all additions).

  5. danmoseley commented on Jul 22, 2020

    @danmoseley
    Contributor

    @abhaycashikar looks good...could you add that diff block although it's just an add? That helps the review process.

  6. abhaycashikar commented on Jul 22, 2020

    @abhaycashikar
    Author

    Sure, no problem.

  7. KalleOlaviNiemitalo commented on Jul 22, 2020

    @KalleOlaviNiemitalo
    public static void WriteLine(string str, ConsoleColor foreground, ConsoleColor background)

    If you have source code that calls this method, but compile it against a target framework that does not have the method, then overload resolution chooses WriteLine(string format, object arg0, object arg1), which unexpectedly treats the string as a format string and the compiler does not warn about this. WriteLine(ConsoleColor foreground, ConsoleColor background, string str) would not have that risk. It would also be consistent with PSHostUserInterface.WriteLine.

  8. KalleOlaviNiemitalo commented on Jul 22, 2020

    @KalleOlaviNiemitalo

    I think the name of the string parameter should be value, for consistency with WriteLine(string value).

  9. SingleAccretion commented on Jul 22, 2020

    @SingleAccretion
    Contributor

    Linking as related: #34742. There was a lot of discussion in the reviews of that API about color, and the consensus was that BCL should consider adding some StreamWriter-type API for writing ANSI escapes and that's how color should be done in the future. Not that it invalidates these additions to Console, just another point in the bucket for ANSIEscapesWriter.

  10. KalleOlaviNiemitalo commented on Jul 22, 2020

    @KalleOlaviNiemitalo

    According to PowerShell/PowerShell#13071 (comment), PowerShell has prototyped System.CommandLine.Rendering for colors. However, that package is still in prerelease, lacks documentation, and is far more complex than the API being proposed here.

  11. danmoseley commented on Jul 22, 2020

    @danmoseley
    Contributor

    @abhaycashikar nit, the diff section would just look like

    + public static void WriteLine(string str, ConsoleColor foreground, ConsoleColor background) { throw null; }

    At this stage, we don't care about implementation.

    As other folks pointed out above, this is not as simple an addition as it may seem 😄

  12. KalleOlaviNiemitalo commented on Jul 22, 2020

    @KalleOlaviNiemitalo

    A developer writing

    Console.ForegroundColor = ConsoleColor.Blue;
    Console.BackgroundColor = ConsoleColor.Black;
    Console.WriteLine("Blue on black.");
    Console.WriteLine(ConsoleColor.Red, ConsoleColor.Green, "This output is red on green.");
    Console.WriteLine("Guess what this is!");

    could expect that the third line is again blue on black. However, that is difficult or impossible to implement if Console is using Console Virtual Terminal Sequences rather than the Win32 console API. It would just have to be documented.

  13. Jaykul commented on Jul 22, 2020

    @Jaykul

    I don't think it's worth the risk to add 16 color support to the Console api when we should be adding 16 million color support.

    @KalleOlaviNiemitalo is on the right track here, the Windows Console supports RGB colors now, and does not support controlling those colors through the Win32 API. There's no point trying to update System.Console. We just need to use VT based APIs through the Terminal class in the System.CommandLine package. It's right here in /dotnet/command-line-api.

  14. SingleAccretion commented on Jul 22, 2020

    @SingleAccretion
    Contributor

    There's no point trying to update System.Console

    That's a little harsh in my view. System.Console is widely used and widely known to be useful, so any improvements there will have the highest impact on the ecosystem.

  15. added this to the Future milestone on Aug 11, 2020
  16. removed
    untriaged
    on Aug 11, 2020
  17. GSPP commented on Sep 1, 2020

    @GSPP

    It would be useful if this API was integrated with the normal locking that writing to the console does. WriteLine is atomic which is important in multi-threaded console output. Changing colors before and after calling WriteLine is racy. Therefore, applications need to introduce their own global console lock if they want atomic colored WriteLine. If this was built-in, it could just be handled correctly.

  18. bramborman commented on Nov 12, 2020

    @bramborman

    I was just going to file a similar proposal with almost identical motivation but found this one so I will add my thoughts to this discussion.

    First of all, I'd expect this API would not reset colors to default after writing but reset to previous:

    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine(ConsoleColor.Red, ConsoleColor.Black, "Red on Black");
    Console.WriteLine("Yellow on Black");  // Not White on Black or whatever the default combination is

    Second - I think that all the overloads of Write and WriteLine should have support for coloring, so there would be many overloads such as:

    Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, int value);
    WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string format, params object[] args);

    Imho foreground color is changed much more frequently than the background color, so it'd be nice to have overloads that change only the foreground color:

    Write(ConsoleColor foregroundColor, string value);

    For the two-color-parameters overloads, the colors may be made nullable so you can change only the non-null color. These may, however, only be added if the color-changing methods have names different to Write and WriteLine as, as noted above, that'd also be a problem if compiled against framework without these overloads, as using the above would silently use the WriteLine(string format, object arg0, object arg1) overload.

    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine(null, ConsoleColor.DarkBlue, "Yellow on Dark Blue");

    Btw if this gets approved, I'm willing to implement it 😊

  19. KalleOlaviNiemitalo commented on Nov 12, 2020

    @KalleOlaviNiemitalo

    I'd expect this API would not reset colors to default after writing but reset to previous

    How would you implement that? The Windows console host does not support XTPUSHSGR and XTPOPSGR yet (microsoft/terminal#1796).

  20. bramborman commented on Nov 12, 2020

    @bramborman

    I'd expect this API would not reset colors to default after writing but reset to previous

    How would you implement that? The Windows console host does not support XTPUSHSGR and XTPOPSGR yet.

    ConsoleColor previousColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Write("Red text");
    Console.ForegroundColor = previousColor;
  21. KalleOlaviNiemitalo commented on Nov 12, 2020

    @KalleOlaviNiemitalo
    ConsoleColor previousColor = Console.ForegroundColor;

    That cannot restore the color properly if the previous color is not any of the 16 colors supported by the ConsoleColor type, e.g. if it was specified in RGB, as implemented in Windows Terminal.

  22. bramborman commented on Nov 12, 2020

    @bramborman
    ConsoleColor previousColor = Console.ForegroundColor;

    That cannot restore the color properly if the previous color is not any of the 16 colors supported by the ConsoleColor type, e.g. if it was specified in RGB, as implemented in Windows Terminal.

    It could be a note in the documentation. I think that if users are using RGB colors, they'd not be using this method :D

  23. eiriktsarpalis commented on Nov 12, 2020

    @eiriktsarpalis
    Member

    I think that all the overloads of Write and WriteLine should have support for coloring

    Agreed, if anything your typical console app does not use the same colors for entire lines, so arguably a Write overload is more useful.

  24. removed their assignment
    on Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Console

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions