LJ Archive

Listing 1: A Program skeleton Illustrating How to Use Some New ncurses Features

#include <curses.h>
#include <signal.h>

static void finish(int sig);

main(int argc, char *argv[])
{
    /* initialize your non-curses data structures here */

    /* arrange interrupts to terminate */
    (void) signal(SIGINT, finish);

    /* initialize the curses library */
    (void) initscr();
    /* enable keyboard mapping (non-BSD feature) */
    keypad(stdscr, TRUE);
    /* tell curses not to do NL->CR/NL on output */
    (void) nonl();
    /* take input chars one at a time, don't wait for <\\>n */
    (void) cbreak();
    /* don't echo input */
    (void) noecho();

    if (has_colors())
    {
        start_color();

        /*
         * Simple color assignment, often all we need.
         * Enables basic 8 colors on black background.
         * BSD curses can't do this!
         */
        init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
        init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
        init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
        init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
        init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
        init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
        init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
        init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
    }

    for (;;)
    {
        /* refresh, accept single keystroke of input */
        int c = getch();

        /* process the command keystroke (see Listing 2) */
    }

    finish(0);               /* we're done */
}

static void finish(int sig)
{
    endwin();

    /* do your non-curses wrapup here; see Listing 2 */

    exit(0);
}
LJ Archive