#!/usr/bin/python -tt # vim:ts=8:sts=4:sw=4:et: from subprocess import * import sys import os, select special = { # trees: Clubs looks more like a tree than the +- symbol '\xf1': u'\u2663', # tree (clubs) # fountains: This is supposed to be just the top half of the integral # symbol, but my fonts don't have that, so we'll use the whole symbol '\xf4': u'\u222b', # Spiderweb: sun symbol '\xe2': u'\u263c', # Special cases for rogue levels '\x01': u'\u263a', # human, elf - smiley '\x04': u'\u2666', # trap, web - dimaond # These are supposed to be used on rogue levels, but don't appear # to be used when I try them... #'\x05': u'\u2663', # food - clubs #'\x0c': u'\u2640', # amulet - female #'\x0e': u'\u266b', # scroll - note #'\x0f': u'\u263c', # gold, gems - sun #'\x18': u'\u2191', # weapon - up arrow } if len(sys.argv) < 2: print "usage: ibmfilter [command]" print "Runs command in a subshell and translates its output from ibm473 codepage to UTF-8." sys.exit(0) handle = Popen(sys.argv[1:], stdout=PIPE, bufsize=1) buf = handle.stdout.read(1) while buf != '': if buf in special: out = special[buf] else: out = buf.decode('ibm437') writeable = select.select([], [sys.stdout], [], 5)[1] if not writeable: os.kill(handle.pid) os.system('reset') raise Exception("Timed out while waiting for stdout to be writeable...") sys.stdout.write(out.encode('UTF-8')) buf = handle.stdout.read(1) handle.wait()