1 import sys, os, io, termios
10 def message(self, msg, level=0):
12 def prompt(self, prompt, echo, default=None):
16 def __init__(self, ifp, ofp):
21 self.ofp.write("%s\n" % (msg,))
23 def message(self, msg, level=0):
24 if level <= self.msg_info:
25 self.ofp.write("%s\n" % (msg,))
27 def prompt(self, prompt, echo, default=None):
29 self.ofp.write(prompt)
31 ret = self.ifp.readline()
32 assert ret[-1] == '\n'
35 attr = termios.tcgetattr(self.ifp.fileno())
38 attr[3] &= ~termios.ECHO
39 termios.tcflush(self.ifp.fileno(), termios.TCIOFLUSH)
40 termios.tcsetattr(self.ifp.fileno(), termios.TCSANOW, attr)
41 self.ofp.write(prompt)
43 ret = self.ifp.readline()
45 assert ret[-1] == '\n'
48 termios.tcsetattr(self.ifp.fileno(), termios.TCSANOW, bka)
50 class ctermconv(termconv):
51 def __init__(self, fp):
52 super().__init__(fp, fp)
59 def __exit__(self, *excinfo):
64 stdioconv = termconv(sys.stdin, sys.stdout)
67 return ctermconv(io.TextIOWrapper(io.FileIO(os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY), "r+")))