X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2;a=blobdiff_plain;f=plomrogue_client%2Ftui.py;fp=plomrogue_client%2Ftui.py;h=ec04304b0b0358b1083b62b90038ab5096dab8a3;hp=0000000000000000000000000000000000000000;hb=d1b7ae346e13f77a72926173a3092d1e83663a9d;hpb=38a55f96dcd90f0f434b2fbcdc8a3dc943d737ed diff --git a/plomrogue_client/tui.py b/plomrogue_client/tui.py new file mode 100644 index 0000000..ec04304 --- /dev/null +++ b/plomrogue_client/tui.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +import curses + + + +class CursesScreen: + + def wrap_loop(self, loop): + curses.wrapper(self.start_loop, loop) + + def safe_addstr(self, y, x, line, attr=0): + if y < self.size.y - 1 or x + len(line) < self.size.x: + self.stdscr.addstr(y, x, line, attr) + else: # workaround to + cut_i = self.size.x - x - 1 + cut = line[:cut_i] + last_char = line[cut_i] + self.stdscr.addstr(y, self.size.x - 2, last_char, attr) + self.stdscr.insstr(y, self.size.x - 2, ' ') + self.stdscr.addstr(y, x, cut, attr) + + def reset_size(self): + from plomrogue.mapping import YX + self.size = YX(*self.stdscr.getmaxyx()) + self.size = self.size - YX(self.size.y % 4, 0) + self.size = self.size - YX(0, self.size.x % 4) + + def start_loop(self, stdscr, loop): + self.stdscr = stdscr + curses.curs_set(0) # hide cursor + stdscr.timeout(10) + loop() + + + +def msg_into_lines_of_width(msg, width): + chunk = '' + lines = [] + x = 0 + for i in range(len(msg)): + if x >= width or msg[i] == "\n": + lines += [chunk] + chunk = '' + x = 0 + if msg[i] == "\n": + x -= 1 + if msg[i] != "\n": + chunk += msg[i] + x += 1 + lines += [chunk] + return lines