From 870379b6d96286a9e91ae03fc37b5fecac99a7c3 Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Sun, 28 Apr 2019 21:05:42 +0200 Subject: [PATCH] Add ability to move selected item on top (fixes #683) --- rtv/docs.py | 1 + rtv/objects.py | 17 +++++++++++++++++ rtv/page.py | 7 +++++++ rtv/templates/rtv.cfg | 1 + tests/test_objects.py | 30 ++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+) diff --git a/rtv/docs.py b/rtv/docs.py index b935f158..74e3a74e 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -36,6 +36,7 @@ h : Return to the previous view m : Move the cursor up one page n : Move the cursor down one page + - : Move the currently selected item to the top gg : Jump to the top of the page G : Jump to the bottom of the page 1-7 : Sort submissions by category diff --git a/rtv/objects.py b/rtv/objects.py index 8c568a8b..f9249299 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -374,6 +374,23 @@ def absolute_index(self): return self.page_index + (self.step * self.cursor_index) + def focus_up(self): + """ + Move the currently selected item to the top of the page. + + Returns: + redraw (bool): Indicates whether or not the screen needs to be + redrawn. + """ + redraw = True + if self.cursor_index > 0 or self.inverted: + self.page_index += self.cursor_index + self.cursor_index = 0 + self.inverted = False + else: + redraw = False + return redraw + def move(self, direction, n_windows): """ Move the cursor up or down by the given increment. diff --git a/rtv/page.py b/rtv/page.py index bf21377c..6acac9d7 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -240,6 +240,13 @@ def move_page_bottom(self): self.nav.cursor_index = 0 self.nav.inverted = True + @PageController.register(Command('FOCUS_UP')) + def focus_up(self): + """ + Move the currently selected item to the top of the page. + """ + self.nav.focus_up() + @PageController.register(Command('UPVOTE')) @logged_in def upvote(self): diff --git a/rtv/templates/rtv.cfg b/rtv/templates/rtv.cfg index 3cca8a2e..efd09d78 100644 --- a/rtv/templates/rtv.cfg +++ b/rtv/templates/rtv.cfg @@ -133,6 +133,7 @@ PREVIOUS_THEME = NEXT_THEME = PAGE_UP = m, , PAGE_DOWN = n, , +FOCUS_UP = - PAGE_TOP = gg PAGE_BOTTOM = G UPVOTE = a diff --git a/tests/test_objects.py b/tests/test_objects.py index dde5c3f9..27a2e452 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -590,6 +590,36 @@ def valid_page_cb(index): assert valid assert redraw +def test_objects_navigator_focus_up(): + + def valid_page_cb(index): + if index < 0 or index > 9: + raise IndexError() + + nav = Navigator(valid_page_cb, cursor_index=3) + + # Move the currently selected item to the top + redraw = nav.focus_up() + assert nav.page_index == 3 + assert nav.cursor_index == 0 + assert not nav.inverted + assert redraw + + # Move up and move the currently selected item to the top + nav.move(-1, 2) + redraw = nav.focus_up() + assert nav.page_index == 2 + assert nav.cursor_index == 0 + assert not nav.inverted + assert not redraw + + # Move down and move the currently selected item to the top + nav.move(1, 3) + redraw = nav.focus_up() + assert nav.page_index == 3 + assert nav.cursor_index == 0 + assert not nav.inverted + assert redraw def test_objects_navigator_flip():