0

I read that command prompt has added support for ansi escape sequences. However, when I tried to run a program that was working fine on a linux terminal it did not color properly in cmd.

#include <stdio.h>

#define ANSI_BLUE "\x1b[32m"
#define ANSI_DEF "\x1b[0m"

int main() {
    printf(ANSI_BLUE "Test" ANSI_DEF);
    return 0;
}

But the output I recieve when I gcc and run the exe is: [32mTest[0m

Is this a problem with the code, compiler or cmd?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Arisien
  • 11
  • 1
  • 4
    Does this answer your question? [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences) – fcdt Jul 07 '20 at 22:37
  • 3
    The relevant documentation for Windows 10 is https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences. Bottom line: You can use ANSI escape sequences after calling `dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(hOut,dwMode);`. – Peter - Reinstate Monica Jul 07 '20 at 22:42
  • ANSI has been depreciated for 39 years. This is the windows way of writing colour text https://winsourcecode.blogspot.com/2019/12/colourtext-changes-colour-of-text-to-be.html –  Jul 08 '20 at 00:27
  • 1
    @Mark you're wrong. Windows 10 supports ANSI because of WSL and now it's the cross-platform way to write color text – phuclv Jul 08 '20 at 00:30
  • It was depreciated 39 years ago as it has been in all versions of Dos and 16 and 32 bit windows. Absolutely no one used it. The very first program I downloaded in 1989 was an ANSI text editor and I paid for it. Absolutely useless as none would be able to see it as no one was going to waste 3000 bytes on an ANSI device driver. –  Jul 08 '20 at 00:47
  • And my computer doesn't support ANSI except for MSDos programs and that would require me to edit config.sys before it would work. The code in the answer also won't work. –  Jul 08 '20 at 00:54
  • 1
    @Mark if you're still using something that runs MS-DOS programs natively then you're already seriously outdated. **DOS has never supported ANSI codes** (unless you run some 3rd party drivers) and so are older Windows. Hence obviously ANSI codes have never been deprecated. Non-Windows people use ANSI codes all the time and Windows 10 **must** also support it. Oh you're on an outdated OS anyway – phuclv Jul 08 '20 at 01:48
  • 1
    From MSDos (I have MSBob as well) help file _**ANSI.SYS** Defines functions that change display graphics, control cursor movement, and reassign keys. The ANSI.SYS device driver supports ANSI terminal emulation of escape sequences to control your system's screen and keyboard._ This is the very first topic in help. –  Jul 08 '20 at 03:16
  • This is my version number 10.0.17134.829 No ansi in this Win 10. –  Jul 08 '20 at 03:27
  • 1
    A Windows console application coded in C should use the [Windows Console API](https://learn.microsoft.com/en-us/windows/console/using-the-console) to output text in colors like [SetConsoleTextAttribute](https://learn.microsoft.com/en-us/windows/console/setconsoletextattribute). Then the console application using colored output into console works on any Windows. I wrote for myself 30 years ago a C coded application for a colored menu in MS-DOS using Turbo C and modified it 10 years ago to use the Windows Console API functions compiled with VS6 and it works on Windows 95 to Windows 10. – Mofi Jul 08 '20 at 10:05

1 Answers1

1

How ansi escapes are handled are terminal dependent, and since cmd is your current terminal, that what decides what to do with those escapes.

Basically, what happens when you call printf, it just sends bytes to stdout. The terminal reads stdout and decides what to do with it.

The problem here has absolutely nothing to do with the compiler. If it is your code or cmd depends on how you view things. If you're using a HP printer with a Dell printer driver, is the problem with the printer or the driver? None of them. They just don't match.

Obviously, your terminal cannot handle those escapes, so if you want to print with color, you'll have to find another way.

These questions might be relevant:

colorful text using printf in C

C color text in terminal applications in windows

So what you can do on Windows is this:

#include <windows.h>
#include <stdio.h>
 
 // Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING  0x0004
#endif

static HANDLE stdoutHandle;
static DWORD outModeInit;

void setupConsole(void) {
    DWORD outMode = 0;
    stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);

    if(stdoutHandle == INVALID_HANDLE_VALUE) {
        exit(GetLastError());
    }
    
    if(!GetConsoleMode(stdoutHandle, &outMode)) {
        exit(GetLastError());
    }

    outModeInit = outMode;
    
    // Enable ANSI escape codes
    outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

    if(!SetConsoleMode(stdoutHandle, outMode)) {
        exit(GetLastError());
    }   
}

void restoreConsole(void) {
    // Reset colors
    printf("\x1b[0m");  
    
    // Reset console mode
    if(!SetConsoleMode(stdoutHandle, outModeInit)) {
        exit(GetLastError());
    }
}

int main(void) {
    setupConsole();
    puts("\x1b[31m\x1b[44mHello, World");
    restoreConsole();
}

I also found this link about wrappers to be able to use ANSI escapes in Windows:

https://solarianprogrammer.com/2019/04/08/c-programming-ansi-escape-codes-windows-macos-linux-terminals/

As far as I know, there is no portable way of printing with colors in C.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 1
    The terminal is cmd! Can you reformulate you answer, so that it fits to the question ("Is this a problem with the code, compiler or cmd?"), and hence is a valid answer? – Jörg Brüggmann Jul 07 '20 at 22:29
  • Better. However I still don't understand the purpose of the statement "This is terminal dependent.", because we now that the terminal is cmd - or can cmd be different terminals? – Jörg Brüggmann Jul 07 '20 at 22:55