-1

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

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

ecjb
  • 5,169
  • 12
  • 43
  • 79
  • 1
    `can_change_color()` indicates that you can actually assign RGB values to the color palette that ncurses uses -- and most terminals don't support that, and apparently the one you are using doesn't. That doesn't mean you can't use color output with ncurses, it just means that you are stuck with the standard color palette your terminal provides. If you want to "solve" that problem, use a different terminal emulator, and set the `TERM` environment variable appropriately. (And ensure that your `termcap`/`terminfo` installation contains information about that `TERM` setting.) – chris_se Mar 06 '23 at 23:16
  • Thank you for your comment @chris_se. I edited the question by showing that I can print without problems 24-bits colors in my terminal (both alacritty and iterm2). So the terminal is able to do that but ncurses isn't? What's your take on this? – ecjb Mar 07 '23 at 11:03
  • Maybe your `TERM` variable isn't setup properly or your `termcap`/`terminfo` is not correct? Also, I just noticed, but you didn't call `start_color()`, which you should do right after `initscr()` - maybe that's what's wrong in your case? – chris_se Mar 07 '23 at 11:06
  • @chris_se. thanks again! So I added commands related to your comment in the edited question (at the end of it) but still could not solve the problem. Do you have an idea about a possible next step? – ecjb Mar 07 '23 at 11:21
  • Sorry, no. I just tried both of your code (after your last edits) on Linux and they appear to work fine there (barring that you don't restore the terminal at the end of your program, requiring me to run 'reset' in the terminal afterwards), both exit with return code 0 and don't display any error message. I don't know enough about ncurses on macOS to be of help here, sorry. – chris_se Mar 07 '23 at 11:37
  • The setting "screen-256color" for $TERM sounds like a description of a terminal with a fixed palette. Apparently, your terminal emulators can do more than ncurses is aware of. I'm pretty sure that functions like `can_change_color()` only check the termcap entries for the given terminal type, but do not have a way to directly ask the terminal (emulation) about its capabilities. – Erich Kitzmueller Mar 07 '23 at 11:54
  • @chris_se what is the `reset` command ? – ecjb Mar 07 '23 at 11:54
  • You don't do `endwin()` in your `return 0` code path is what I meant. On the terminal you can use `reset` (at least on Linux, don't know about macOS) to reset the terminal options so that your shell again reacts normally. It's useful if a program that changed settings (e.g. disabling echo) messed stuff up and didn't terminate properly. – chris_se Mar 07 '23 at 11:56

2 Answers2

1

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".

ecjb
  • 5,169
  • 12
  • 43
  • 79
William McBrine
  • 2,166
  • 11
  • 7
  • thanks for your answer @WilliamMcBrine. Is `screen-256color` a hint to where the problem is? I would like ideally to keep using alacritty. Do you have a command line command equivalent (which might work for several terminals)? – ecjb Mar 07 '23 at 12:12
  • 1
    Actually yeah, "screen-256color" suggests that you might be using screen (are you?), which is its own terminal type with its own capabilities -- it doesn't necessarily pass everything through, unmangled. As for the command line, you can do "export TERM=xterm-256color", but that will only last for the session. You can add it to your .profile if you want. – William McBrine Mar 07 '23 at 13:34
  • Many thanks @WilliamMcBrine. `export TERM=xterm-256color` command worked for me! I allowed myself to adapt your answer accordingly – ecjb Mar 07 '23 at 13:53
  • Actually, there's a correct terminal description for [alacritty](https://invisible-island.net/ncurses/terminfo.src.html#toc-_Alacritty), which is different from [xterm-256color](https://invisible-island.net/ncurses/terminfo.src.html#tic-xterm-256color). Alacritty's developers however prefer different settings for the number of colors and the mouse protocol. – Thomas Dickey Mar 08 '23 at 08:41
0

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 (February 12, 2020). MacOS bundles ncurses 5.7 (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, which can be installed, or you could use the corresponding terminal description from MacPorts/brew.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105