Notes about working with curses

21 May, 2016

A few notes on working with curses (on Python) as I seem to have to rediscover this for every new project.

Curses work with Y, X coordinates, upper left corner is 0, 0. X is horizontal, Y vertical.

Zero based naturally, so on a normal 24x80 terminal the last positions are 23, 79 (lower right corner).

Curses coordinates

getch() is normally blocking so if you update curses in a loop and want to check for input you need to set timeout(n) for the screen.

If you don't need a cursor (for input) disabling it with curs_set(0) seems to get less flicker when redrawing the screen.

Each successive call to addstr() is relative to previous call by default.

Some keys, like Escape (and Alt and arrow keys) send two keycodes. To handle this you need to check for key 27, then immediately after make another call to getch() and check for the next keycode. In the case of Escape there is no second. (If you use timeout() for non-blocking input, you can disable it with nodelay() and afterwards enable it again).

Escape has a delay by default, to disable it set the environment variable ESCDELAY to a suitable value. In Python this is as easy as

import os
os.environ['ESCDELAY']='25'

To handle UTF-8 input, use get_wch()

Color pair 0 can't be used, it's always white text on black background.
Color pair is foreground (text), background.