是否有 ansi 转义序列来为空白区域设置颜色?

Is there an ansi escape sequence to set colors for empty areas?

我看过一些关于 ANSI 序列的概述。在测试中,我发现前景色和背景色设置仅适用于带有文本的部分,从而导致一些不和谐的对比,尤其是对于语法突出显示。是否有指定非文本区域颜色/关闭此渲染行为的序列?

我也发现了对这两个序列的提及,但我发现的所有地方都指定这些是实现定义的,没有提及任何特定的语法。我试着把它当作 rgb 序列来使用,没有任何影响。

3[49
3[39

这是我要整理的示例。

这就是我想要的。 当前使用 windows 控制台,便携式解决方案更可取。

3[393[49分别用于重置文本和背景颜色。
您可以使用以下命令测试它们:

echo -e '\e[43m\e[31mbg&text\e[49mtext\e[39mnone'

要将背景颜色应用于整行,我发现您可以在行的开头使用 "erase in line" 控制序列((ESC)[K 对于基于 xterm 的终端)背景颜色控制序列强制使用您的背景颜色重绘行中的所有单元格:

echo -e '\e[41m\e[Kwhole line with background'

这不是编程问题; OP 可能需要一个教程,这使它偏离主题。也没有MCVE

"ANSI"(更正式地说 ECMA-48)定义了控制序列的语法并给出了(通常是简洁的)描述。问题中的片段

3[49
3[39

不是控制序列,因为它不完整。 ECMA-48 需要一个 final 字符来完成这些。根据问题的上下文,人们知道 m 是有意的,但这是一个假设。毕竟,ECMA-48(和各种终端文档)定义了其他可以将 39 和 49 作为参数的序列(例如 set-mode 控件)。

现在...ECMA-48 在这方面含糊(而且简洁),因为解释描述的方法不止一种

Set background color to default

并且委员会成员不想描述不同终端的细微差别。在不同的上下文中,curses(和 ncurses)库必须处理这个问题。有一些 终端功能 用于此目的。 terminfo(5) 手册页提供了一些有用的信息:

Some basic color capabilities are independent of the color method. The numeric capabilities colors and pairs specify the maximum numbers of colors and color-pairs that can be displayed simultaneously. The op (original pair) string resets foreground and background colors to their default values for the terminal. The oc string resets all colors or color-pairs to their default values for the terminal. Some terminals (including many PC terminal emulators) erase screen areas with the current background color rather than the power-up default background; these should have the boolean capability bce.

并且还提到了一个与这个问题有关的不同特征:

On some color terminals, colors collide with highlights. You can register these collisions with the ncv capability. This is a bit-mask of attributes not to be used when colors are enabled. The correspondence with the attributes understood by curses is as follows

也就是说,行为依赖于实现,curses/ncurses 提供了一种调整终端描述以反映差异的方法。 ncurses FAQ My terminal shows some uncolored spaces. The particular combination of features used in xterm was based on Linux console (see discussion) 中讨论了一些变体,这反过来又是对某些早期终端的不完美模仿,其行为可能在定义 bcencv.