From b8a5d28aa74610089f64d863189a483a553c67b1 Mon Sep 17 00:00:00 2001 From: codesoap Date: Sat, 25 Aug 2018 19:12:13 +0200 Subject: [PATCH] Enable link selection, with more than 8 links --- rtv/terminal.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/rtv/terminal.py b/rtv/terminal.py index 1f714a4..6c4cca8 100644 --- a/rtv/terminal.py +++ b/rtv/terminal.py @@ -355,18 +355,45 @@ class Terminal(object): return ch def prompt_user_and_open_selected_link(self, links): + 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 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(links[:9]): + 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']) - try: - choice = int(chr(self.show_notification(text))) - except ValueError: - return - if choice <= len(links): - self.open_link(links[choice - 1]['href']) + i + 1, capped_link_text, link['href']) + return text def open_link(self, url): """