def test_getkey(self): raw_getkey = term.impl.raw_getkey def gk(*rets): #all of the yuck i = [0] def mock(): if i[0] == len(rets): return r = rets[i[0]] i[0] = i[0] + 1 return r term.impl.raw_getkey = mock try: gk('a') self.assertEqual(term.getkey(), 'a') gk('C') self.assertEqual(term.getkey(), 'C') gk('pgdn') self.assertEqual(term.getkey(), 'pgdn') gk(None, 'b') self.assertEqual(term.getkey(), 'b') gk('\x01') self.assertEqual(term.getkey(), 'ctrl-a') gk('\x03') self.assertRaises(KeyboardInterrupt, term.getkey) finally: term.impl.getkey = raw_getkey
def main(): logging.info("Starting quill.") term.color(0, 7) term.clear() term.color(7, 0) T = Tab((0, 0), term.size, True) k = term.getkey() while k != "^C": T.handle(k) k = term.getkey() logging.info("Exiting.")
def defeat_prompt(): import message key = term.getkey() if key == 'down': message.scroll_message(rel=-1) if key == 'up': message.scroll_message(rel=1)
def title_prompt(): while True: c = term.getkey() if c == 'enter': event.fire('explore.start') state.mode = 'explore' raise state.StateChanged()
def skip_test_blink_timing(self): import time term.set_cursor_type(1) term.move_cursor(1, 1) t = buffer.PlainText("Time!", x=0, y=0) t.draw() start = time.time() while time.time() - start < 2: term.flip() t.set("Keys!") t.draw() term.flip() for i in range(10): term.getkey()
def level_prompt(): while True: event.fire('flip') ret = False key = term.getkey() if key in ('up', 'down', 'left', 'right') and not level.been_cursed: message.newline() message.add("The curse of Melimnor takes hold upon you!") state.player.add_limb() message.newline() level.been_cursed = True if key == 'enter': return elif key == 'f3' and False: #TAKE ME OUT BEFORE RELEASE state.player.mutate() elif key == 'f4' and False: #TAKE ME OUT BEFORE RELEASE state.player.add_limb() elif key == 'up': ret = level.layout.curr_room.try_move(y=-1) elif key == 'down': ret = level.layout.curr_room.try_move(y=+1) elif key == 'left': ret = level.layout.curr_room.try_move(x=-1) elif key == 'right': ret = level.layout.curr_room.try_move(x=+1) log.debug('ret: %r', ret) if not ret: continue elif ret is True: return else: action, args = ret log.debug("action: %r, args: %r", action, args) if action == 'changeroom': level.layout.change_room(*args) add_room_messages() draw_explore() elif action == 'changelevel': log.debug("changing level") level.layout = layouts.random_layout() state.found_key = False add_room_messages() draw_explore() message.add("<LIGHTRED>The stairs vanish behind you!") return
def test_getkey(self): raw_getkey = term.impl.raw_getkey def gk(*rets): #all of the yuck i = [0] def mock(): if i[0] == len(rets): return r = rets[i[0]] i[0] = i[0] + 1 return r term.impl.raw_getkey = mock try: gk('a'); self.assertEqual(term.getkey(), 'a') gk('C'); self.assertEqual(term.getkey(), 'C') gk('pgdn'); self.assertEqual(term.getkey(), 'pgdn') gk(None, 'b'); self.assertEqual(term.getkey(), 'b') gk('\x01'); self.assertEqual(term.getkey(), 'ctrl-a') gk('\x03'); self.assertRaises(KeyboardInterrupt, term.getkey) finally: term.impl.getkey = raw_getkey
def _writer(self, fullmode=False): """Loop and copy console->serial until EOF character is found""" while self._resume: try: c = getkey(fullmode) if fullmode and ord(c) == 0x1: # Ctrl+A self._cleanup() return else: self._port.write(c) except KeyboardInterrupt: print '%sAborting...' % os.linesep self._cleanup() return
def connect_over_serial(url, baudrate): from term import getkey from sys import platform, stdin, stdout, stderr MSWIN = platform == 'win32' if not MSWIN: from termios import TCSANOW, tcgetattr, tcsetattr if not MSWIN and stdout.isatty(): termstates = [(fd, tcgetattr(fd)) for fd in (stdin.fileno(), stdout.fileno(), stderr.fileno())] from pyftdi.serialext import serial_for_url try: port = serial_for_url(url, baudrate=baudrate) except SerialException as e: print("Uh-oh:", e) from pyftdi.ftdi import Ftdi Ftdi().open_from_url('ftdi:///?') sys.exit(1) print("Connected.") try: while True: try: c = getkey(False) if MSWIN and ord(c) == 3: raise KeyboardInterrupt() stdout.write(c.decode('utf8', errors='replace')) stdout.flush() port.write(c) except KeyboardInterrupt: port.close() print("kbai") break finally: for fd, att in termstates: tcsetattr(fd, TCSANOW, att)
def _writer(self, fullmode, silent, localecho, crlf=0): """Loop and copy console->serial until EOF character is found""" while self._resume: try: c = getkey(fullmode) if mswin: if ord(c) == 0x3: raise KeyboardInterrupt() if fullmode and ord(c) == 0x2: # Ctrl+B self._cleanup() return if silent: if ord(c) == 0x6: # Ctrl+F self._silent = True print('Silent\n') continue if ord(c) == 0x7: # Ctrl+G self._silent = False print('Reg\n') continue else: if localecho: stdout.write(c.decode('utf8', errors='replace')) stdout.flush() if crlf: if c == b'\n': self._port.write(b'\r') if crlf > 1: continue self._port.write(c) except KeyboardInterrupt: if fullmode: continue print('%sAborting...' % linesep) self._cleanup() return
def _writer(self, fullmode=False): """Loop and copy console->serial until EOF character is found""" while self._resume: try: keybytes = getkey(fullmode) if keybytes is None: continue c = keybytes[0] if platform == 'win32': if c == 0x3: raise KeyboardInterrupt() if self._twecmd: if (fullmode and c == 0x1) or c == 0x18 or c == ord( 'x'): # Ctrl+A, X, 'x' to exit self._cleanup() return elif c == ord('c'): # type Ctrl+C self._port.write(b'\x03') elif c == ord('l') or c == 12: self.screen_clear() elif c == 0x12 or c == ord('r'): # Ctrl+R # reset TWELITE thru FTDI bitbang self.screen_print("[RESET TWE]") self._port.udev.set_bitmode(0xFB, 0x20) self._port.udev.set_bitmode(0xFF, 0x20) elif c == 0x9 or c == ord('i'): # Ctrl+I # just press '+' three times for interactive mode. self.screen_print("[+ + +]") stdout.flush() self._port.write(b'+') time.sleep(0.5) self._port.write(b'+') time.sleep(0.5) self._port.write(b'+') elif c == ord( 'A'): # input ASCII format, output ASCII format # ASCII format analysis self.screen_print("[FMT: console ASCII, serial ASCII]") self._twefmt_serail = FmtAscii() self._tweformat = TWEDict.format_ascii elif c == ord( 'B'): # input ASCII format, outout BINARY format # BINARY format analysis self.screen_print( "[FMT: console ASCII, serial BINARY]") self._twefmt_serail = FmtBinary() self._tweformat = TWEDict.format_binary elif c == ord('N'): # format none self.screen_print("[FMT: none]") self._tweformat = TWEDict.format_none else: self.screen_print("[Canceled]") self.screen_string_black() stdout.flush() self._twecmd = False elif self._tweformat != TWEDict.format_none: # console input should be handled as ASCII format self._twefmt_console.process(c) print(chr(c), end='') # echo back stdout.flush() # when format is complete, send it to serial if self._twefmt_console.is_comp(): if self._twefmt_serail is not None: self._port.write( self._twefmt_serail.S_output( self._twefmt_console.get_payload())) self._twefmt_console.reinit() # clean it anyway else: self._port.write(keybytes) except KeyboardInterrupt: if self._twecmd: self.screen_print("[Exit]") self.screen_string_black() self._cleanup() return else: self._twecmd = True self.screen_string_red() self.screen_print( "*** r:reset i:+++ A:ASCFMT B:BINFMT x:exit>", end='') stdout.flush() continue
return buf if __name__ == "__main__": import WConio as W x = 0 def show(filename): global x f = open(filename) buf = read_to_buffer(f) buf.x = x x += buf.width buf.draw() try: import sys if not len(sys.argv) > 1: print "nom nom need argument" else: term.init() show(sys.argv[1]) if len(sys.argv) > 2: show(sys.argv[2]) term.flip() W.settitle("Mutants Of Melimnor") term.getkey() except: raise finally: W.textmode() W.textcolor(W.LIGHTGREY)