I’d like to be able to have the DevTerm in a CLI REPL, such as a text game, a Python interpreter, etc, and keep a live trace of the session printed on thermal paper.
Here are some experiments I’ve done:
I’ve used script(1) from util-linux, which is functional but a bit overzealous:
script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.
I agree with this statement from the manpage, but would have hoped for an alternative to be mentioned alongside. In practice, you get extra characters (carriage returns show up as music notes) and you can see backspaces, etc.
$ script - | tee /tmp/DEVTERM_PRINTER_IN
$ reset
$ fizmo-console Zork1.dat
...
I couldn’t find any way to convert this raw output into something cleaner (apply ^H linefeed operations, etc), but I’m sure it exists.
Alternately I tried just teeing stdout+stderr to the printer, but I feel like the print is a bit useless without input.
I also wish I could print with a larger font than the 5 provided by default. If printing a .txt file with lp I get a huge font which I’m not fond of. I guess I could probably add another medium size ttf (maybe of Terminus or some similar bitmap font) and recompile the driver, but I don’t feel like doing that right now.
Another thing I tried is coding a basic duplicating REPL in Python but I haven’t really got anywhere yet:
#!/usr/bin/env python3
from io import StringIO
import sys
import logging
import traceback
with open("/tmp/DEVTERM_PRINTER_IN", "w") as printer:
# Use least tiny font
printer.write("\x1b\x21\x04")
while True:
try:
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
old_stdout.write("> ")
old_stdout.flush()
cmd = input()
printer.write(f"> {cmd}\n")
out = eval(cmd)
sys.stdout = old_stdout
message = mystdout.getvalue()
print(message, end="")
printer.write(message)
if out is not None:
print(out)
printer.write(f"{out}\n")
except EOFError:
printer.write("\n" * 14)
break
except Exception:
err = traceback.format_exc()
logging.error(err)
printer.write(err)
sys.stdout = old_stdout
Neither eval
nor exec
is really what I want, and the whole exchanging stdout thing is still messy. Also I’m using \x04
for the font size instead of the documented \x4
because the latter is invalid in Python strings; but I’m not entirely sure it’s equivalent.
I don’t have a proper print of the output on hand but you can’t easily get return values printed with either print
s out as well or manipulating variables …
I also looked through stty but haven’t found anything useful.
Is anyone else interested in this kind of project?