Adding tests and some minor tweaks to structure.

This commit is contained in:
Michael Lazar
2018-10-01 00:45:09 -04:00
parent 67fb93b2ef
commit e4cced27eb
5 changed files with 158 additions and 49 deletions

View File

@@ -355,9 +355,16 @@ class Terminal(object):
return ch
def prompt_user_to_select_link(self, links):
"""
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.
"""
link_pages = self.get_link_pages(links)
for link_page in link_pages:
text = self.get_link_page_text(link_page)
for n, link_page in enumerate(link_pages, start=1):
text = 'Select a link to open (page {} of {}):\n\n'
text = text.format(n, len(link_pages))
text += self.get_link_page_text(link_page)
if link_page is not link_pages[-1]:
text += '[9] next page...'
@@ -371,7 +378,14 @@ class Terminal(object):
return None
return link_page[choice - 1]['href']
def get_link_pages(self, links):
@staticmethod
def get_link_pages(links):
"""
Given a list of links, separate them into pages that can be displayed
to the user and navigated using the 1-9 number keys. The last page
can contain up to 9 links, and all other pages can contain up to 8
links.
"""
link_pages = []
i = 0
while i < len(links):
@@ -385,13 +399,16 @@ class Terminal(object):
link_pages.append(link_page)
return link_pages
def get_link_page_text(self, link_page):
text = 'Open link:\n'
@staticmethod
def get_link_page_text(link_page):
"""
Construct the dialog box to display a list of links to the user.
"""
text = ''
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'])
text += '[{}] [{}]({})\n'.format(i + 1, capped_link_text, link['href'])
return text
def open_link(self, url):