From d1f84633cff02d9d43c321814364b2c30c4fa3a9 Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 29 Oct 2017 23:31:46 +0100 Subject: merged recent changes into package branch again --- asciifarm/client/display/messagepad.py | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 asciifarm/client/display/messagepad.py (limited to 'asciifarm/client/display/messagepad.py') diff --git a/asciifarm/client/display/messagepad.py b/asciifarm/client/display/messagepad.py new file mode 100644 index 0000000..50d34f5 --- /dev/null +++ b/asciifarm/client/display/messagepad.py @@ -0,0 +1,42 @@ + +import curses +import textwrap + +class MessagePad: + + def __init__(self, maxLines=10): + self.maxLines = maxLines + self.pad = curses.newpad(maxLines+2, 200) + self.changed = False + self.lastView = None + self.messages = [] + + def addMessage(self, message): + self.messages.append(message) + self.changed = True + + def getHeight(self): + return min(len(self.messages), self.maxLines) + + def update(self, screen, x, y, xmax, ymax): + if not self.changed and (x, y, xmax, ymax) == self.lastView : + return + width = xmax - x + height = ymax - y # should equal self.getHeight() + if height < 1: + return + lines = [] + for message in self.messages: + lines += textwrap.wrap(message, width) + if len(lines) > height: + lines = lines[len(lines)-height:] + self.pad.erase() + self.pad.addstr(0,0,'\n'.join(lines)) + self.changed = False + self.pad.noutrefresh( + 0, + 0, + y, + x, + ymax-1, + xmax-1) -- cgit