【问题标题】:How do I change the cursor color in ncurses forms?如何更改 ncurses 表单中的光标颜色?
【发布时间】:2023-09-11 01:58:01
【问题描述】:

我找不到任何将 ncurses 表单库中的光标颜色从绿色更改为其他颜色的方法。谷歌搜索并在手册页中搜索光标或颜色没有帮助。有人知道这是怎么做到的吗?

【问题讨论】:

  • 似乎没有控制光标颜色的功能,您可以使用printf("\e]12;blue\a"); 来完成这项工作吗?
  • 完全可以!把它放在一个答案中(如果你不介意的话,加上一些解释),我会接受它。

标签: ncurses


【解决方案1】:

你可以通过写\e]12;COLOR\a\033]12;COLOR\007来改变颜色,它们都一样,这里是一个简单的例子:

#include <stdio.h>
#include <unistd.h>

void cursor_set_color_string(const char *color) {
    printf("\e]12;%s\a", color);
    fflush(stdout);
}

int main(int argc, char **argv) {

    cursor_set_color_string("yellow"); sleep(1);
    cursor_set_color_string("gray"); sleep(1); 
    cursor_set_color_string("blue"); sleep(1);
    cursor_set_color_string("red"); sleep(1);
    cursor_set_color_string("brown"); sleep(1);

    return 0;
}

以下是颜色名称列表:Xterm Colors

看起来你也可以使用\e]12;#XXXXXX\a形式的RGB颜色:

#include <stdio.h>
#include <unistd.h>

void cursor_set_color_rgb(unsigned char red,
                          unsigned char green,
                          unsigned char blue) {
    printf("\e]12;#%.2x%.2x%.2x\a", red, green, blue);
    fflush(stdout);
}

int main(int argc, char **argv) {

    cursor_set_color_rgb(0xff, 0xff, 0xff); sleep(1);
    cursor_set_color_rgb(0xff, 0xff, 0x00); sleep(1);
    cursor_set_color_rgb(0xff, 0x00, 0xff); sleep(1);
    cursor_set_color_rgb(0x00, 0xff, 0xff); sleep(1);

    return 0;
}

【讨论】: