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

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

问题描述

我用 C 语言编写了一个简单的控制台程序,它使用 ANSI 转义码为其文本着色.

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?

我宁愿避免使用 ncurses.

I'd prefer to avoid using ncurses.

推荐答案

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

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

例如使用:

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).

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.

要在 C 中使用它,您显然需要 printf 而不是 echo.

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

ANSI 转义码上的 wiki 页面有更多信息.

The wiki page on ANSI escape codes has additional information.

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

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