在运行时确定终端/TTY 背景颜色 [英] Determine terminal/TTY background color at runtime

查看:55
本文介绍了在运行时确定终端/TTY 背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 chalk 库对终端进行样式化/着色.

从'粉笔'导入粉笔;

如果我使用:

console.log(chalk.blue('foobar'));

在浅色背景的终端中完全可读,但在深色背景的终端中完全不可读.

有没有办法在运行时确定终端的背景颜色?

<小时>

给出的例子:

"npm notice" 日志级别就是这个问题的一个例子:

黑底蓝字很难看.

解决方案

以下是通用答案,关于

<块引用>

反向视频通常用于软件程序中作为视觉辅助突出显示已作为帮助预防的选择描述错误,在对象上执行预期操作这不是我们想要的.它在现代桌面中更为常见环境将背景更改为其他颜色,例如蓝色,或使用半透明背景来突出显示"所选文本.在终端理解 ANSI 转义序列,反向视频函数使用转义序列 CSI 7 m(等于SGR 7).https://en.wikipedia.org/wiki/Reverse_video

来自我自己的回答:如何在 Linux 中更改 echo 的输出颜色,信息量很大的问题关于ansi序列.

Using the chalk library to stylize/colorize the terminal.

import chalk from 'chalk';

if I use:

console.log(chalk.blue('foobar'));

that's totally readable in a terminal with a light background, but totally unreadable in a terminal with a dark background.

Is there some way to determine the background color of a terminal at runtime?


Example given:

The "npm notice" log level is a case of this problem:

It's difficult to read the blue on black.

解决方案

The following is a generic answer, about ANSI Escape sequences in the tty context.

To highlight a specific sequence by swapping the backgroud/foreground, so it stay readable on all color schemes, use code 7 Reverse video.

This way there is no need to prior knows the palette and adapt.

Example in bash, using reverse video:

echo -e "\033[7mHello world\e[0m"

This can be chained, reverse video, and a red background:

echo -e "\033[31;7mHello world\e[0m"

This will always be readable on any background, like the second line of this gif:

Reverse video is commonly used in software programs as a visual aid to highlight a selection that has been made as an aid in preventing description errors, where an intended action is performed on an object that is not the one intended. It is more common in modern desktop environments to change the background to other colors such as blue, or to use a semi-transparent background to "highlight" the selected text. On a terminal understanding ANSI escape sequences, the reverse video function is activated using the escape sequence CSI 7 m (which equals SGR 7). https://en.wikipedia.org/wiki/Reverse_video

From my own answer: How to change the output color of echo in Linux , a question with a lot of informations about ansi sequences.

这篇关于在运行时确定终端/TTY 背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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