Skip to content
Search

Ncurses

lastmod 2025-08-19created 2015-02-1327 edits

ncurses (new curses) is a programming library that provides an API allowing programmers to write text user interfaces in a terminal-independent manner. It is a toolkit for developing GUI-like application software that runs in a terminal emulator. It also optimizes screen changes to reduce the latency that can be experienced when using a remote shell.

How to install

$ sudo apt-get install libncurses-dev

Hello world

#include <ncurses.h>

int main()
{
    initscr();                  // Start curses mode
    printw("Hello World !!!");  // Print Hello World
    refresh();                  // Print it on to the real screen
    getch();                    // Wait for user input
    endwin();                   // End curses mode

    return 0;
}

Non-blocking keyboard input

WINDOW *w = initscr();
cbreak();
nodelay(w, TRUE);
//noecho(); // If you want to no-echo mode.

Attribute

  • attron(): Turns ON the Bit-mask for an attribute.
  • attroff(): Turns OFF the Bit-mask for an attribute.

Attribute Bit-mask

  • A_NORMAL: Normal display (no highlight)
  • A_STANDOUT: Best highlighting mode of the terminal.
  • A_UNDERLINE: Underlining
  • A_REVERSE: Reverse video
  • A_BLINK: Blinking
  • A_DIM: Half bright
  • A_BOLD: Extra bright or bold
  • A_PROTECT: Protected mode
  • A_INVIS: Invisible or blank mode
  • A_ALTCHARSET: Alternate character set
  • A_CHARTEXT: Bit-mask to extract a character
  • COLOR_PAIR(n): Color-pair number n

attron vs attrset

So what exactly is the difference between the attron() and attrset() functions? While attron can only turn on the attributes given to it, attrset sets the attributes of the window. That is, attset() sets new attributes regardless of what attributes the window currently has. attroff() likewise can only turn off the attributes given to it as arguments. While this makes attributes easy to handle, if you don't use it carefully, you may lose track of what attributes the window has and garble the whole display. This is a particularly common problem when dealing with menus. So set up a policy for attributes in advance and stick to it. Oh, and you can also call the standend() function at any time, which does the same as attrset(A_NORMAL): it turns off all attributes and returns to normal mode.

Color

  • COLOR_BLACK: 0
  • COLOR_RED: 1
  • COLOR_GREEN: 2
  • COLOR_YELLOW: 3
  • COLOR_BLUE: 4
  • COLOR_MAGENTA: 5
  • COLOR_CYAN: 6
  • COLOR_WHITE: 7

Example

#include <ncurses.h>
#include <unistd.h>  /* only for sleep() */

int kbhit(void)
{
    int ch = getch();

    if (ch != ERR) {
        ungetch(ch);
        return 1;
    } else {
        return 0;
    }
}

int main(void)
{
    initscr();

    cbreak();
    noecho();
    nodelay(stdscr, TRUE);

    scrollok(stdscr, TRUE);
    while (1) {
        if (kbhit()) {
            printw("Key pressed! It was: %d\n", getch());
            refresh();
        } else {
            printw("No key pressed yet...\n");
            refresh();
            sleep(1);
        }
    }
}

Troubleshooting

curses.h no such file or directory

If the curses.h file cannot be found, install the ncurses library on CentOS as follows.

$ yum search ncurses
$ yum install ncurses-devel

Download

Ncurses-wrapper-class-example.zip
NCurses wrapper class example. (by. lcw)

See also

Favorite site

man page

References


  1. KLDPWiki-NCURSES-Programming-HOWTO.pdf ↩

  2. Joinc-ncurses_programming.pdf ↩

author Zer0 sitemap