diff options
| author | troido <troido@hotmail.com> | 2018-01-03 17:07:15 +0100 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2018-01-03 17:07:15 +0100 |
| commit | 99d7920cb3daf3fc5cb6f824eb4a639542d47974 (patch) | |
| tree | 62b2cbafbe2caf462fab7b7f4766c1d5eb63a8d8 /asciifarm/client/display/window.py | |
| parent | 03d45d60b84c07c29a304301fb290aecee9cfa24 (diff) | |
added intermediary window wrapper for safety. also server now appends logs
Diffstat (limited to 'asciifarm/client/display/window.py')
| -rw-r--r-- | asciifarm/client/display/window.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/asciifarm/client/display/window.py b/asciifarm/client/display/window.py new file mode 100644 index 0000000..a8e0696 --- /dev/null +++ b/asciifarm/client/display/window.py @@ -0,0 +1,71 @@ + +import curses + +class Window: + """ Small wrapper around curses windows """ + + def __init__(self, win, colours=None): + + self.setWin(win) + self.colours = colours + + + + def setWin(self, win): + self.win = win + + def getSize(self): + if not self.win: + return (0, 0) + height, width = self.win.getmaxyx() + return (width, height) + + def getPos(self): + if not self.win: + return (0, 0) + y, x = self.win.getparyx() + return (x, y) + + def addLine(self, pos, string, colour=(0,0)): + """Draw a string that does not contain newlines or characters with larger width + + long lines are cropped to fit in the window""" + x, y = pos + width, height = self.getSize() + string = string[:width-x] + drawLast = None + if self.colours: + self._addstr(y, x, string, self.colours.get(*colour)) + else: + self._addstr(y, x, string) + + + def _addstr(self, y, x, string, *args): + width, height = self.getSize() + if y == height-1 and x+len(string) == width: + if len(string) > 1: + self.win.addstr(y, x, string[:-1], *args) + try: + self.win.addstr(height-1, width-1, string[-1], *args) + except curses.error: + # ncurses has a weird problem: + # it always raises an error when drawing to the last character in the window + # it draws first and then raises the error + # therefore to draw in the last place of the window the last character needs to be ingored + # other solutions might be possible, but are more hacky + pass + else: + self.win.addstr(y, x, string, *args) + + def erase(self): + self.win.erase() + + def noutrefresh(self): + self.win.noutrefresh() + + def getStr(self, pos): + x, y = pos + return self.win.getstr(y, x) + + + |
