Cleanup refactor.
This commit is contained in:
13
rtv/page.py
13
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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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']:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user