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

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.

Python best practices

04 May, 2016

A short list of suggested reading about Python style and best practices:

Migrating pelicans

09 Apr, 2016

I have finally finished migrating this site from Wordpress to Pelican. Pelican is a static site generator written in Python.

My main reasons for moving from Wordpress to Pelican are:

  • Smaller attack surface.
  • I did not use one tenth of the features of Wordpress.
  • Written in Python, which I vastly prefer hacking on compared to PHP.
  • Easier to write and prepare blog posts in my editor of choice.
  • Integrates very well with git.

Migration was a breeze, most of the functionality I used from Wordpress are present via plugins. The only thing missing are comments, but that's not something I particularly miss. It is possible to integrate a comment system like Disqus with Pelican, or even use a clever system that converts emails to blog comments.

I have already written a small (crappy) plugin for Pelican that grabs RSS feeds for publishing on the index page.