Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a static framed ASCII interface in Python?

When I use the less command in my Mac Terminal I'm shown a 23 row slice of the specified file. If I move down the file, then scroll up in the terminal window, I don't see any file content before the current slice. Instead, I see the commands I typed before using less.

I would like to use this or a similar effect to create an ascii game interface that shows only the current screen, with no history. I would draw my frames on screen and change only the text or options within those frames. This is very common in older systems that ran mostly or entirely in a command line environment.

Is there a Python module that offers this? Is it an effect that I can or should implement myself?

Here are some example screen elements that could benefit from this effect.

+-------------------------------------------------------------------+
|                                                                   |
| Dialog dialog dialog dialog dialog dialog dialog dialog dialog    |
| dialog dialog dialog dialog dialog dialog dialog dialog dialog    |
| dialog dialog dialog dialog dialog dialog dialog dialog dialog    |
| dialog dialog dialog dialog dialog dialog dialog dialog dialog    |
| dialog dialog dialog dialog.                                      |
|                                                                   |
|                                                                   |
|                                                                   |
+--------------------+---------------+------------------------------+
|   Attack           |     South     |            HP/Max   MP/Max   |
| > Cast Magic       |       -       |   Tom:    120/120   60/91    |
|   Use Item         |      New      | > Dick:    27/133   47/47    |
|   Tactics          |  Schwartzton  |   Harry:   87/104   16/113   |
+--------------------+---------------+------------------------------+
like image 495
Jack Stout Avatar asked Jul 01 '12 18:07

Jack Stout


3 Answers

I've been struggling with this issue too. I wanted a simple cross platform solution, so I extended asciimatics to provide a set of widgets to create form-based UIs like this. For example:

Text UI widgets

The resulting code should be quite concise and work on any modern OS (including Windows, Linux and OSX) without installing extra native libraries. As you can see from the contact list sample the standard widgets are easy to lay out and should provide most of your needs.

like image 62
Peter Brittain Avatar answered Oct 18 '22 01:10

Peter Brittain


I would suggest looking into the curses module. It's an interface to the ncurses library, which is used for exactly this purpose (I believe many terminal applications are written in it). The documentation is quite decent, and there are several nice tutorials that should also set you in the right direction.

(Either that or liberal use of \r.) I would strongly suggest the curses module, since it should provide more flexibility for what you're looking to do.

EDIT: If you're interested in creating a cross-platform application, keep in mind that the curses module is only available on *nix systems. Per the documentation:

No one has made a Windows port of the curses module.

That tutorial recommends this library for use on Windows, although if the dates on the download page are any indication, it has not been updated in a fair amount of time. You could look into this module, which looks considerably more recent.

like image 6
Ricardo Altamirano Avatar answered Oct 18 '22 01:10

Ricardo Altamirano


What you're talking about is curses. It is a Python module that interfaces with ncurses. The best tutorial that I've found for using curses is this one: http://www.dev-explorer.com/articles/python-with-curses

Some popular programs that use curses (albeit from another language) are aptitude and nano.

like image 3
C0deH4cker Avatar answered Oct 18 '22 02:10

C0deH4cker