Skip to content

ncurses #

ncurses

Thin bindings for the system ncurses or curses library.

Supported targets:

  • macOS
  • Linux
  • BSD and Solaris targets that provide a compatible curses library
  • Windows currently reports ncurses.is_supported() == false

Most functions return ncurses.ok on success and ncurses.err on failure. getch() and wgetch() return an int. Compare special keys with ncurses.key_*.

import ncurses

fn main() {
    if !ncurses.is_supported() {
        return
    }
    stdscr := ncurses.initscr()
    defer {
        ncurses.endwin()
    }
    ncurses.cbreak()
    ncurses.noecho()
    ncurses.keypad(stdscr, true)
    ncurses.addstr('Press a key...')
    ncurses.refresh()

    key := ncurses.getch()
    ncurses.mvaddstr(1, 0,'Key code: ${key}')
    ncurses.refresh()
    ncurses.getch()
}

Use newwin, box, waddstr, wrefresh, and wgetch for additional windows. Use start_color, init_pair, and color_pair for color attributes.

fn addstr #

fn addstr(text string) int

addstr writes a string to the default screen at the current cursor position.

fn attroff #

fn attroff(attr int) int

attroff disables an attribute on the default screen.

fn attron #

fn attron(attr int) int

attron enables an attribute on the default screen.

fn box #

fn box(win Window, vertical int, horizontal int) int

box draws a border around a window. Pass 0 for the default border characters.

fn cbreak #

fn cbreak() int

cbreak disables line buffering while keeping signal keys active.

fn clear #

fn clear() int

clear clears the default screen buffer.

fn color_pair #

fn color_pair(pair int) int

color_pair returns the attribute mask for a color pair number.

fn curs_set #

fn curs_set(visibility int) int

curs_set changes cursor visibility using the module cursor constants.

fn delwin #

fn delwin(win Window) int

delwin destroys a window created by newwin.

fn echo #

fn echo() int

echo enables terminal echo for characters typed by the user.

fn endwin #

fn endwin() int

endwin restores the terminal after initscr.

fn getch #

fn getch() int

getch reads one key code from the default screen.

fn getmaxx #

fn getmaxx(win Window) int

getmaxx returns the width of a window in columns.

fn getmaxy #

fn getmaxy(win Window) int

getmaxy returns the height of a window in rows.

fn has_colors #

fn has_colors() bool

has_colors reports whether the terminal supports colors.

fn init_pair #

fn init_pair(pair int, fg int, bg int) int

init_pair defines a foreground/background color pair for use with color_pair.

fn initscr #

fn initscr() Window

initscr initializes the curses screen and returns the default window handle.

fn is_supported #

fn is_supported() bool

is_supported reports whether this target has an ncurses backend in the standard library.

fn key_f #

fn key_f(n int) int

key_f returns the key code for function keys like F1, F2, and so on.

fn keypad #

fn keypad(win Window, enabled bool) int

keypad enables or disables keypad processing for a window.

fn mvaddstr #

fn mvaddstr(y int, x int, text string) int

mvaddstr moves the cursor on the default screen and writes a string there.

fn mvwaddstr #

fn mvwaddstr(win Window, y int, x int, text string) int

mvwaddstr moves the cursor in a window and writes a string there.

fn newwin #

fn newwin(lines int, cols int, begin_y int, begin_x int) Window

newwin creates a new curses window.

fn nocbreak #

fn nocbreak() int

nocbreak restores normal line buffering after cbreak.

fn nodelay #

fn nodelay(win Window, enabled bool) int

nodelay enables or disables nonblocking reads for a window.

fn noecho #

fn noecho() int

noecho disables terminal echo for characters typed by the user.

fn noraw #

fn noraw() int

noraw disables raw input mode.

fn raw #

fn raw() int

raw enables raw input mode.

fn refresh #

fn refresh() int

refresh flushes pending changes for the default screen.

fn start_color #

fn start_color() int

start_color enables ncurses color support.

fn stdscr #

fn stdscr() Window

stdscr returns the current default screen window handle.

fn timeout #

fn timeout(delay int)

timeout sets the blocking behavior for getch on the default screen.

fn waddstr #

fn waddstr(win Window, text string) int

waddstr writes a string to a specific window at its current cursor position.

fn wattroff #

fn wattroff(win Window, attr int) int

wattroff disables an attribute on a specific window.

fn wattron #

fn wattron(win Window, attr int) int

wattron enables an attribute on a specific window.

fn wclear #

fn wclear(win Window) int

wclear clears a specific window.

fn wgetch #

fn wgetch(win Window) int

wgetch reads one key code from a specific window.

fn wmove #

fn wmove(win Window, y int, x int) int

wmove moves the cursor inside a specific window.

fn wrefresh #

fn wrefresh(win Window) int

wrefresh flushes pending changes for a specific window.

fn wtimeout #

fn wtimeout(win Window, delay int)

wtimeout sets the blocking behavior for wgetch on a specific window.

type Window #

type Window = voidptr

Window is an opaque handle to an ncurses window.