While writing a larger program I stumbled upon a small problem with colored text output. Here's a much simpler program that reproduces this issue.
#include <stdio.h>
#define COL_RESET "\033[0m"
#define COL_BG_RED "\x1B[41m"
char *str = "the quick brown fox jumped over the lazy dog";
int main(int argc, char *argv[])
{
int i = 10;
while (i) {
puts(COL_BG_RED);
puts(str);
puts(COL_RESET);
puts(str);
i--;
}
return 0;
}
Now this is what I get when I run the program:
First time - expected result
Second time
As you can tell, the program decides to randomly print lines even after resetting the colors in red. When started in a fresh terminal it always prints the expected result. Unless I run clear
, there is no guarantee the output won't be mangled like in the second picture.
In the pictures I'm using xterm
, although other terminals do the same thing.
What can I do to prevent this?
As commented, this is known behavior for some well-known terminals: when scrolling (or reverse-scrolling), the newly cleared area on the screen is filled with the current background color. Linux console does this (except for a glitch some years ago, noted in the terminal database). xterm does it.
In ncurses, several related behaviors are lumped together as the bce
(background color erase) feature:
Normally ncurses fills in the blanks (and it is only a problem when the terminal entry is poorly chosen), and you would not see this. But using plain escape sequences means that you get to explore the nuances of bce
a little.
Terminal applications which use escape sequences directly for printing color should reset the color before writing any other text which is not intended to be colored. Other applications (such as line-editing in shells) have to keep this rule in mind when erasing text within the line
It seems that the issue occurs when the terminal starts scrolling.
The problem is probably caused by the fact that puts
appends a new-line character. Modify your code by using printf
.
printf(COL_BG_RED);
printf(str);
puts(COL_RESET);
puts(str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With