summaryrefslogtreecommitdiff
path: root/asciifarm/client/display/messagepad.py
blob: d5de5a9408d4dbc4f256eb6defd23d5d97c9da0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

import curses
import textwrap

class MessagePad():
    
    def __init__(self):
        self.changed = False
        self.messages = []
        self.win = None
    
    def setWin(self, win):
        self.win = win
    
    def addMessage(self, message):
        self.messages.append(message)
        self.changed = True
    
    def update(self, force):
        if not self.changed and not force or not self.win:
            return
        win = self.win
        height, width = win.getmaxyx()
        if height < 1:
            return
        lines = []
        for message in self.messages:
            lines += textwrap.wrap(message, width)
        if len(lines) > height:
            lines = lines[len(lines)-height:]
        win.erase()
        win.addstr(0,0,'\n'.join(lines))
        self.changed = False
        win.noutrefresh()