ncurses

Language: C

CLI/Utils

ncurses was developed as a free-software emulation of the proprietary curses library, originally created for BSD Unix systems. It enables developers to build interactive command-line applications with advanced text-based UIs, such as menus, forms, and dashboards.

ncurses is a programming library that provides an API for creating text-based user interfaces in a terminal-independent manner. It supports windows, colors, keyboard input, and mouse events in terminal applications.

Installation

linux: sudo apt install libncurses5-dev libncursesw5-dev
mac: brew install ncurses
windows: Use Cygwin or MSYS2 to install ncurses for Windows development

Usage

ncurses allows developers to control cursor movement, handle keyboard input, display colored text, create multiple windows, and manage terminal screens efficiently. It is widely used for CLI-based applications, system monitors, text editors, and terminal games.

Initializing and ending ncurses

#include <ncurses.h>

int main() {
    initscr(); // Start ncurses mode
    printw("Hello, ncurses!");
    refresh(); // Print on screen
    getch();   // Wait for user input
    endwin();  // End ncurses mode
    return 0;
}

Initializes the ncurses environment, prints text to the screen, waits for a key press, and then exits cleanly.

Using colors

#include <ncurses.h>

int main() {
    initscr();
    start_color();
    init_pair(1, COLOR_RED, COLOR_BLACK);
    attron(COLOR_PAIR(1));
    printw("Red Text");
    attroff(COLOR_PAIR(1));
    refresh();
    getch();
    endwin();
    return 0;
}

Demonstrates creating a color pair and using it to display colored text in the terminal.

Creating multiple windows

#include <ncurses.h>

int main() {
    initscr();
    WINDOW *win1 = newwin(10, 20, 0, 0);
    WINDOW *win2 = newwin(10, 20, 5, 25);
    box(win1, 0, 0);
    box(win2, 0, 0);
    wrefresh(win1);
    wrefresh(win2);
    getch();
    delwin(win1);
    delwin(win2);
    endwin();
    return 0;
}

Shows how to create two separate windows, draw boxes around them, and refresh each individually.

Handling keyboard input

#include <ncurses.h>

int main() {
    initscr();
    keypad(stdscr, TRUE);
    printw("Press arrow keys (ESC to exit)\n");
    int ch;
    while((ch = getch()) != 27) {
        switch(ch) {
            case KEY_UP: printw("Up\n"); break;
            case KEY_DOWN: printw("Down\n"); break;
            case KEY_LEFT: printw("Left\n"); break;
            case KEY_RIGHT: printw("Right\n"); break;
            default: printw("Key code: %d\n", ch);
        }
        refresh();
    }
    endwin();
    return 0;
}

Enables arrow key input and responds to different keys in a loop until the ESC key is pressed.

Menus and forms

// Use <menu.h> and <form.h> headers to create interactive menus and forms (requires linking with -lmenu -lform).

Demonstrates how ncurses can be extended to create complex interactive text-based user interfaces.

Error Handling

Unable to initialize ncurses: Ensure terminal supports ncurses and that your program links against `-lncurses`.
KEY_* constants not recognized: Call `keypad(stdscr, TRUE)` or on the specific window to enable special key input.

Best Practices

Always call `initscr()` before using ncurses functions and `endwin()` before exiting.

Use `refresh()` and `wrefresh()` appropriately to update the screen or windows.

Enable keypad mode when handling special keys.

Use color pairs instead of direct color codes for portability.

Clean up all windows with `delwin()` to avoid memory leaks.