How to set terminal background color on linux terminal without using ncurses?

I wrote a simple C console program that uses ANSI escape codes for colored text.

Is there a way to temporarily set the background of the entire terminal to black, and the default font color is light gray? Can this be returned after the program ends?

I would prefer to avoid using ncurses.

+4
source share
2 answers

Probably the easiest way is to set the background color of your text using ANSI:

For example, using:

echo -e "\e[37m\e[41m" 

will give you blue text on a red background (you can use it to check the effect in bright, easily distinguishable colors).

While

 echo -e "\e[97m\e[40m" 

sets the foreground to white and the background to black for the duration of your program.

If you find yourself getting a kind of ugly transition zone between the background color and the terminal, just print enough lines of a new line to erase the entire screen.

To use this in C, you obviously need printf instead of echo .

The ANSI escape codes wiki page contains additional information.

+5
source

How to do this depends on the terminal that the user is using. It can be ANSI, it can be VT100, it can be a linear printer. ncurses abstracts this horror for you. It uses a database on how to talk to different terminals (see the contents of $ TERM to find out which one you are currently using), usually stored in /lib/terminfo or /usr/share/terminfo .

Once you look in these files, you probably want to reconsider not using ncurses unless you have specific requirements to avoid this (embedded system with insufficient memory, etc.).

+3
source

All Articles