Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

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

I've wrote a simple console program in C which uses ANSI escape codes to color its text.

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

I'd prefer to avoid using ncurses.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably the simplest way to go is to set the background colour of your text with ANSI:

For instance using:

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

will give you blue text on a red background (you can use this to test the effect in dramatic, easy to see colours).

Whereas

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

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

If you find that you're getting a kind of ugly transition zone between your background colour and the terminal's just print a sufficient number of newlines to wipe the whole screen.

To use this in C, you'll obviously want printf instead of echo.

The wiki page on ANSI escape codes has additional information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...