Question

I have some code in C to print out lines of text in different colors.

It's working on Linux using escape characters (for example here).
It's working on Windows using SetConsoleTextAttribute

But my problem is when using Cygwin.
Escape characters don't work on Cygwin.
And calls to GetConsoleScreenBufferInfo always failed, and getLastErrorText() gives me this message:

Incorrect function. (0x1).

So my question is: how to print text in color from a C program running in a Cygwin terminal? Do I need to install special libraries?

Was it helpful?

Solution

Thanks SzG and M Oehm for you comments.

As you said, M Oehm, the linux example is working.

I found the problem in my code. I was using the color value for Windows instead of the ones for Linux.

For example:

_ftprintf(target, TEXT("%c[%d;%dm%s%c[K\n"), 
          0x1B, foreground, background, printBuffer, 0x1B);

The values for foreground and background were not correct.

Now it's working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top