Curses notes

From Helpful
Jump to navigation Jump to search
This article/section is a stub — probably a pile of half-sorted notes and is probably a first version, is not well-checked, so may have incorrect bits. (Feel free to ignore, or tell me)


curses, ncurses

curses is an API that lets you write text-mode interfaces, and makes it more terminal-independent by basing its behaviour on terminfo/termcap.


(In the slow-remote-shell days it also mattered that it tried to send only the changes)


ncurses is the modern implementation, which evolved from BSD curses and SysV curses.

ncurses was originally a clone, but now its own thing.

ncurses is a C library, though various wrappers exist. While most are fairly thin wrappers, they may still be less code for the same thing.


Command line wrappers for some simple dialogs

Related:

  • dialog, built on curses/ncurses, provides GUI-like widgets in text mode from a script-like description
  • newt, effectively an alternative to ncurses
  • whiptail, like dialog (and largely compatible) but built on newt. Simple example

Example:

dialog   --title "Example" --msgbox "Hit OK to continue." 8 78
whiptail --title "Example" --msgbox "Hit OK to continue." 8 78



Colors

This article/section is a stub — probably a pile of half-sorted notes and is probably a first version, is not well-checked, so may have incorrect bits. (Feel free to ignore, or tell me)

See has_colors() to check whether the terminal supports colors

call start_color()

after initscr, before any color manipulation

ncurses stores color pairs (foreground and background) and you refer to drawing colors via that pair number.

call init_pair(pair_number, fg, bg) to initialize colors. It returns ints that are an enumeration of to pairs you have initialised.


The standard eight-ish colors (plus brightness) have a numbering as they do in shells, so in basic use you would call

init_pair(colorpair, fg, bg);

where fg and bg are:

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

If you called use_default_colors()) you can use -1 for bg or fg on init_pair(), for default.


You can have

  • up to COLORS amount of colors defined,
  • up to COLOR_PAIRS amount of pairs defined


Their values depend a lot on your TERM (see also Terminal colors).


For example, it seems that for

  • vt100, both are 0
  • xterm or screen, then COLORS is probably 8 and COLOR_PAIRS probably 64
you should probably cover assume a bunch of people will get no better, and have a case for it
  • xterm+88color, then COLORS is probably 88 and COLOR_PAIRS probably 256
  • screen-256color, then probably both are 256.
  • there are other possibilities

And true color has been introduced in ncurses 6 (verify)



See also