+#!/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 <https://stackoverflow.com/q/7063128>
+ 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