Search code examples
cmacosterminalncursesalacritty

Function `can_change_color()` of ncurses not working although my terminal (tmux + alacritty) can display 24-bits colors


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


Solution

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