Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this text not being colored by ncurses?

I wanted to create a window in ncurses, surround it with a box, and write some colored text in it.

When I try to make simple colored text in the standard window it works perfectly, but when I try to put it in a new window the text appears white on black (i.e. the default)

Here's the code I've tried. Why doens't it work?

#include <ncurses.h>

int main(int argc, char *argv[])
{
    initscreen();
    WINDOW * win = newwin(8,15,1,1);
    box(win,0,0);
    start_color();
    init_pair(1, COLOR_BLACK, COLOR_RED);
    attron(COLOR_PAIR(1));
    mvwprintw(win,1,1,"colored text");
    wrefresh(win);
    getch();
    return 0;
}
like image 251
user3329072 Avatar asked Feb 19 '14 16:02

user3329072


People also ask

How do you color Ncurses?

To use color, you must call the start_color() function soon after calling initscr() , to initialize the default color set (the curses. wrapper() function does this automatically). Once that's done, the has_colors() function returns TRUE if the terminal in use can actually display color.


1 Answers

I soloved the problem by using

wattron(win, COLOR_PAIR(1));

instead of

attron(COLOR_PAIR(1));

wattron affects the given window, while attron assumes you mean stdscr, rather than the current window.

like image 73
user3329072 Avatar answered Oct 11 '22 12:10

user3329072