【问题标题】:ncurses (init_extended_pair): can't create more than 255 color pairsncurses (init_extended_pa​​ir):不能创建超过 255 个颜色对
【发布时间】:2019-09-22 03:09:36
【问题描述】:

在 6.1 版中,ncurses 引入了init_extended_pair 以将可能的颜色对的限制扩展到short 限制之上。 在我的实验中,直到值 255 为止一切正常。对于 256 或更大的值,没有错误,但前景和背景具有默认值。对于 32767 及更大的值,函数返回 error

程序返回:

COLOR_PAIRS: 65536
                  Error: 32767

创建大量颜色对的正确原因是什么?就我而言,我至少需要 65536 个。 (在 Ubuntu 19.04 上测试)

#include <iostream>
#include <ncurses.h>

// g++ main.cpp -l:libncursesw.so.6.1 -ltinfo

int main() {
    initscr();
    start_color();

    std::cout << "COLOR_PAIRS: " << COLOR_PAIRS << std::endl;

    init_extended_color(2, 999, 0, 0);
    init_extended_color(3, 0, 999, 0);

    int pair1 = 255;
    if (init_extended_pair(pair1, 2, 3) == ERR)
        std::cout << "Error: " << pair1 << std::endl;

    attron(COLOR_PAIR(pair1));
    mvprintw(2, 1, "pair255");
    attroff(COLOR_PAIR(pair1));

    int pair2 = 256;
    if (init_extended_pair(pair2, 2, 3) == ERR)
        std::cout << "Error: " << pair2 << std::endl;

    attron(COLOR_PAIR(pair2));
    mvprintw(3, 1, "pair256");
    attroff(COLOR_PAIR(pair2));

    int pair3 = 32767; // 2^15-1
    if (init_extended_pair(pair3, 3, 2) == ERR)
        std::cout << "Error: " << pair3 << std::endl;

    attron(COLOR_PAIR(pair3));
    mvprintw(4, 1, "pair32767");
    attroff(COLOR_PAIR(pair3));

    refresh();
    getch();
    endwin();

    return 0;
}

编辑: 关于类似问题How to enable 32k color pairs in ncurses?。在我的情况下,COLOR_PAIRS 返回值 65536 而不是 256,更多问题来自 2015 年,init_extended_pair 于 2017.04.01 和released in version 6.1 January 27, 2018 添加到库中。尽管如此,我还是使用 --enable-ext-colors 重建了 libncursesw6 包(--enable-widec 已经可用),但我得到了相同的结果。

【问题讨论】:

  • 嗨@MarcSances,恐怕这不是我的问题。我正在使用更新的 API -init_extended_pair 在 2015 年不可用,COLOR_PAIRS 返回值 65536 而不是 256

标签: c++ colors terminal console ncurses


【解决方案1】:

实际上(针对 ncurses 6.1 开发运行此程序),我没有看到来自 init_extended_pair 的失败。乍一看,问题似乎出在这个块上:

attron(COLOR_PAIR(pair3));
mvprintw(4, 1, "pair32767");
attroff(COLOR_PAIR(pair3));

那些attron/attrofflegacy functions。您应该使用 attr_onattr_offattronattroffmacro 形式(通常用来代替 函数)是

#define wattron(win,at)         wattr_on(win, NCURSES_CAST(attr_t, at), NULL)
#define wattroff(win,at)        wattr_off(win, NCURSES_CAST(attr_t, at), NULL)

但无论哪种情况,数据都是“相同的”:适合 attr_t(32 位值)的数据。在其他一些函数中,颜色对是单独传递的,ncurses 6.1 提供了通过 opts 参数传递大于 16 位的对。这些特定功能并未以这种方式扩展。

但是,您的程序返回了init_extended_pair 的错误。这可能是来自_nc_init_pair 的(少数)回报中的任何一个,但主要的回报使用ValidPair

#define ValidPair(sp,pair) \
((sp != 0) && (pair >= 0) && (pair < sp->_pair_limit) && sp->_coloron)

为了检查这一点,我使用 TERM=xterm-256colorTERM=xterm-direct 针对当前的 ncurses6 运行代码。两者都有效,尽管后者中的 init_extended_color 失败(如预期的那样)。通过使用TRACE 编译ncurses 并使用NCURSES_TRACE=0x220 打开跟踪,我可以看到那个 失败。这是跟踪的屏幕截图,例如:

当前代码可从 ncurses 主页 (here) 获得。如果您能够使用当前代码重现该问题,您可能希望在bug-ncurses 邮件列表中进行讨论。否则(请参阅邮件列表),Debian package 是您正在使用的版本的参考。

【讨论】:

  • 嗨@thomas-dickey,我应该如何调用这两个函数?在您提供的链接中,请注意: "...wattr_on 和 wattr_off 不被此实现使用,除非检查它们是否为 NULL" (我相信 attr_on/offwattr_on/off 的别名, 并且在源代码中opts 设置为GCC_UNUSED (base/lib_wattron.c:48)。第二个/第一个问题是init_extended_pairpair3 返回ERR,这个问题发生在我打电话之前attron/off。请注意,对于 pair3,我希望使用反转颜色(绿色前景,红色背景)。
  • 我没有看到提到TERM。如果我使用TERM=xterm-256color 运行您的示例,则init_extended_pair 成功。它不适用于TERM=xterm-direct,因为 RGB 调色板是不可更改的。 package rules 在 ncurses 源中。至于“别名”,请阅读&lt;curses.h&gt;...
  • 我正在查看“除了检查”方面,稍后会更新答案:-)
  • 是的,我有 xterm-256color:echo $TERM xterm-256color 也许我会尝试另一个发行版。
  • 分发并不重要:只是源版本以及您如何配置 ncurses。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 2017-03-13
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
相关资源
最近更新 更多