question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CLI (permanently) changes console color when an invalid command is typed

See original GitHub issue

Steps to reproduce

Try to use a command that doesn’t exist. e.g.: dotnet.exe migate

Expected behavior

An error message appears, maybe in a different color, and then console color is unchanged after that.

Actual behavior

Console color remains pink-ish.

Environment data

.NET Command Line Tools (1.0.0-preview4-004129)

Product Information:
 Version:            1.0.0-preview4-004129
 Commit SHA-1 hash:  325b849858

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   E:\Tools\dotnet-dev-win-x64.latest\sdk\1.0.0-preview4-004129

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
reecebradleycommented, Apr 21, 2017

Greetings! I’ve worked in a way to fix this issue, however, I’d like to explain what the issue is here first.

The Problem

Given the example above, here is the same example in the easiest way to explain it:

<OriginalConsoleColor>
dotnet foo
<Bold><Red>No executable found matching command "dotnet-foo"<OriginalConsoleColor><NotBold>
C:\Users\scott>

Each tag represents a ForegroundColor change. The real issue is in AnsiColorExtensions

        public static string Bold(this string text)
        {
            return "\x1B[1m" + text + "\x1B[22m";
        }

The marker 22m turns off bold - according to the AnsiConsole.Write.

If you look at the color extensions:

        public static string Red(this string text)
        {
            return "\x1B[31m" + text + "\x1B[39m";
        }

They all use 39m which translates to setting the console foreground back to the original color before the program started.

Summary

Since Console.ForegroundColor is static, whatever the last state this value is set to when the program ends will be what the user sees. Therefore, anyone who uses the AnsiColorExensions.Bold() last as in the case of the error message, the result is what you’ll see in the original post.

Possible Solution

I’ve gone through a few variations of fixing the issue and through that process I see a flaw with the AnsiColorExensions and AnsiConsole. Mainly that we cannot assume the <OriginalConsoleColor> is either <Bold> or <NotBold>. So using any actual color (excluding bold) in the AnsiColorExensions will result in writing a message out to the Console using the original color which could be a Bold or NotBold color, maybe that was intended? And using the Bold() extension was to ensure that the color would in fact be bold? The fact that color and boldness are not two different properties of the console complicates what is trying to be done. You have Green and DarkGreen, no property for bold.

That said, this is one of the ways to fix this:

        public static string Bold(this string text)
        {
            return "\x1B[1m" + text + "\x1B[39m";
        }
        public static string NotBold(this string text)
        {
            //This is the default but this option forces console windows
            return "\x1B[22m" + text + "\x1B[39m";
        }

Going back to my earlier statement, the original color of the Console.ForegroundColor could be a bold or not bold color, maybe you want to ensure text will in fact be NotBold? In which case, the above changes will be explicit.

I have those changes and others to support it in my fork for your viewing. I was about to make a PR, but due to design changes and get feedback there, but it seems maybe discussing it here is better.

Hopefully this all makes sense!

Reece

0reactions
reecebradleycommented, Jul 3, 2017

Watched the community stand-up - interesting background on how this bug really ‘BUGGED’ @shanselman 😉 Always interesting listening to community stand-ups, keep up the good work!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to permanently change the colour of CMD ...
there is no direct command to permanently change color of cmd but you can use Reg command and edit HKLM\Console\ strings or edit ......
Read more >
[Bug] Some commands change default console colors #907
The only way I've found to recover the proper console colors is to invoke the [Console]::ResetColor() method, but this is a hack and...
Read more >
How to change the color of your Linux terminal
You can add color to your Linux terminal using special ANSI encoding settings, either dynamically in a terminal command or in configuration ...
Read more >
Can I change the color of command I'm typing in terminal?
Yes, you can change it by tweaking your prompt. Usually when setting colored prompt you will reset to default color at the end...
Read more >
Colorizing text in the console with C++
Add a little Color to your Console Text HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // you can loop k higher to see more color ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found