Share via


Color ANSI codes don't work anymore

Question

Thursday, May 28, 2020 12:58 PM

I wrote a program in C++ about 2/3 weeks ago. It has a line where it should print some text with a specific color. To do so I decided to use ANSI escape color codes. I know there are other ways, but this one seems the most straight forward.

So for example I used: `std::cout << "\x1B[31mTEST RED\033[0m\n";` to print red text.

I compiled and linked the code using MSVC, it produced an .exe file, I ran it and all worked fine.

Yesterday I ran that same .exe file again, but this time it printed the string  "←[31mTEST RED←[0m", white.

I don't know how is this possible, I haven't touched the .exe file in any way, but 2 weeks ago it worked, now it doesn't!

I contacted MS support, and they tried to update my Windows version to the newest one, but it didn't solved the issue, so they told me to ask in Microsoft Q&A, and they subsequently told me to ask here.

If you need more information I'll give it to you as soon as possible. Thanks!

All replies (4)

Thursday, June 4, 2020 9:12 AM

Hi Lapo Falcone,

You need to add "system("cls")" before print ANSI code.

#include <iostream>
#include <stdlib.h> 

int main()
{
    system("cls");
    std::cout << "\x1B[31mTEST RED\033[0m\n";
    system("pause");
    return 0;
}

 

The related thread I refer: https://stackoverflow.com/q/58165576/11557747

Best Regards,

Dylan

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]


Thursday, June 4, 2020 11:58 AM

If it does not work, then go to console window’s menu, Properties, Options tab, uncheck the “Use legacy console […]” option and press OK. Then close the console and try again.


Thursday, June 4, 2020 2:27 PM

What behaviour do you see when you run the executable through Visual Studio? I am wondering if you previously only ran it through Visual Studio and then when you last ran it, you just double clicked on the .exe file or execute it through cmd.exe.

The documentation has you explicitly enabling the virtual terminal support on the output handle, so did you do that?

This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.


Thursday, June 4, 2020 2:58 PM

Run this Java testcase:
public class Test {

    public static void main(String[] args) {
        System.out.println("\033[37;1mwhite\033[0m");
    }
}
You will get this output: �[37;1mwhite�[0m
Redirect the application stdout to foo.log and run type foo.log
Notice that the output now reads white using white text