Wednesday, June 27, 2012

Command 'n' Cursor

I've been travelling with my trusty (but ageing) eeepc netbook this last week. There's much to love about it but it's starting to feel slow in comparison with my other machine.

Increasingly when I use the netbook I try to get away with doing things in a ctrl-alt-f1 shell without logging in to the GUI at all. I'm starting to wish more software could be used in this environment so I began to look at Curses, the standard library for text-window UIs. There's a convenient Python wrapper of course. And there's another nice library in Python : Cmd, for creating a command-line driven apps. That is, not programs that literally run as small tools on the shell with command-line arguments, but programs which have their own internal "repl" style loop which you drive by typing in commands. Cmd handily hides the details from you, letting you declare a subclass of the Cmd class which simply defines handlers for specific commands. It's not a million miles away from something I ended up writing to handle the commands in SdiDesk.

For some of my projects it would be useful to combine the two modes : to have Cmd style input driving a 2D textual output using Curses. Unfortunately Cmd and Curses don't obviously play well together.  Both of them want to take over the input, with Curses thinking in terms of keystrokes while Cmd still expects full lines.

Nevertheless, after a bit of exploration, and learning about Curses's textpads and Cmd's supplementary methods, I'm starting to get the two to co-operate. As this gist shows :



It doesn't do anything yet. Just handles a "greet NAME" command that prints "hello NAME". And a "quit" command that exits the program. But it has combined Cmd inputs with Curses output.

No comments: