Cross-platform curses color CLI - guide

Posted on January 15, 2014

Wow. Never choose a library without being aware of what other options exist. Sometimes this requires some research, but it will pay off. The thing is that I was fiddling with making a color-enabled python script for the windows command prompt. I had tried the default curses library, but that wouldn’t catch on. Then I tried Colorama and Blessings. Both are very good, but curses gives more options when it comes to interfaces. I finally settled on Colorama. It’s what I’ll use if I only need to print some pretty lines. For a full-blown interface I’ll use UniCurses. Yeah, it’s great. It’s everything curses is, but it’s cross-platform – even the color.

There’s two ways to make this work. The second way will disable all panel functionality from unicurses, so only use it when desperate. Also make sure you don’t already have the binary curses installed in Python if you opt for the first method and you’re using Windows. When unicurses detects a curses install it tries to use it, but it assumes that it’s the linux version, which doesn’t have the same functionality as the windows one. So bottom line, under Windows you need to install PDCurses and uninstall curses, or keep using curses albeit with missing features. Or just keep both installed and modify unicurses.py to use PDCurses if on Windows. Phew. That being said, there you go:

First way

Install UniCurses.

A unicurses folder should now reside in YOUR_PYTHON_PATH\Lib\site-packages\.

Inside you will find demos and docs.

Get pdcurses.dll and associated C files from here.

Extract them to YOUR_PYTHON_PATH\Lib\site-packages\ or wherever your unicurses.py resides. By default unicurses.py is found at YOUR_PYTHON_PATH\Lib\site-packages\.

You’re done. Run the demos to make sure nothing messed up.

If you get an exception saying something like “I didn’t find your pdcurses.dll” or whatever, try commenting out lines 52-54 in unicurses.py. In my case it was doing an unnecessary check that wasn’t returning the right value.

Second way

For Windows, you’ll need to get a binary package of curses right here.

Then install UniCurses.

A unicurses folder should now reside in YOUR_PYTHON_PATH\Lib\site-packages\

Inside you will find demos and docs.

Run a demo. You will soon find that it throws an exception. Simply comment out the if block at line 649 in YOUR_PYTHON_PATH\Lib\site-packages\unicurses.py