Jack Stout Jack Stout - 23 days ago 6
Python Question

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 |
+--------------------+---------------+------------------------------+

Answer

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.