diff --git a/rtv/content.py b/rtv/content.py index 832ac4c..824f2b5 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -68,6 +68,12 @@ class BaseContent(object): def iterate(self, index, step, n_cols): while True: + + # Hack to prevent displaying negative indicies if iterating in the + # negative direction. + if step < 0 and index < 0: + break + try: yield self.get(index, n_cols=n_cols) except IndexError: diff --git a/rtv/page.py b/rtv/page.py index 9f776bc..dac19fe 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -52,18 +52,15 @@ class Navigator(object): valid = False else: self.cursor_index += 1 - if self.cursor_index >= n_windows - 1: - # We have reached the end of the page - if self._is_valid(self.absolute_index): - # Flip the orientation - self.page_index += (self.step * self.cursor_index) - self.cursor_index = 0 - self.inverted = not self.inverted - redraw = True - else: - # Unless we are at the absolute end of the submission - self.cursor_index -= 1 # Revert - valid = False + if not self._is_valid(self.absolute_index): + # Move would take us out of bounds + self.cursor_index -= 1 + valid = False + elif self.cursor_index >= (n_windows - 1): + # Flip the orientation and reset the cursor + self.flip(self.cursor_index) + self.cursor_index = 0 + redraw = True else: if self.cursor_index > 0: self.cursor_index -= 1 @@ -78,12 +75,17 @@ class Navigator(object): return valid, redraw + def flip(self, n_windows): + "Flip the orientation of the page" + self.page_index += (self.step * n_windows) + self.cursor_index = n_windows + self.inverted = not self.inverted + def _is_valid(self, page_index): "Check if a page index will cause entries to fall outside valid range" try: self._page_cb(page_index) - self._page_cb(page_index + self.step * self.cursor_index) except IndexError: return False else: @@ -185,6 +187,15 @@ class BasePage(object): current_row += step * (window_rows + 1) if available_rows <= 0: break + else: + # If the page is not full we need to make sure that it is NOT + # inverted. Unfortunately, this currently means drawing the whole + # page over again. Could not think of a better way to pre-determine + # if the content will fill up the page, given that it is dependent + # on the size of the terminal. + if self.nav.inverted: + self.nav.flip((len(self._subwindows) - 1)) + self._draw_content() self._content_window.refresh() @@ -196,7 +207,7 @@ class BasePage(object): if not valid: curses.flash() - # If we don't redraw, ACS_VLINE gets screwed up when changing the + # TODO: If we don't redraw, ACS_VLINE gets screwed up when changing the # attr back to normal. There may be a way around this. if True: #if redraw self._draw_content() diff --git a/rtv/submission.py b/rtv/submission.py index 410a7e1..1233e28 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -39,10 +39,12 @@ class SubmissionPage(BasePage): # Refresh page elif cmd in (curses.KEY_F5, ord('r')): self.refresh_content() + self.draw() # Show / hide a comment tree elif cmd in (curses.KEY_RIGHT, ord(' ')): self.toggle_comment() + self.draw() elif cmd == curses.KEY_RESIZE: self.draw() @@ -61,14 +63,12 @@ class SubmissionPage(BasePage): def toggle_comment(self): self.content.toggle(self.nav.absolute_index) - self.draw() def refresh_content(self): self.content.reset() self.nav.page_index, self.nav.cursor_index = -1, 0 self.nav.inverted = False - self.draw() def draw_item(self, win, data, inverted=False): diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 2aca4b8..0627d8b 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -36,10 +36,12 @@ class SubredditPage(BasePage): # View submission elif cmd in (curses.KEY_RIGHT, curses.KEY_ENTER, ord(' '), 10): self.open_submission() + self.draw() # Enter edit mode to change subreddit elif cmd == ord('/'): self.prompt_subreddit() + self.draw() # Refresh page elif cmd in (curses.KEY_F5, ord('r')): @@ -72,8 +74,6 @@ class SubredditPage(BasePage): self.nav.inverted = False self.name = name - self.draw() - def prompt_subreddit(self): attr = curses.A_BOLD | Color.MAGENTA @@ -85,9 +85,7 @@ class SubredditPage(BasePage): window.attrset(attr) out = text_input(window) - if out is None: - self.draw() - else: + if out is not None: self.refresh_content(name=out) def open_submission(self):