From: Christian Heller Date: Wed, 23 Aug 2017 22:49:11 +0000 (+0200) Subject: Specify widget classes, names by their use. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Bdb.prefix%7D%7D/%7B%7Bitem_name%7D%7D?a=commitdiff_plain;h=b05efdc0d63180090a1c945235e47991e10218fa;p=plomrogue2-experiments Specify widget classes, names by their use. --- diff --git a/client.py b/client.py index 743d2a9..5e91828 100755 --- a/client.py +++ b/client.py @@ -54,7 +54,7 @@ class UrwidSetup: - self.reply_widget, a urwid.Text widget printing self.socket replies """ edit_widget = self.EditToSocketWidget(self.socket, 'SEND: ') - self.reply_widget = urwid.Text('') + self.reply_widget = self.LogWidget('') self.map_widget = self.MapWidget('', wrap='clip') map_box = urwid.Padding(self.map_widget, width=50) widget_pile = urwid.Pile([edit_widget, map_box, self.reply_widget]) @@ -74,6 +74,13 @@ class UrwidSetup: plom_socket_io.send(self.socket, self.edit_text) self.edit_text = '' + class LogWidget(urwid.Text): + """Display client log, newest message on top.""" + + def add(self, text): + """Add text to (top of) log.""" + self.set_text(text + '\n' + self.text) + class MapWidget(urwid.Text): """Stores/updates/draws game map.""" map_size = (5, 5) @@ -152,9 +159,9 @@ class UrwidSetup: urwid-external thread. """ - def __init__(self, widget1, widget2, message_container): - self.widget1 = widget1 - self.widget2 = widget2 + def __init__(self, log_widget, map_widget, message_container): + self.log_widget = log_widget + self.map_widget = map_widget self.message_container = message_container def handle_input(self, trigger): @@ -174,7 +181,7 @@ class UrwidSetup: def mapdraw_command(prefix, func): n = len(prefix) if len(msg) > n and msg[:n] == prefix: - m = getattr(self.widget2, func) + m = getattr(self.map_widget, func) m(msg[n:]) return True return False @@ -190,11 +197,10 @@ class UrwidSetup: mapdraw_command('POSITION ', 'update_position') or mapdraw_command('MAP_SIZE ', 'update_map_size')) except ArgumentError as e: - self.widget1.set_text('ARGUMENT ERROR: ' + msg + '\n' + - str(e)) + self.log_widget.add('ARGUMENT ERROR: ' + msg + '\n' + str(e)) else: if not found_command: - self.widget1.set_text('UNHANDLED INPUT: ' + msg) + self.log_widget.add('UNHANDLED INPUT: ' + msg) del self.message_container[0] def recv_loop(self):