Enable link selection, with more than 8 links

This commit is contained in:
codesoap
2018-08-25 19:12:13 +02:00
parent cec9744702
commit b8a5d28aa7

View File

@@ -355,18 +355,45 @@ class Terminal(object):
return ch
def prompt_user_and_open_selected_link(self, links):
text = 'Open link:\n'
for i, link in enumerate(links[:9]):
capped_link_text = (link['text'] if len(link['text']) <= 20
else link['text'][:19] + '')
text += '[{}] [{}]({})\n'.format(
i+1, capped_link_text, link['href'])
link_pages = self.get_link_pages(links)
for link_page in link_pages:
text = self.get_link_page_text(link_page)
if link_page is not link_pages[-1]:
text += '[9] next page...'
try:
choice = int(chr(self.show_notification(text)))
except ValueError:
return
if choice <= len(links):
self.open_link(links[choice - 1]['href'])
if link_page is not link_pages[-1] and choice == 9:
continue
elif choice == 0 or choice > len(link_page):
return
self.open_link(link_page[choice - 1]['href'])
return
def get_link_pages(self, links):
link_pages = []
i = 0
while i < len(links):
link_page = []
while i < len(links) and len(link_page) < 8:
link_page.append(links[i])
i += 1
if i == len(links) - 1:
link_page.append(links[i])
i += 1
link_pages.append(link_page)
return link_pages
def get_link_page_text(self, link_page):
text = 'Open link:\n'
for i, link in enumerate(link_page):
capped_link_text = (link['text'] if len(link['text']) <= 20
else link['text'][:19] + '')
text += '[{}] [{}]({})\n'.format(
i + 1, capped_link_text, link['href'])
return text
def open_link(self, url):
"""