def create_win(self): if self.win: self.win.clear() h, w = self.stdscr.getmaxyx() if self.want_height < 0: self.make_height = h - self.y + self.want_height else: self.make_height = self.want_height if self.want_width < 0: self.make_width = w - self.x + self.want_width else: self.make_width = self.want_width self.win = self.stdscr.derwin(self.make_height, self.make_width, self.y, self.x) self.formatter = VirtualTextFormatter(self.make_width - 1)
class ScrollingWindow: SIZE = 32000 def __init__(self, stdscr, y, x, height, width): self.stdscr = stdscr self.y = y self.x = x self.want_height = height self.want_width = width self._scrollpos = -1 self.win = None self.create_win() self.evhistory = [] self.formatter = None self.create_win() def _get_lastline(self): return len(self.formatter.format) lastline = property(_get_lastline) def create_win(self): if self.win: self.win.clear() h, w = self.stdscr.getmaxyx() if self.want_height < 0: self.make_height = h - self.y + self.want_height else: self.make_height = self.want_height if self.want_width < 0: self.make_width = w - self.x + self.want_width else: self.make_width = self.want_width self.win = self.stdscr.derwin(self.make_height, self.make_width, self.y, self.x) self.formatter = VirtualTextFormatter(self.make_width - 1) def newline(self, indent=0): self.evhistory.append((None, indent)) self.formatter.newline(indent) def write(self, text, opts=None): self.evhistory.append((text, opts)) self.formatter.add(text, opts) if self._scrollpos == -1: self.draw() def backfill(self): for obj, opts in self.evhistory: if obj is None: self.formatter.newline(opts) else: self.formatter.add(obj, opts) def check_resize(self): # Screen has been resized! if self.want_height < 0 or self.want_width < 0: self.create_win() self.backfill() self.draw() def page_up(self): scrollpos = self._scrollpos if scrollpos == -1: scrollpos = self.lastline - self.make_height if scrollpos != 0: self._scrollpos = max(scrollpos - (self.make_height / 2), 0) self.draw() def page_down(self): if self._scrollpos != -1: self._scrollpos = self._scrollpos + (self.make_height / 2) if self._scrollpos >= self.lastline - self.make_height: self._scrollpos = -1 self.draw() def draw(self): self.win.clear() scrollpos = self._scrollpos if scrollpos == -1: scrollpos = max(self.lastline - self.make_height, 0) for y_pos, x in enumerate(xrange(scrollpos, scrollpos + self.make_height)): try: line = self.formatter.format[x] except IndexError: break x_pos = 0 for s, opts in line: if opts: self.win.addstr(y_pos, x_pos, s, opts) else: self.win.addstr(y_pos, x_pos, s) x_pos += len(s) self.win.refresh()