gpt4 book ai didi

node.js - 在运行时确定终端/TTY 背景颜色

转载 作者:搜寻专家 更新时间:2023-10-31 22:46:47 26 4
gpt4 key购买 nike

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

import chalk from 'chalk';

如果我使用:

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

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

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


给出的例子:

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

enter image description here

黑底蓝字很难看。

最佳答案

以下是关于 ANSI Escape sequences 的通用回答tty 上下文中

要通过交换背景/前景来突出显示特定序列,使其在所有配色方案上保持可读性,请使用code 7 Reverse video

这样就无需事先了解调色板并进行调整。

bash 中的示例,使用反向视频:

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

这可以是链接的、反向视频和红色背景:

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

这将始终在任何背景下可读,就像这个 gif 的第二行:

enter image description here

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

来 self 自己的回答:How to change the output color of echo in Linux ,一个包含大量关于 ansi 序列 信息的问题。

关于node.js - 在运行时确定终端/TTY 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com