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

标签 c macos ncurses

相关问题

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

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 兼容。如果您转到终端的首选项 → 配置文件 → 高级,第一个设置是“将终端声明为”,正确答案(就 ncurses 而言)是 nsterm,而不是 xterm-256color ,明显的默认值。

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

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

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

关于c - 为什么 init_color() 在 Terminal.app 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54137680/

相关文章:

C - 输入正确的代码但没有收到输出

image - Unix:将 PDF 文件和图像合并为 PDF 文件?

macos - 在 Mac 中如何在 Android Studio 中的项目窗口之间切换?

c++ - ncurses:奇怪的行格式

C 编程 ncurses 颜色在多个窗口上不起作用

C - 双向链表

c - strcat 函数不适用于指针

c - C 中的 MPI_Send/Recv 动态分配数组

xcode - Xcode 7.3.1 可以在 macOS 10.12 上运行吗?

c/ncurses - 在不同的程序部分使用 getch