1 import sys, os, io, termios, tempfile, subprocess
3 class autherror(Exception):
13 def message(self, msg, level=0):
15 def prompt(self, prompt, echo, default=None):
17 def image(self, image):
21 def __init__(self, ifp, ofp):
26 self.ofp.write("%s\n" % (msg,))
28 def message(self, msg, level=0):
29 if level <= self.msg_info:
30 self.ofp.write("%s\n" % (msg,))
32 def prompt(self, prompt, echo, default=None):
34 self.ofp.write(prompt)
36 ret = self.ifp.readline()
37 assert ret[-1] == '\n'
40 attr = termios.tcgetattr(self.ifp.fileno())
43 attr[3] &= ~termios.ECHO
44 termios.tcflush(self.ifp.fileno(), termios.TCIOFLUSH)
45 termios.tcsetattr(self.ifp.fileno(), termios.TCSANOW, attr)
46 self.ofp.write(prompt)
48 ret = self.ifp.readline()
50 assert ret[-1] == '\n'
53 termios.tcsetattr(self.ifp.fileno(), termios.TCSANOW, bka)
54 def image(self, image):
55 fd, fn = tempfile.mkstemp()
57 with os.fdopen(fd, "wb") as fp:
59 subprocess.call(["sxiv", fn])
63 class ctermconv(termconv):
64 def __init__(self, fp):
65 super().__init__(fp, fp)
72 def __exit__(self, *excinfo):
77 stdioconv = termconv(sys.stdin, sys.stdout)
80 return ctermconv(io.TextIOWrapper(io.FileIO(os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY), "r+")))