From 2c3b00b79a77ebb6ed8ddcb776389730b52297cb Mon Sep 17 00:00:00 2001 From: woorst Date: Wed, 19 Jul 2017 10:13:17 -0500 Subject: [PATCH] jump directly to parent/sibling comments in submission page --- CONTROLS.rst | 2 ++ rtv/docs.py | 2 ++ rtv/submission_page.py | 28 ++++++++++++++++++++++++++++ rtv/templates/rtv.cfg | 2 ++ tests/test_submission.py | 20 ++++++++++++++++++-- 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CONTROLS.rst b/CONTROLS.rst index e8ad399..34f3139 100644 --- a/CONTROLS.rst +++ b/CONTROLS.rst @@ -73,3 +73,5 @@ In submission mode you can view the self text for a submission and browse commen :``o`` or ``ENTER``: Open the comment permalink with your web browser :``SPACE``: Fold the selected comment, or load additional comments :``b``: Display URLs with urlview +:``J``: Move cursor to next sibling comment +:``K``: Move cursor to parent comment diff --git a/rtv/docs.py b/rtv/docs.py index e07f882..a529abf 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -30,6 +30,8 @@ https://github.com/michael-lazar/rtv m : Move up one page gg : Jump to the first post G : Jump to the last post + J : Jump to next sibling comment + K : Jump to parent comment 1 : Sort by hot 2 : Sort by top 3 : Sort by rising diff --git a/rtv/submission_page.py b/rtv/submission_page.py index 68c8b36..1095bd4 100644 --- a/rtv/submission_page.py +++ b/rtv/submission_page.py @@ -181,6 +181,34 @@ class SubmissionPage(Page): else: self.term.flash() + @PageController.register(Command('SUBMISSION_GOTO_PARENT')) + def move_parent_up(self): + cur = self.nav.absolute_index + if cur > 0: + child_level = self.content.get(cur)['level'] + while self.content.get(cur-1)['level'] >= 1 \ + and self.content.get(cur-1)['level'] >= child_level: + self._move_cursor(-1) + cur -= 1 + self._move_cursor(-1) + self.clear_input_queue() + + @PageController.register(Command('SUBMISSION_GOTO_SIBLING')) + def move_sibling_next(self): + cur = self.nav.absolute_index + if cur in range(self.content.range[1]): + sibling_level = self.content.get(cur)['level'] + try: + move = 1 + while self.content.get(cur + move)['level'] > sibling_level: + move += 1 + except IndexError: + pass + else: + if self.content.get(cur + move)['level'] == sibling_level: + [self._move_cursor(1) for _ in range(move)] + self.clear_input_queue() + def _draw_item(self, win, data, inverted): if data['type'] == 'MoreComments': diff --git a/rtv/templates/rtv.cfg b/rtv/templates/rtv.cfg index 36fcc7e..0cf32ee 100644 --- a/rtv/templates/rtv.cfg +++ b/rtv/templates/rtv.cfg @@ -128,6 +128,8 @@ SUBMISSION_POST = c SUBMISSION_EXIT = h, SUBMISSION_OPEN_IN_PAGER = l, SUBMISSION_OPEN_IN_URLVIEWER = b +SUBMISSION_GOTO_PARENT = K +SUBMISSION_GOTO_SIBLING = J ; Subreddit page SUBREDDIT_SEARCH = f diff --git a/tests/test_submission.py b/tests/test_submission.py index ad69377..db83ef0 100644 --- a/tests/test_submission.py +++ b/tests/test_submission.py @@ -192,6 +192,22 @@ def test_submission_move_top_bottom(submission_page): assert submission_page.nav.absolute_index == -1 +def test_submission_move_sibling_parent(submission_page): + + # Jump to sibling + with mock.patch.object(submission_page, 'clear_input_queue'): + submission_page.controller.trigger('j') + submission_page.controller.trigger('J') + assert submission_page.nav.absolute_index == 7 + + # Jump to parent + with mock.patch.object(submission_page, 'clear_input_queue'): + submission_page.controller.trigger('k') + submission_page.controller.trigger('k') + submission_page.controller.trigger('K') + assert submission_page.nav.absolute_index == 0 + + def test_submission_pager(submission_page, terminal): # View a submission with the pager @@ -314,7 +330,7 @@ def test_submission_comment_save(submission_page, terminal, refresh_token): with mock.patch.object(submission_page, 'clear_input_queue'): submission_page.controller.trigger('j') - # Test save on the coment submission + # Test save on the comment submission with mock.patch('rtv.packages.praw.objects.Comment.save') as save, \ mock.patch('rtv.packages.praw.objects.Comment.unsave') as unsave: @@ -533,4 +549,4 @@ def test_copy_to_clipboard_linux(submission_page, terminal, refresh_token): else: # Nither xclip or xsel installed, this is what happens on Travis CI text = b'Failed to copy url: External copy application not found' - window.addstr.assert_called_with(1, 1, text) \ No newline at end of file + window.addstr.assert_called_with(1, 1, text)