Merge pull request #318 from michael-lazar/simplify_navigation_2
Simplify navigation 2
This commit is contained in:
@@ -340,15 +340,15 @@ class SubmissionContent(Content):
|
||||
data['split_title'] = self.wrap_text(data['title'], width=n_cols-2)
|
||||
data['split_text'] = self.wrap_text(data['text'], width=n_cols-2)
|
||||
data['n_rows'] = len(data['split_title'] + data['split_text']) + 5
|
||||
data['offset'] = 0
|
||||
data['h_offset'] = 0
|
||||
|
||||
else:
|
||||
data = self._comment_data[index]
|
||||
indent_level = min(data['level'], self.max_indent_level)
|
||||
data['offset'] = indent_level * self.indent_size
|
||||
data['h_offset'] = indent_level * self.indent_size
|
||||
|
||||
if data['type'] == 'Comment':
|
||||
width = min(n_cols - data['offset'], self._max_comment_cols)
|
||||
width = min(n_cols - data['h_offset'], self._max_comment_cols)
|
||||
data['split_body'] = self.wrap_text(data['body'], width=width)
|
||||
data['n_rows'] = len(data['split_body']) + 1
|
||||
else:
|
||||
@@ -626,7 +626,7 @@ class SubredditContent(Content):
|
||||
data['split_title'] = data['split_title'][:self.max_title_rows-1]
|
||||
data['split_title'].append('(Not enough space to display)')
|
||||
data['n_rows'] = len(data['split_title']) + 3
|
||||
data['offset'] = 0
|
||||
data['h_offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
@@ -700,6 +700,6 @@ class SubscriptionContent(Content):
|
||||
data = self._subscription_data[index]
|
||||
data['split_title'] = self.wrap_text(data['title'], width=n_cols)
|
||||
data['n_rows'] = len(data['split_title']) + 1
|
||||
data['offset'] = 0
|
||||
data['h_offset'] = 0
|
||||
|
||||
return data
|
||||
|
||||
17
rtv/page.py
17
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
|
||||
@@ -452,10 +455,10 @@ class Page(object):
|
||||
subwin_n_rows = min(subwin_n_rows, top_item_height)
|
||||
subwin_inverted = True
|
||||
top_item_height = None
|
||||
subwin_n_cols = win_n_cols - data['offset']
|
||||
subwin_n_cols = win_n_cols - data['h_offset']
|
||||
start = current_row - subwin_n_rows + 1 if inverted else current_row
|
||||
subwindow = window.derwin(
|
||||
subwin_n_rows, subwin_n_cols, start, data['offset'])
|
||||
subwin_n_rows, subwin_n_cols, start, data['h_offset'])
|
||||
attr = self._draw_item(subwindow, data, subwin_inverted)
|
||||
self._subwindows.append((subwindow, attr))
|
||||
available_rows -= (subwin_n_rows + 1) # Add one for the blank line
|
||||
|
||||
@@ -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
|
||||
@@ -162,16 +162,16 @@ class SubmissionPage(Page):
|
||||
@SubmissionController.register(Command('DELETE'))
|
||||
@logged_in
|
||||
def delete_comment(self):
|
||||
"Delete a comment as long as it is not the current submission"
|
||||
"Delete the selected comment"
|
||||
|
||||
if self.nav.absolute_index != -1:
|
||||
if self.get_selected_item()['type'] == 'Comment':
|
||||
self.delete_item()
|
||||
else:
|
||||
self.term.flash()
|
||||
|
||||
@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
|
||||
|
||||
@@ -178,7 +178,7 @@ def test_content_submission(reddit, terminal):
|
||||
assert content.get(40)['type'] == 'Comment'
|
||||
|
||||
for data in content.iterate(-1, 1):
|
||||
assert all(k in data for k in ('object', 'n_rows', 'offset', 'type',
|
||||
assert all(k in data for k in ('object', 'n_rows', 'h_offset', 'type',
|
||||
'hidden'))
|
||||
# All text should be converted to unicode by this point
|
||||
for val in data.values():
|
||||
@@ -278,7 +278,7 @@ def test_content_subreddit(reddit, terminal):
|
||||
|
||||
for data in content.iterate(0, 1):
|
||||
assert all(k in data for k in (
|
||||
'object', 'n_rows', 'offset', 'type', 'index', 'title',
|
||||
'object', 'n_rows', 'h_offset', 'type', 'index', 'title',
|
||||
'split_title', 'hidden'))
|
||||
# All text should be converted to unicode by this point
|
||||
for val in data.values():
|
||||
@@ -300,7 +300,7 @@ def test_content_subreddit_load_more(reddit, terminal):
|
||||
assert content.range == (0, 50)
|
||||
|
||||
for i, data in enumerate(islice(content.iterate(0, 1), 0, 50)):
|
||||
assert all(k in data for k in ('object', 'n_rows', 'offset', 'type',
|
||||
assert all(k in data for k in ('object', 'n_rows', 'h_offset', 'type',
|
||||
'index', 'title', 'split_title'))
|
||||
# All text should be converted to unicode by this point
|
||||
for val in data.values():
|
||||
@@ -424,7 +424,7 @@ def test_content_subscription(reddit, terminal):
|
||||
|
||||
# Validate content
|
||||
for data in islice(content.iterate(0, 1), 20):
|
||||
assert all(k in data for k in ('object', 'n_rows', 'offset', 'type',
|
||||
assert all(k in data for k in ('object', 'n_rows', 'h_offset', 'type',
|
||||
'title', 'split_title'))
|
||||
# All text should be converted to unicode by this point
|
||||
for val in data.values():
|
||||
|
||||
Reference in New Issue
Block a user