ncurses, how do i check how many colors my terminal supports?

This is a discussion on ncurses, how do i check how many colors my terminal supports? within the C Programming forums, part of the General Programming Boards category; I'm generating all ncurses color pairs like this Code: void generate_all_color_pairs(){ int foreground; int background; int i = 0; for(foreground=0; ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    ncurses, how do i check how many colors my terminal supports?

    I'm generating all ncurses color pairs like this

    Code:
    void generate_all_color_pairs(){        int foreground;
            int background;
            int i = 0;
    
    
            for(foreground=0; foreground<8; foreground++){          
                    for(background=0; background<8; background++){
                            i++;
                            init_pair(i, foreground, background);
                    }
            }
    }
    but according on several sources, some terminals suport more colors.

    Is there any way to determine how many colors the current terminal supports?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    terminfo(5): terminal capability data base - Linux man page
    Scroll down to "Color Handling"

    Also look up termcap as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,217
    After calling initscr(), you can use LINES, COLS and COLOR_PAIRS to determine if the numbers meet your program requirements. Here is an example of how I initialize some curses programs

    Code:
    #include <curses.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <assert.h>
    #include <string.h>
    
    #define PROGNAME "curses_init"
    #define REQD_COLOR_PAIRS (5)
    #define REQD_COLS (40)
    #define REQD_LINES (7)
    int main(void)
    {
    /* Verify terminal capabilities */
    	if (!isatty(1) || initscr() == NULL) {
    		fprintf(stderr, "%s: This program must be run from a terminal!\n",
    				PROGNAME);
    		return EXIT_FAILURE;
    	}
    	if (has_colors() && COLOR_PAIRS < REQD_COLOR_PAIRS) {
    		printw("This program requires at least %d colors (%d found)\n", 
    				REQD_COLOR_PAIRS, COLOR_PAIRS);
    		goto cleanup_curses;
    	}
    	if (!has_colors()) {
    		printw("This program requires colors (no support found)\n");
    		goto cleanup_curses;
    	}
    	if (COLS < REQD_COLS || LINES < REQD_LINES) {
    		printw("Terminal must be at least %dx%d!\n", REQD_COLS, REQD_LINES);
    		goto cleanup_curses;
    	}
    	
    /* Initialize all the colors */
    	printw("This terminal supports %d colors\n", COLOR_PAIRS);
    	start_color();
    	init_pair(1, COLOR_WHITE, COLOR_BLACK);
    	init_pair(2, COLOR_GREEN, COLOR_BLACK);
    	init_pair(3, COLOR_BLUE, COLOR_BLACK);
    	init_pair(4, COLOR_CYAN, COLOR_BLACK);
    	init_pair(5, COLOR_YELLOW, COLOR_BLUE);
    	assert(5 <= REQD_COLOR_PAIRS);
    	
    	for (int i=1; i <= 5; i++) {
    		attron(COLOR_PAIR(i));
    		printw("This is color pair %d\n", i);
    	}
    	assert(6 <= REQD_LINES);
    	assert(strlen("This is color pair XXX\n") <= REQD_COLS);	
    	refresh();
    
    /* End of program */
    cleanup_curses:
    	attron(COLOR_PAIR(5) | A_BOLD );
    	mvprintw(LINES-1,0,"Press any key to end the program...");
    	refresh();
    	getch();
    	endwin();
    	return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Thank you, I'll experiment in different terminals and see how they differ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to tell ncurses program terminal size
    By apawamajawa in forum Linux Programming
    Replies: 1
    Last Post: 07-20-2011, 03:04 AM
  2. Why I only get 8 colors out of ncurses?
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 05-08-2007, 07:06 PM
  3. ncurses, windows, and colors
    By subflood in forum Linux Programming
    Replies: 2
    Last Post: 08-26-2004, 11:14 AM
  4. Colors + terminal
    By Nino in forum C Programming
    Replies: 4
    Last Post: 07-24-2004, 12:27 AM
  5. NCurses: Problem with bckg colors
    By Karl in forum C Programming
    Replies: 2
    Last Post: 01-20-2004, 03:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21