繁体   English   中英

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

[英]Why is init_color() ineffectual in Terminal.app?

相关问题

ncurses:init_color()不起作用 (特定于PuTTY,xterm和gnome-terminal)

NCurses:为什么init_color返回OK,但仍然没有设置颜色? (特定于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来源 相关部分在此文本下:

# 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转义序列\\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为背景, RGB为数字值在0 ... 255范围内。 但是,无论出于何种原因,看来ncurses都无法使用此功能。

从评论,您将需要一个terminfo项(源) 这样在莫哈维使用新功能:

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

使用与brew一起安装的ncurses 6.1 tic进行编译/安装。

这是一个起点(Apple确实做出了不兼容的更改,您可能会在nsterm条目的历史中注意到这一点)。 还有其他细节需要通过测试进行调查(例如,参见“ tack”和“ vttest ”)。

但是: init_color在这种情况下用处不大(即直接颜色 ),因为它假定您可以重新编程RGB条目的颜色内容。 你不能 可以做的是使用picsmap示例中所示的init_extended_pa​​ir 有些人可能会觉得有用(请参阅讨论 )。 颜色对只会影响ncurses的数据; 颜色内容会更改终端。

对于256色... Terminal.app没有可更改的调色板,除非最近已完成。 这就是nsterm-256color使用xterm+256setaf作为构建块的原因(在该领域中这不是必须的 )。 如果您点击链接回到定义,您将看到:

# 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@,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM