home · contact · privacy
More TUI client refactoring.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 6 Jun 2021 23:27:30 +0000 (01:27 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 6 Jun 2021 23:27:30 +0000 (01:27 +0200)
plomrogue_client/tui.py
rogue_chat_curses.py

index 7cfc01139c35a8cbb75aa0039640fc353b98a109..9f28a04c542ba586ad3650679c43c70b3fd24734 100644 (file)
@@ -3,10 +3,17 @@ import curses
 
 
 
+class AbortOnGetkey(Exception):
+    pass
+
+
+
 class TUI:
 
     def __init__(self):
         self._log = []
+        self.do_refresh = True
+        self.store_widechar = False
         curses.wrapper(self.run_loop)
 
     def addstr(self, y, x, line, attr=0):
@@ -35,11 +42,32 @@ class TUI:
         self.stdscr = stdscr
         self.init_loop()
         while True:
-            self.loop()
+            try:
+                self.loop()
+            except AbortOnGetkey:
+                continue
+            self.do_refresh = True
 
     def log(self, msg):
         self._log += [msg]
+        self.do_refresh = True
 
+    def get_key_and_keycode(self):
+        try:
+            key = self.stdscr.getkey()
+        except curses.error:
+            raise AbortOnGetkey
+        keycode = None
+        if len(key) == 1:
+            keycode = ord(key)
+            # workaround for <https://stackoverflow.com/a/56390915>
+            if self.store_widechar:
+                self.store_widechar = False
+                key = bytes([195, keycode]).decode()
+            if keycode == 195:
+                self.store_widechar = True
+                raise AbortOnGetkey
+        return key, keycode
 
 
 def msg_into_lines_of_width(msg, width):
index f798047136b271c0be140f299da0645b33ece7e0..60c46b270b30dac481408f23bc46181f79288732 100755 (executable)
@@ -498,7 +498,6 @@ class RogueChatTUI(TUI):
         self.game = Game()
         self.game.tui = self
         self.parser = Parser(self.game)
-        self.do_refresh = True
         self.login_name = None
         self.map_mode = 'terrain + things'
         self.password = 'foo'
@@ -558,7 +557,6 @@ class RogueChatTUI(TUI):
         self.offset = YX(0,0)
         self.explorer = YX(0, 0)
         self.input_ = ''
-        self.store_widechar = False
         self.input_prompt = '> '
         self.action_descriptions = {
             'move': 'move',
@@ -1186,21 +1184,7 @@ class RogueChatTUI(TUI):
             self.do_refresh = False
         for msg in self.socket.get_message():
             handle_input(msg)
-        try:
-            key = self.stdscr.getkey()
-            self.do_refresh = True
-        except curses.error:
-            return
-        keycode = None
-        if len(key) == 1:
-            keycode = ord(key)
-            # workaround for <https://stackoverflow.com/a/56390915>
-            if self.store_widechar:
-                self.store_widechar = False
-                key = bytes([195, keycode]).decode()
-            if keycode == 195:
-                self.store_widechar = True
-                return
+        key, keycode = self.get_key_and_keycode()
         self.show_help = False
         self.draw_face = False
         if key == 'KEY_RESIZE':