6 class AbortOnGetkey(Exception):
15 self.do_refresh = True
16 self.store_widechar = False
17 curses.wrapper(self.run_loop)
19 def addstr(self, y, x, line, attr=0):
20 if y < self.size.y - 1 or x + len(line) < self.size.x:
21 self.stdscr.addstr(y, x, line, attr)
22 else: # workaround to <https://stackoverflow.com/q/7063128>
23 cut_i = self.size.x - x - 1
25 last_char = line[cut_i]
26 self.stdscr.addstr(y, self.size.x - 2, last_char, attr)
27 self.stdscr.insstr(y, self.size.x - 2, ' ')
28 self.stdscr.addstr(y, x, cut, attr)
31 from plomrogue.mapping import YX
32 self.size = YX(*self.stdscr.getmaxyx())
33 self.size = self.size - YX(self.size.y % 4, 0)
34 self.size = self.size - YX(0, self.size.x % 4)
38 self.do_refresh = True
41 curses.curs_set(0) # hide cursor
42 self.stdscr.timeout(10)
45 def get_key_and_keycode(self):
47 key = self.stdscr.getkey()
53 # workaround for <https://stackoverflow.com/a/56390915>
54 if self.store_widechar:
55 self.store_widechar = False
56 key = bytes([195, keycode]).decode()
58 self.store_widechar = True
62 def run_loop(self, stdscr):
66 self.on_each_loop_start()
67 for msg in self.socket.get_message():
68 self.handle_server_message(msg)
72 self.do_refresh = False
74 key, keycode = self.get_key_and_keycode()
77 self.on_key(key, keycode)
78 self.do_refresh = True
82 def msg_into_lines_of_width(msg, width):
86 for i in range(len(msg)):
87 if x >= width or msg[i] == "\n":