From 6a395216a31f126834b7d6d009d1927120b8e218 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Mon, 31 Oct 2016 13:50:41 -0700 Subject: [PATCH] Cleanup refactor. --- rtv/page.py | 13 ++++++++----- rtv/submission_page.py | 14 +++++++------- rtv/subreddit_page.py | 10 +++++----- rtv/subscription_page.py | 2 +- rtv/terminal.py | 24 ++++++------------------ 5 files changed, 27 insertions(+), 36 deletions(-) diff --git a/rtv/page.py b/rtv/page.py index 600b023..fbaf773 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -57,6 +57,9 @@ class Page(object): def _draw_item(self, window, data, inverted): raise NotImplementedError + def get_selected_item(self): + return self.content.get(self.nav.absolute_index) + def loop(self): """ Main control loop runs the following steps: @@ -186,7 +189,7 @@ class Page(object): @PageController.register(Command('UPVOTE')) @logged_in def upvote(self): - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if 'likes' not in data: self.term.flash() elif data['likes']: @@ -203,7 +206,7 @@ class Page(object): @PageController.register(Command('DOWNVOTE')) @logged_in def downvote(self): - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if 'likes' not in data: self.term.flash() elif data['likes'] or data['likes'] is None: @@ -220,7 +223,7 @@ class Page(object): @PageController.register(Command('SAVE')) @logged_in def save(self): - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if 'saved' not in data: self.term.flash() elif not data['saved']: @@ -255,7 +258,7 @@ class Page(object): Delete a submission or comment. """ - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if data.get('author') != self.reddit.user.name: self.term.flash() return @@ -279,7 +282,7 @@ class Page(object): Edit a submission or comment. """ - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if data.get('author') != self.reddit.user.name: self.term.flash() return diff --git a/rtv/submission_page.py b/rtv/submission_page.py index 784c25c..b64678c 100644 --- a/rtv/submission_page.py +++ b/rtv/submission_page.py @@ -93,7 +93,7 @@ class SubmissionPage(Page): def open_link(self): "Open the selected item with the webbrowser" - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() url = data.get('permalink') if url: self.term.open_browser(url) @@ -103,7 +103,7 @@ class SubmissionPage(Page): @SubmissionController.register(Command('SUBMISSION_OPEN_IN_PAGER')) def open_pager(self): "Open the selected item with the system's pager" - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if data['type'] == 'Submission': text = '\n\n'.join((data['permalink'], data['text'])) self.term.open_pager(text) @@ -124,7 +124,7 @@ class SubmissionPage(Page): Comment - add a comment reply """ - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if data['type'] == 'Submission': body = data['text'] reply = data['object'].add_comment @@ -171,7 +171,7 @@ class SubmissionPage(Page): @SubmissionController.register(Command('SUBMISSION_OPEN_IN_URLVIEWER')) def comment_urlview(self): - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() comment = data.get('body') or data.get('text') or data.get('url_full') if comment: self.term.open_urlview(comment) @@ -228,11 +228,11 @@ class SubmissionPage(Page): self.term.add_line(win, text, attr=attr) if data['stickied']: - text, attr = self.term.stickied + text, attr = '[stickied]', Color.GREEN self.term.add_line(win, text, attr=attr) if data['saved']: - text, attr = self.term.saved + text, attr = '[saved]', Color.GREEN self.term.add_line(win, text, attr=attr) for row, text in enumerate(split_body, start=offset+1): @@ -308,7 +308,7 @@ class SubmissionPage(Page): self.term.add_line(win, text, attr=attr) if data['saved']: - text, attr = self.term.saved + text, attr = '[saved]', Color.GREEN self.term.add_line(win, text, attr=attr) win.border() diff --git a/rtv/subreddit_page.py b/rtv/subreddit_page.py index 2684bed..b7f9b02 100644 --- a/rtv/subreddit_page.py +++ b/rtv/subreddit_page.py @@ -99,7 +99,7 @@ class SubredditPage(Page): data = {} if url is None: - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() url = data['permalink'] with self.term.loader('Loading submission'): @@ -121,7 +121,7 @@ class SubredditPage(Page): def open_link(self): "Open a link with the webbrowser" - data = self.content.get(self.nav.absolute_index) + data = self.get_selected_item() if data['url_type'] == 'selfpost': self.open_submission() elif data['url_type'] == 'x-post subreddit': @@ -244,16 +244,16 @@ class SubredditPage(Page): text, attr = self.term.get_arrow(data['likes']) self.term.add_line(win, text, attr=attr) self.term.add_line(win, ' {created} '.format(**data)) - text, attr = self.term.timestamp_sep + text, attr = '-', curses.A_BOLD self.term.add_line(win, text, attr=attr) self.term.add_line(win, ' {comments} '.format(**data)) if data['saved']: - text, attr = self.term.saved + text, attr = '[saved]', Color.GREEN self.term.add_line(win, text, attr=attr) if data['stickied']: - text, attr = self.term.stickied + text, attr = '[stickied]', Color.GREEN self.term.add_line(win, text, attr=attr) if data['gold']: diff --git a/rtv/subscription_page.py b/rtv/subscription_page.py index 9db646f..024d8b5 100644 --- a/rtv/subscription_page.py +++ b/rtv/subscription_page.py @@ -59,7 +59,7 @@ class SubscriptionPage(Page): def select_subreddit(self): "Store the selected subreddit and return to the subreddit page" - name = self.content.get(self.nav.absolute_index)['name'] + name = self.get_selected_item()['name'] with self.term.loader('Loading page'): content = SubredditContent.from_name( self.reddit, name, self.term.loader) diff --git a/rtv/terminal.py b/rtv/terminal.py index 3dbce2f..1c09f0d 100644 --- a/rtv/terminal.py +++ b/rtv/terminal.py @@ -73,30 +73,12 @@ class Terminal(object): attr = curses.A_BOLD return symbol, attr - @property - def timestamp_sep(self): - symbol = '-' - attr = curses.A_BOLD - return symbol, attr - @property def guilded(self): symbol = '*' if self.config['ascii'] else '✪' attr = curses.A_BOLD | Color.YELLOW return symbol, attr - @property - def stickied(self): - text = '[stickied]' - attr = Color.GREEN - return text, attr - - @property - def saved(self): - text = '[saved]' - attr = Color.GREEN - return text, attr - @property def vline(self): return getattr(curses, 'ACS_VLINE', ord('|')) @@ -138,6 +120,9 @@ class Terminal(object): @staticmethod def flash(): + """ + Flash the screen to indicate that an action was invalid. + """ return curses.flash() @staticmethod @@ -154,6 +139,9 @@ class Terminal(object): window.addch(y, x, ch, attr) def getch(self): + """ + Wait for a keypress and return the corresponding character code (int). + """ return self.stdscr.getch() @staticmethod