text box with support for raw ansii input
See original GitHub issueFor my app, I really needed to be able to support getting verbatim output from other terminal apps and showing it to the user. These other commands might print color text, and I wanted the color to be preserved and shown to the user as-is. I’ve basically been approaching my asciimatics app as if it were a web app in the terminal; if I were doing a web app, I would be able to use any HTML along with the framework widgets, and I had not considered that a similar way of working with asciimatics and raw ansii codes would be difficult.
I dug around in the asciimatics code for a while and decided that implementing this as a widget would be… pretty difficult, at least for me. My workaround below might be useful for the library documentation.
First we create a new exception to hold the external command to be run and scene to resume once it’s finished:
class ExternalCallException(Exception):
def __init__(self, last_scene, command):
self.last_scene = last_scene
self.command = command
When the external command should be run, we raise the exception like so (self is the widget object):
raise ExternalCallException(self._scene, 'my command here')
Then we add an extra except
clause under the one handling screen resizes:
except ResizeScreenError as e:
last_scene = e.scene
# new code below
except ExternalCallException as e:
last_scene = e.last_scene
try:
# -K means quit on ^C; -R means print ansii codes as-is
process = Popen(e.command + " | less -K -R", shell=True)
process.wait()
except KeyboardInterrupt:
# let less handle this, -K will exit cleanly
pass
With this, the external command is piped to less
, which temporarily controls the whole screen and shows the command’s output in color. When the user quits less
, the app reappears using the same mechanism as when the screen is resized.
Issue Analytics
- State:
- Created 6 years ago
- Comments:17 (10 by maintainers)
The
more_colours
branch looks like it might be getting close to being a suitable framework. A bit more tidy up to do, then I need to check what happens when I need pass state between parsers. Once I have that working, we can go live.One other related feature I would love to have is related to the ability to print raw ansi. I would love to be able to use iTerm’s image display capability. It uses an extension of the ansi escape codes. Even if it’s not the best to have in core asciimatics because it isn’t portable, it would be nice to have a mechanism where I could do this myself (by printing the raw ansi code at some location).