如何在linux终端上设置终端背景颜色而不使用ncurses? [英] How to set terminal background color on linux terminal without using ncurses?

查看:262
本文介绍了如何在linux终端上设置终端背景颜色而不使用ncurses?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中写了一个简单的控制台程序,它使用ANSI转义代码为其文本着色。



有一种方法来临时设置整个背景终端黑色,默认字体颜色为浅灰色?可以在程序结束后还原?



我希望避免使用ncurses。

解决方案

可能最简单的方法是使用ANSI设置文本的背景颜色:



例如使用:

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

会给你在红色背景上的蓝色文字(你可以用它来测试戏剧性的,容易看到的颜色)。



鉴于

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



会在程序期间将前景设置为白色,将背景设置为黑色。



如果你发现你的背景颜色和终端之间有一个丑陋的过渡区域,只要打印足够多的换行符来擦除整个屏幕。



要在C中使用它,你显然需要 printf 而不是 echo p>

ANSI转义代码上的wiki页面附加信息。


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.

解决方案

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

For instance using:

echo -e "\e[37m\e[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[97m\e[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.

这篇关于如何在linux终端上设置终端背景颜色而不使用ncurses?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆