Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Merge branch 'woorst-yank_links_prompt'
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-lazar committed Feb 3, 2019
2 parents 7b06344 + c019a62 commit db61dfc
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 709 deletions.
2 changes: 1 addition & 1 deletion rtv/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
SPACE : Hide a submission, or fold/expand the selected comment tree
b : Display urls with urlview
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
F3 : Cycle to next theme
Expand Down
41 changes: 36 additions & 5 deletions rtv/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,39 @@ def get_inbox(self):
message = 'New Messages' if inbox > 0 else 'No New Messages'
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')
permalink = data.get('permalink')

if url_full and url_full != permalink:
# The item is a link-only submission that won't contain text
link = url_full
else:
html = data.get('html')
if html:
extracted_links = self.content.extract_links(html)
if not extracted_links:
# Only one selection to choose from, so just pick it
link = permalink
else:
# Let the user decide which link to open
links = []
if permalink:
links += [{'text': 'Permalink', 'href': permalink}]
links += extracted_links
link = self.term.prompt_user_to_select_link(links)
else:
# Some items like hidden comments don't have any HTML to parse
link = permalink

return link

@PageController.register(Command('COPY_PERMALINK'))
def copy_permalink(self):
"""
Expand All @@ -329,11 +362,9 @@ def copy_permalink(self):
@PageController.register(Command('COPY_URL'))
def copy_url(self):
"""
Copies submission url to OS clipboard
Copies link to OS clipboard
"""

data = self.get_selected_item()
url = data.get('url_full')
url = self.prompt_and_select_link()
if url is None:
self.term.flash()
return
Expand All @@ -346,7 +377,7 @@ def copy_url(self):
'Failed to copy url: {0}'.format(e))
else:
self.term.show_notification(
'Copied url to clipboard', timeout=1)
['Copied to clipboard:', url], timeout=1)

def clear_input_queue(self):
"""
Expand Down
33 changes: 8 additions & 25 deletions rtv/submission_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,17 @@ def open_link(self):
"""
data = self.get_selected_item()
if data['type'] == 'Submission':
opened_link = self.prompt_and_open_link(data)
if opened_link is not None:
self.config.history.add(opened_link)
elif data['type'] == 'Comment' and data['permalink']:
self.prompt_and_open_link(data)
link = self.prompt_and_select_link()
if link:
self.config.history.add(link)
self.term.open_link(link)
elif data['type'] == 'Comment':
link = self.prompt_and_select_link()
if link:
self.term.open_link(link)
else:
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:
extracted_links = self.content.extract_links(data['html'])
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'))
def open_pager(self):
"""
Expand Down
Loading

0 comments on commit db61dfc

Please sign in to comment.