From 52dfbe786c70d15c30b2561ea63d2c4190b16b5c Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 8 Dec 2015 18:22:13 -0800 Subject: [PATCH] Removed banner for subscriptions and cleaned up a bunch of unnecessary screen refreshes. --- rtv/objects.py | 1 - rtv/page.py | 93 ++++++++++++++++++++------------------------- rtv/subscription.py | 4 ++ rtv/terminal.py | 1 - 4 files changed, 46 insertions(+), 53 deletions(-) diff --git a/rtv/objects.py b/rtv/objects.py index ebd0c3c..34af0f8 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -180,7 +180,6 @@ class LoadScreen(object): self._is_running = False self._animator.join() - self._terminal.stdscr.refresh() if e is None or not self.catch_exception: # Skip exception handling diff --git a/rtv/page.py b/rtv/page.py index 3a0884e..b3c6162 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -42,9 +42,7 @@ class Page(object): self.controller = None self.active = True - self._header_window = None - self._banner_window = None - self._content_window = None + self._row = 0 self._subwindows = None def refresh_content(self, order=None, name=None): @@ -252,37 +250,32 @@ class Page(object): def draw(self): - window = self.term.stdscr - n_rows, n_cols = window.getmaxyx() + n_rows, n_cols = self.term.stdscr.getmaxyx() if n_rows < self.term.MIN_HEIGHT or n_cols < self.term.MIN_WIDTH: # TODO: Will crash when you try to navigate if the terminal is too # small at startup because self._subwindows will never be populated return - # Note: 2 argument form of derwin breaks PDcurses on Windows 7! - self._header_window = window.derwin(1, n_cols, 0, 0) - self._banner_window = window.derwin(2, n_cols, 1, 0) - self._content_window = window.derwin(n_rows - 3, n_cols, 3, 0) - - window.erase() + self._row = 0 self._draw_header() self._draw_banner() self._draw_content() self._add_cursor() + self.term.stdscr.touchwin() + self.term.stdscr.refresh() def _draw_header(self): - n_rows, n_cols = self._header_window.getmaxyx() - self._header_window.erase() + n_rows, n_cols = self.term.stdscr.getmaxyx() + # Note: 2 argument form of derwin breaks PDcurses on Windows 7! + window = self.term.stdscr.derwin(1, n_cols, self._row, 0) + window.erase() # curses.bkgd expects bytes in py2 and unicode in py3 ch, attr = str(' '), curses.A_REVERSE | curses.A_BOLD | Color.CYAN - self._header_window.bkgd(ch, attr) + window.bkgd(ch, attr) sub_name = self.content.name.replace('/r/front', 'Front Page') - self.term.add_line(self._header_window, sub_name, 0, 0) - if self.content.order is not None: - order = ' [{}]'.format(self.content.order) - self.term.add_line(self._header_window, order) + self.term.add_line(window, sub_name, 0, 0) if self.reddit.user is not None: # The starting position of the name depends on if we're converting @@ -293,53 +286,59 @@ class Page(object): s_col = (n_cols - width(username) - 1) # Only print username if it fits in the empty space on the right if (s_col - 1) >= width(sub_name): - self.term.add_line(self._header_window, username, 0, s_col) + self.term.add_line(window, username, 0, s_col) - self._header_window.refresh() + self._row += 1 def _draw_banner(self): - n_rows, n_cols = self._header_window.getmaxyx() - self._banner_window.erase() - ch, attr = str(' '), curses.A_BOLD - self._banner_window.bkgd(ch, attr) + n_rows, n_cols = self.term.stdscr.getmaxyx() + window = self.term.stdscr.derwin(1, n_cols, self._row, 0) + window.erase() + ch, attr = str(' '), curses.A_BOLD | Color.YELLOW + window.bkgd(ch, attr) items = ['[1]hot', '[2]top', '[3]rising', '[4]new', '[5]controversial'] distance = (n_cols - sum(len(t) for t in items) - 1) / (len(items) - 1) spacing = max(1, int(distance)) * ' ' text = spacing.join(items) - self.term.add_line(self._banner_window, text, 0, 0) + self.term.add_line(window, text, 0, 0) if self.content.order is not None: col = text.find(self.content.order) - 3 - self._banner_window.chgat(0, col, 3, attr | curses.A_REVERSE) + window.chgat(0, col, 3, attr | curses.A_REVERSE) + + self._row += 1 def _draw_content(self): """ Loop through submissions and fill up the content page. """ - n_rows, n_cols = self._content_window.getmaxyx() - self._content_window.erase() - self._subwindows = [] + n_rows, n_cols = self.term.stdscr.getmaxyx() + window = self.term.stdscr.derwin( + n_rows - self._row, n_cols, self._row, 0) + window.erase() + win_n_rows, win_n_cols = window.getmaxyx() + self._subwindows = [] page_index, cursor_index, inverted = self.nav.position step = self.nav.step # If not inverted, align the first submission with the top and draw # downwards. If inverted, align the first submission with the bottom # and draw upwards. - current_row = (n_rows - 1) if inverted else 0 - available_rows = (n_rows - 1) if inverted else n_rows - for data in self.content.iterate(page_index, step, n_cols - 2): - window_rows = min(available_rows, data['n_rows']) - window_cols = n_cols - data['offset'] - start = current_row - window_rows if inverted else current_row - subwindow = self._content_window.derwin( - window_rows, window_cols, start, data['offset']) + current_row = (win_n_rows - 1) if inverted else 0 + available_rows = (win_n_rows - 1) if inverted else win_n_rows + for data in self.content.iterate(page_index, step, win_n_cols - 2): + subwin_n_rows = min(available_rows, data['n_rows']) + subwin_n_cols = win_n_cols - data['offset'] + start = current_row - subwin_n_rows if inverted else current_row + subwindow = window.derwin( + subwin_n_rows, subwin_n_cols, start, data['offset']) attr = self._draw_item(subwindow, data, inverted) self._subwindows.append((subwindow, attr)) - available_rows -= (window_rows + 1) # Add one for the blank line - current_row += step * (window_rows + 1) + available_rows -= (subwin_n_rows + 1) # Add one for the blank line + current_row += step * (subwin_n_rows + 1) if available_rows <= 0: break else: @@ -352,7 +351,7 @@ class Page(object): self.nav.flip((len(self._subwindows) - 1)) self._draw_content() - self._content_window.refresh() + self._row = n_rows def _add_cursor(self): self._edit_cursor(curses.A_REVERSE) @@ -362,13 +361,11 @@ class Page(object): def _move_cursor(self, direction): self._remove_cursor() + # Note: ACS_VLINE doesn't like changing the attribute, so disregard the + # redraw flag and opt to always redraw valid, redraw = self.nav.move(direction, len(self._subwindows)) if not valid: self.term.flash() - - # Note: ACS_VLINE doesn't like changing the attribute, - # so always redraw. - self._draw_content() self._add_cursor() def _move_page(self, direction): @@ -376,10 +373,6 @@ class Page(object): valid, redraw = self.nav.move_page(direction, len(self._subwindows)-1) if not valid: self.term.flash() - - # Note: ACS_VLINE doesn't like changing the attribute, - # so always redraw. - self._draw_content() self._add_cursor() def _edit_cursor(self, attribute): @@ -400,6 +393,4 @@ class Page(object): n_rows, _ = window.getmaxyx() for row in range(n_rows): - window.chgat(row, 0, 1, attribute) - - window.refresh() \ No newline at end of file + window.chgat(row, 0, 1, attribute) \ No newline at end of file diff --git a/rtv/subscription.py b/rtv/subscription.py index d5af500..6341a4a 100644 --- a/rtv/subscription.py +++ b/rtv/subscription.py @@ -52,6 +52,10 @@ class SubscriptionPage(Page): self.active = False + def _draw_banner(self): + # Subscriptions can't be sorted, so disable showing the order menu + pass + def _draw_item(self, win, data, inverted): n_rows, n_cols = win.getmaxyx() n_cols -= 1 # Leave space for the cursor in the first column diff --git a/rtv/terminal.py b/rtv/terminal.py index 44eb69e..746ee6d 100644 --- a/rtv/terminal.py +++ b/rtv/terminal.py @@ -422,7 +422,6 @@ class Terminal(object): # Create a separate window for text input input_win = curses.newwin(1, n_cols-len(prompt), n_rows-1, len(prompt)) input_win.attrset(attr) - input_win.refresh() if key: curses.curs_set(1)