Merge branch 'yank_links_prompt' of https://github.com/woorst/rtv into woorst-yank_links_prompt

This commit is contained in:
Michael Lazar
2019-02-02 22:38:25 -05:00
4 changed files with 38 additions and 34 deletions

View File

@@ -149,7 +149,7 @@ class Content(object):
data['type'] = 'Comment' data['type'] = 'Comment'
data['level'] = comment.nested_level data['level'] = comment.nested_level
data['body'] = comment.body data['body'] = comment.body
data['html'] = comment.body_html data['html'] = comment.body_html or ''
data['created'] = cls.humanize_timestamp(comment.created_utc) data['created'] = cls.humanize_timestamp(comment.created_utc)
data['score'] = '{0} pts'.format( data['score'] = '{0} pts'.format(
'-' if comment.score_hidden else comment.score) '-' if comment.score_hidden else comment.score)

View File

@@ -67,7 +67,7 @@ https://github.com/michael-lazar/rtv
SPACE : Hide a submission, or fold/expand the selected comment tree SPACE : Hide a submission, or fold/expand the selected comment tree
b : Display urls with urlview b : Display urls with urlview
y : Copy submission permalink to clipboard y : Copy submission permalink to clipboard
Y : Copy submission link to clipboard Y : Copy submission or comment urls to clipboard
F2 : Cycle to previous theme F2 : Cycle to previous theme
F3 : Cycle to next theme F3 : Cycle to next theme

View File

@@ -304,6 +304,29 @@ class Page(object):
message = 'New Messages' if inbox > 0 else 'No New Messages' message = 'New Messages' if inbox > 0 else 'No New Messages'
self.term.show_notification(message) self.term.show_notification(message)
def prompt_and_select_link(self):
"""
Prompt the user to select a link from a list to open.
Return the link that was selected, or ``None`` if no link was selected.
"""
data = self.get_selected_item()
url_full = data.get('url_full')
if url_full and url_full != data['permalink']:
# The item is a link-only submission that won't contain text
link = data['url_full']
else:
extracted_links = self.content.extract_links(data.get('html', ''))
if not extracted_links:
# Only one selection to choose from, so just pick it
link = data.get('permalink')
else:
# Let the user decide which link to open
links = [{'text': 'Permalink', 'href': data['permalink']}]
links += extracted_links
link = self.term.prompt_user_to_select_link(links)
return link
@PageController.register(Command('COPY_PERMALINK')) @PageController.register(Command('COPY_PERMALINK'))
def copy_permalink(self): def copy_permalink(self):
""" """
@@ -329,11 +352,9 @@ class Page(object):
@PageController.register(Command('COPY_URL')) @PageController.register(Command('COPY_URL'))
def copy_url(self): def copy_url(self):
""" """
Copies submission url to OS clipboard Copies link to OS clipboard
""" """
url = self.prompt_and_select_link()
data = self.get_selected_item()
url = data.get('url_full')
if url is None: if url is None:
self.term.flash() self.term.flash()
return return
@@ -346,7 +367,7 @@ class Page(object):
'Failed to copy url: {0}'.format(e)) 'Failed to copy url: {0}'.format(e))
else: else:
self.term.show_notification( self.term.show_notification(
'Copied url to clipboard', timeout=1) ['Copied to clipboard:', url], timeout=1)
def clear_input_queue(self): def clear_input_queue(self):
""" """

View File

@@ -143,35 +143,18 @@ class SubmissionPage(Page):
If there is more than one link contained in the item, prompt the user If there is more than one link contained in the item, prompt the user
to choose which link to open. to choose which link to open.
""" """
link = self.prompt_and_select_link()
if link:
data = self.get_selected_item() data = self.get_selected_item()
if data['type'] == 'Submission': if data['type'] == 'Submission':
opened_link = self.prompt_and_open_link(data) self.config.history.add(link)
if opened_link is not None: self.term.open_link(link)
self.config.history.add(opened_link)
elif data['type'] == 'Comment' and data['permalink']: elif data['type'] == 'Comment' and data['permalink']:
self.prompt_and_open_link(data) self.term.open_link(link)
else: else:
self.term.flash() self.term.flash()
def prompt_and_open_link(self, data):
url_full = data.get('url_full')
if url_full and url_full != data['permalink']:
# The item is a link-only submission that won't contain text
link = data['url_full']
else: else:
extracted_links = self.content.extract_links(data['html']) self.term.flash()
if not extracted_links:
# Only one selection to choose from, so just pick it
link = data['permalink']
else:
# Let the user decide which link to open
links = [{'text': 'Permalink', 'href': data['permalink']}]
links += extracted_links
link = self.term.prompt_user_to_select_link(links)
if link is not None:
self.term.open_link(link)
return link
@SubmissionController.register(Command('SUBMISSION_OPEN_IN_PAGER')) @SubmissionController.register(Command('SUBMISSION_OPEN_IN_PAGER'))
def open_pager(self): def open_pager(self):