英文:
Function `can_change_color()` of ncurses not working although my terminal (tmux + alacritty) can display 24-bits colors
问题
以下是您提供的内容的翻译:
在我的macOS 11.6.1上,当使用gcc script.c -lncurses
编译以下代码时,我在两个不同的终端(tmux
与alacritty
和iterm2
)上收到以下错误消息:
/* quest.c */
#include <curses.h>
#include <stdlib.h>
int main(void)
{
initscr();
start_color();
keypad(stdscr, TRUE);
cbreak();
noecho();
if (has_colors() == FALSE) {
endwin();
printf("Your terminal does not support has_colors()\n");
exit(1);
}
if (can_change_color() == FALSE) {
endwin();
printf("Your terminal does not support can_change_color()\n");
exit(1);
}
return 0;
}
Your terminal does not support can_change_color()
我还在以下程序中收到以下错误:
/* quest.c */
#include <curses.h>
#include <stdlib.h>
int main(void)
{
initscr();
start_color();
keypad(stdscr, TRUE);
cbreak();
noecho();
if (has_colors() == FALSE) {
endwin();
printf("Your terminal does not support has_colors()\n");
exit(1);
}
if (COLOR_PAIRS < 6)
{
endwin();
printf("COLOR_PAIRS < 6: Warning. Your terminal can't handle this program. \n");
exit(1);
}
return 0;
}
COLOR_PAIRS < 6: Warning. Your terminal can't handle this program.
然而,在另一个程序中,我在终端中没有问题地打印24位颜色:
#include <stdio.h>
#define ANSI_FONT_COL_RESET "\x1b[0m"
#define FONT_COL_CUSTOM_RED "\e[38;2;200;0;0m" // rrr;ggg;bbb在38;2;rrr;ggg;bbb中可以从0到255
#define FONT_COL_CUSTOM_GREEN "\e[38;2;0;200;0m" // rrr;ggg;bbb在38;2;rrr;ggg;bbb中可以从0到255
#define FONT_COL_CUSTOM_BLUE "\e[38;2;0;0;200m" // rrr;ggg;bbb在38;2;rrr;ggg;bbb中可以从0到255
#define BCKGRD_COL_CUSTOM_RED "\e[48;2;200;0;0m" // rrr;ggg;bbb在48;2;rrr;ggg;bbb中可以从0到255
#define BCKGRD_COL_CUSTOM_GREEN "\e[48;2;0;200;0m" // rrr;ggg;bbb在48;2;rrr;ggg;bbb中可以从0到255
#define BCKGRD_COL_CUSTOM_BLUE "\e[48;2;0;0;200m" // rrr;ggg;bbb在48;2;rrr;ggg;bbb中可以从0到255
int main (int argc, char const *argv[]) {
printf(FONT_COL_CUSTOM_RED "This font color is CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_GREEN "This font color is CUSTOM_GREEN!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_BLUE "This font color is CUSTOM_BLUE!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_RED "This background color is CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_GREEN "This background color is CUSTOM_GREEN!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_BLUE "This background color is CUSTOM_BLUE!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_GREEN BCKGRD_COL_CUSTOM_RED "This font color is CUSTOM_GREEN with background CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf( "This font color is NORMAL!\n");
return 0;
}
带有相应输出:
[![enter image description here][1]][1]
如何解决这个问题?
编辑 1
- 我在两个脚本中都在
initscr()
后添加了start_color()
,但未解决问题,输出也没有变化。 - 命令
$ echo $TERM
给出了以下输出:screen-256color
- 命令
$ echo $terminfo
和$ echo $termcap
给出了以下输出:
no yes no no yes no no no yes no no no no yes yes no no no no no no no no no no no no no no no no no no no no no no yes no no no no yes no 61 8 37 256 32767 3 [Z
[%i%p1%d;%p2%dr [3g [H[J [K [J [%i%p1%d;%p2%dH
[H [?25l [34h[?25h [C M [34l [P [M [5m [1m [?1049h [4h [7m [3m [4m [m [?1049l [4l [23m [24m g )0 [L [3~ OB OP [21~ OQ OR OS [15~ [17~ [18~ [19~ [20~ [1~ [2~ OD [6~ [5~ OC OA [?1l> [?1h= E [%p1%dP [%p1%dM [%p1%dB [%p1%d@ [%p1%dL [%p1%dD [%p1%dC [%p1%dA c[?1000l[?25h 8 7
M [0%?%p6%t;1%;%?%p1%t;3%;%?%p2%t;4%;%?%p3%t;7%;%?%p4%t;5%;m%?%p9%t%e%; H ++,,--..00``aaffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~ [Z (B)0 [4~ [23~ [24~ [1K [39;49m [M [%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m [%?%p1%{8}%<%t4%p1%d%e%p1%{16
<details>
<summary>英文:</summary>
I get the following error message with the following code when compilig with `gcc script.c -lncurses` on my macos 11.6.1 2 different terminals (`tmux` with `alactritty` and `iterm2`):
/* quest.c */
#include <curses.h>
#include <stdlib.h>
int main(void)
{
initscr();
start_color();
keypad(stdscr, TRUE);
cbreak();
noecho();
if (has_colors() == FALSE) {
endwin();
printf("Your terminal does not support has_colors()\n");
exit(1);
}
if (can_change_color() == FALSE) {
endwin();
printf("Your terminal does not support can_change_color()\n");
exit(1);
}
return 0;
}
Your terminal does not support can_change_color()
I also get the following error with the following program:
/* quest.c */
#include <curses.h>
#include <stdlib.h>
int main(void)
{
initscr();
start_color();
keypad(stdscr, TRUE);
cbreak();
noecho();
if (has_colors() == FALSE) {
endwin();
printf("Your terminal does not support has_colors()\n");
exit(1);
}
if (COLOR_PAIRS < 6)
{
endwin();
printf("COLOR_PAIRS < 6: Warning. Your terminal can't handle this program. \n");
exit(1);
}
return 0;
}
COLOR_PAIRS < 6: Warning. Your terminal can't handle this program.
However in this other program I have no problem printing 24 bits colors in my terminal:
#include <stdio.h>
#define ANSI_FONT_COL_RESET "\x1b[0m"
#define FONT_COL_CUSTOM_RED "\e[38;2;200;0;0m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define FONT_COL_CUSTOM_GREEN "\e[38;2;0;200;0m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define FONT_COL_CUSTOM_BLUE "\e[38;2;0;0;200m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_RED "\e[48;2;200;0;0m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_GREEN "\e[48;2;0;200;0m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_BLUE "\e[48;2;0;0;200m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
int main (int argc, char const *argv[]) {
printf(FONT_COL_CUSTOM_RED "This font color is CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_GREEN "This font color is CUSTOM_GREEN!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_BLUE "This font color is CUSTOM_BLUE!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_RED "This background color is CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_GREEN "This background color is CUSTOM_GREEN!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_BLUE "This background color is CUSTOM_BLUE!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_GREEN BCKGRD_COL_CUSTOM_RED "This font color is CUSTOM_GREEN with background CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf( "This font color is NORMAL!\n");
return 0;
}
with the corresponding output:
[![enter image description here][1]][1]
How can I solve the problem?
**EDIT 1**
- I added `start_color();` after `initscr();` in the 2 scripts but it did not solve the problem and did not change the output
- the command `$ echo $TERM` gave me the following output: `screen-256color`
- The commands `$ echo $terminfo` and `$ echo $termcap` give me the following output:
no yes no no yes no no no yes no no no no yes yes no no no no no no no no no no no no no no no no no no no no no no yes no no no no yes no 61 8 37 256 32767 3 [Z
[%i%p1%d;%p2%dr [3g [H[J [K [J [%i%p1%d;%p2%dH
[H [?25l [34h[?25h [C M [34l [P [M [5m [1m [?1049h [4h [7m [3m [4m [m [?1049l [4l [23m [24m g )0 [L [3~ OB OP [21~ OQ OR OS [15~ [17~ [18~ [19~ [20~ [1~ [2~ OD [6~ [5~ OC OA [?1l> [?1h= E [%p1%dP [%p1%dM [%p1%dB [%p1%d@ [%p1%dL [%p1%dD [%p1%dC [%p1%dA c[?1000l[?25h 8 7
M [0%?%p6%t;1%;%?%p1%t;3%;%?%p2%t;4%;%?%p3%t;7%;%?%p4%t;5%;m%?%p9%t%e%; H ++,,--..00``aaffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~ [Z (B)0 [4~ [23~ [24~ [1K [39;49m [M [%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m [%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m
[1]: https://i.stack.imgur.com/3L8ZS.png
</details>
# 答案1
**得分**: 1
在你的`.profile`(例如`.zshrc`)或终端中,你应该写入:`export TERM=xterm-256color`
在终端中,进入设置 > 配置文件 > 高级 > Terminfo,在"声明终端为:"选项中选择"xterm-256color"。
<details>
<summary>英文:</summary>
In your `.profile` (e.g. `.zshrc`) or in the Terminal you should write: `export TERM=xterm-256color`
In Terminal, under Settings > Profiles > Advanced > Terminfo, set "Declare terminal as:" to "xterm-256color".
</details>
# 答案2
**得分**: 0
ncurses支持24位颜色(正式称为“直接颜色”),但MacOS捆绑版本的ncurses版本太旧,无法支持此功能。您可以安装并使用MacPorts(或brew),以获取比2009年更近的终端数据库,并具备使用32位数字能力(而不是有符号的16位数字)。对32位数字的支持始于[ncurses 6.2][1](2020年2月12日)。MacOS捆绑的是[ncurses 5.7][2](2008年末),并有一些小的更新。
通过更新的终端数据库,您可以使用适当的终端描述,即`TERM=alacritty`。Alacritty的开发者提供了他们首选的[terminfo][3]版本,可以安装,或者您可以使用来自[MacPorts][4]/[brew][5]的相应终端描述。
[1]: https://invisible-island.net/ncurses/announce-6.2.html
[2]: https://invisible-island.net/ncurses/ncurses.faq.html#what_platforms
[3]: https://github.com/alacritty/alacritty/blob/master/extra/alacritty.info
[4]: https://github.com/macports/macports-ports/blob/master/devel/ncurses/Portfile
[5]: https://formulae.brew.sh/formula/ncurses
<details>
<summary>英文:</summary>
ncurses supports 24-bit color (properly known as "direct colors"), but the bundled version of ncurses on MacOS is too old for this. You could install and use MacPorts (or brew), and get both a terminal database which is more recent than 2009, as well as the ability to use numeric capabilities with 32 bits (versus signed, 16-bit numbers). The support for 32-bit numbers began with [ncurses 6.2][1] (February 12, 2020). MacOS bundles [ncurses 5.7][2] (late 2008), with some minor updates.
With an up-to-date terminal database, you could use an appropriate terminal description, i.e., `TERM=alacritty`. Alacritty's developers provide their preferred flavor of [terminfo][3], which can be installed, or you could use the corresponding terminal description from [MacPorts][4]/[brew][5].
[1]: https://invisible-island.net/ncurses/announce-6.2.html
[2]: https://invisible-island.net/ncurses/ncurses.faq.html#what_platforms
[3]: https://github.com/alacritty/alacritty/blob/master/extra/alacritty.info
[4]: https://github.com/macports/macports-ports/blob/master/devel/ncurses/Portfile
[5]: https://formulae.brew.sh/formula/ncurses
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论