为什么 init_color() 在 Terminal.app 中无效?

Why is init_color() ineffectual in Terminal.app?

相关问题

ncurses: init_color() has no effect(特定于 PuTTY、xterm 和 gnome-terminal)

NCurses: Why does init_color return OK but still not set the color?(特定于 xterm)

问题

init_color 正在将颜色信息保存在 ncurses 内存中,并报告成功 - 但对 MacOS (Mojave 10.14.2) Terminal.app 版本 2.9.1 (421.1) 没有影响。 ncurses 通过 HomeBrew 安装:

$ brew info ncurses
ncurses: stable 6.1 (bottled) [keg-only]
Text-based UI library
https://www.gnu.org/software/ncurses/
/usr/local/Cellar/ncurses/6.1 (3,869 files, 8.3MB)
  Poured from bottle on 2018-12-31 at 21:59:36

我的解决方法可能必须依赖默认的 ncurses 调色板。

根据上一个问题中的一个答案,我查看了 terminfo source。相关部分在本文下方:

# The AppKit Terminal.app descriptions all have names beginning with
# "nsterm".

特别是:

# For Apple_Terminal v309+, use "nsterm-256color" (or "nsterm-bce")

好像没有10.14的专区;最新条目适用于 10.13:

# reviewed Terminal.app in High Sierra (version 2.8 build 400) -TD
# Comparing with build361, little has changed, except that italics work.
# Direct-color is not supported, by the way.
#
# Improved rmso/rmul -TD
nsterm-build400|Terminal.app in OS X 10.13,
        rmso=\E[27m, rmul=\E[24m, use=xterm+sm+1006,
        use=ecma+italics, use=nsterm-build361,

# This is an alias which should always point to the "current" version
nsterm|nsterm-256color|Apple_Terminal|AppKit Terminal.app,
        use=nsterm-build400,

阅读后我不清楚这是否是预期的行为。

最小可重现示例

当我 运行 这样做时,所有断言都通过了,但是前景和背景显示了默认颜色(而不是编辑后的颜色)。

#include <assert.h>
#include <fcntl.h>
#include <ncursesw/ncurses.h>
#include <unistd.h>


int main() {
    int fdump = open("/tmp/ncurses_dump", O_WRONLY|O_CREAT|O_TRUNC, 0600);
    dup2(fdump, STDOUT_FILENO);

    assert(initscr() != NULL);
    assert(ERR != start_color());
    assert(has_colors());
    assert(can_change_color());
    assert(COLOR_PAIRS >= 256);
    assert(COLORS >= 256);

    // color 21 is blue by default
    // let's make it white
    const NCURSES_COLOR_T fore = 21;
    assert(ERR != init_color(fore, 998, 999, 1000));
    NCURSES_COLOR_T r, g, b;
    assert(ERR != color_content(fore, &r, &g, &b));
    assert(r == 998);
    assert(g == 999);
    assert(b == 1000);

    // color 195 is light blue by default
    // let's make it black
    const NCURSES_COLOR_T back = 195;
    assert(ERR != init_color(back, 0, 1, 2));
    assert(ERR != color_content(back, &r, &g, &b));
    assert(r == 0);
    assert(g == 1);
    assert(b == 2);

    // arbitrary
    const NCURSES_PAIRS_T pair = 100;
    assert(ERR != init_pair(pair, fore, back));
    NCURSES_COLOR_T fc, bc;
    assert(ERR != pair_content(pair, &fc, &bc));
    assert(fc == fore);
    assert(bc == back);

    // The pair init works, but the color init doesn't - this still outputs blue
    // on light blue
    assert(ERR != attron(COLOR_PAIR(pair)));
    assert(ERR != addch('X'));

    while (getch() != 'q');

    endwin();
    return 0;
}

转储文件包含以下内容:

$ hexdump -C /tmp/ncurses_dump 
00000000  1b 5b 3f 31 30 34 39 68  1b 5b 32 32 3b 30 3b 30  |.[?1049h.[22;0;0|
00000010  74 1b 5b 31 3b 34 37 72  1b 28 42 1b 5b 6d 1b 5b  |t.[1;47r.(B.[m.[|
00000020  34 6c 1b 5b 3f 37 68 1b  5b 33 39 3b 34 39 6d 1b  |4l.[?7h.[39;49m.|
00000030  5d 34 3b 32 31 3b 72 67  62 3a 46 45 2f 46 45 2f  |]4;21;rgb:FE/FE/|
00000040  46 46 1b 5c 1b 5d 34 3b  31 39 35 3b 72 67 62 3a  |FF.\.]4;195;rgb:|
00000050  30 30 2f 30 30 2f 30 30  1b 5c 1b 5b 33 39 3b 34  |00/00/00.\.[39;4|
00000060  39 6d 1b 5b 33 37 6d 1b  5b 34 30 6d 1b 5b 48 1b  |9m.[37m.[40m.[H.|
00000070  5b 32 4a 1b 5b 33 38 3b  35 3b 32 31 6d 1b 5b 34  |[2J.[38;5;21m.[4|
00000080  38 3b 35 3b 31 39 35 6d  58 1b 28 42 1b 5b 6d 1b  |8;5;195mX.(B.[m.|
00000090  5b 33 39 3b 34 39 6d 1b  5b 33 37 6d 1b 5b 34 30  |[39;49m.[37m.[40|
000000a0  6d 1b 5b 33 38 3b 35 3b  32 31 6d 1b 5b 34 38 3b  |m.[38;5;21m.[48;|
000000b0  35 3b 31 39 35 6d 71 1b  28 42 1b 5b 6d 1b 5b 33  |5;195mq.(B.[m.[3|
000000c0  39 3b 34 39 6d 1b 5b 33  37 6d 1b 5b 34 30 6d 1b  |9;49m.[37m.[40m.|
000000d0  5b 33 39 3b 34 39 6d 0d  1b 5b 34 37 64 1b 5b 4b  |[39;49m..[47d.[K|
000000e0  1b 5b 33 39 3b 34 39 6d  1b 5d 31 30 34 07 1b 5b  |.[39;49m.]104..[|
000000f0  34 37 3b 31 48 1b 5b 3f  31 30 34 39 6c 1b 5b 32  |47;1H.[?1049l.[2|
00000100  33 3b 30 3b 30 74 0d 1b  5b 3f 31 6c 1b 3e        |3;0;0t..[?1l.>|
0000010e

此时,我很确定 macOS 终端不支持修改颜色索引。您在转储中得到 xterm escape sequence for doing so (\e]4),但 Apple 的终端不是真正的 xterm。

最可能的解释是 Apple 的终端将自己声明为 xterm,因为它 几乎xterm 兼容。如果你去终端的首选项→配置文件→高级,第一个设置是"Declare terminal as",正确的答案(就ncurses而言)是nsterm,而不是xterm-256color,明显的默认.

但是,就像浏览器用户代理一样,声明未知内容有时比做虚假声明更糟糕。您的大多数其他程序可能知道如何处理 nsterm,但也许不知道。例如,当使用 nsterm 时,macOS 附带的 bash 安装会停止识别 delete⌦ 键,而是打印 ~

Ncurses 知道 nsterm。如果您将 $TERM 更改为那个,您编写的示例程序将不再有效:它断言颜色索引不能在此终端仿真器上更改,这显然是正确的。

可以使用 \e[C;2;R;G;Bm 命令在 macOS 终端上使用 24 位 RGB 颜色,其中 C 是前景 38,背景 48,R , GB 是 0...255 范围内的数值。但是,无论出于何种原因,ncurses 似乎都无法使用此功能。

根据评论,您需要一个 terminfo 条目(来源)like 这样才能使用 Mohave 中的新功能:

nsterm-direct|nsterm with direct color,
        use=xterm+indirect, use=nsterm,

Compile/install 使用你用 brew 安装的 ncurses 6.1 tic。

这是一个起点(Apple 确实进行了不兼容的更改,例如您可能会在 history of the nsterm entries). There are other details to investigate by testing (see tack and vttest 中注意到)。

但是:init_color 在这种情况下用处不大(即 直接颜色 ),因为它假定您可以重新编程 RGB 条目的颜色内容.你不能。你可以做的是使用init_extended_pair as shown in the picsmap example. Some people might find that useful (see discussion)。颜色对只影响 ncurses 中的数据;颜色内容更改终端。

对于 256 种颜色...Terminal.app 没有可更改的调色板,除非最近才这样做。这就是为什么 nsterm-256color uses xterm+256setaf as a building block (it's a have-not 在这个领域)。如果您点击链接回到定义,您将看到:

# palette is hardcoded...
xterm+256setaf|xterm 256-color (set-only),
        ccc@,
        colors#0x100, pairs#0x10000,
        initc@, op=\E[39;49m,
        setab=\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;
              5;%p1%d%;m,
        setaf=\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5
              ;%p1%d%;m,
        setb@, setf@,