Site Tools


h-curses

Table of Contents

<curses.h>

<curses.h> (typically <ncurses.h>) provides terminal control for building text-based user interfaces: window creation, cursor positioning, text attributes, and input handling. It translates portable abstractions into the right escape sequences for any terminal.

Use it to build full-screen TUI applications that work across different terminal types.

Example

This example initializes ncurses, draws text at a specific position, and waits for user input.

// compile: gcc -o cursesexample cursesexample.c -lncurses
// run: ./cursesexample
// description: draw positioned text and handle input
 
#include <ncurses.h>
#include <unistd.h>
 
int main() {
    initscr();
    int h, w;
    getmaxyx(stdscr, h, w);
 
    mvprintw(h/2, w/2 - 8, "Hello, ncurses!");
    mvprintw(h/2 + 1, w/2 - 10, "Terminal: %d x %d", w, h);
    refresh();
 
    sleep(2);
    endwin();
    return 0;
}

Common functions

Initialization and cleanup:

  • initscr(): initialize ncurses, return main window
  • endwin(): clean up and restore terminal
  • refresh(): update screen display
  • clear(): clear screen
  • cbreak(): disable line buffering
  • noecho(): disable echo of user input

Window management:

  • newwin(h, w, y, x): create new window
  • delwin(w): delete window
  • mvwin(w, y, x): move window
  • getmaxyx(w, &y, &x): query window size
  • stdscr: standard screen (main window)

Cursor and output:

  • move(y, x): move cursor
  • mvprintw(y, x, fmt, ...): print at position
  • mvaddstr(y, x, str): add string at position
  • addch(c): add single character
  • attron(attr), attroff(attr): enable/disable text attributes
  • A_BOLD, A_DIM, A_REVERSE, A_STANDOUT: text attributes

Input:

  • getch(): get character from input
  • getstr(s): get string input
  • scanw(fmt, ...): formatted input

Colors (if supported):

  • has_colors(): check if terminal supports colors
  • start_color(): enable color mode
  • init_pair(id, fg, bg): define color pair
  • COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, etc.: color constants
h-curses.md · Last modified: by 127.0.0.1