From b609d2f05c0b2bb1a42b9654f380d38ab4219df6 Mon Sep 17 00:00:00 2001 From: John Victor Date: Sun, 17 Nov 2019 21:16:03 -0300 Subject: [PATCH] Changed from using python-inquirer to PyInquirer (#12) - Closes #10 - Closes #7 - Closes #4 --- nyaacli/cli/__init__.py | 3 +++ nyaacli/colors.py | 45 ++++++++++++++++------------------------- nyaacli/nyaa_search.py | 43 ++++++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/nyaacli/cli/__init__.py b/nyaacli/cli/__init__.py index 2fbf82a..9e5b440 100644 --- a/nyaacli/cli/__init__.py +++ b/nyaacli/cli/__init__.py @@ -6,6 +6,9 @@ from nyaacli.nyaa_search import search_torrent from nyaacli.colors import red, green +from colorama import init + +init() @click.command() diff --git a/nyaacli/colors.py b/nyaacli/colors.py index e3adc2c..2b0406f 100644 --- a/nyaacli/colors.py +++ b/nyaacli/colors.py @@ -1,42 +1,31 @@ -from inquirer.themes import Theme -from blessings import Terminal -from colorama import init +from PyInquirer import Token, style_from_dict +from colorama import init, Fore, Style init() def red(text: str) -> str: - return f'\033[31m{text}\033[0m' + return Fore.RED + text + Style.RESET_ALL def green(text: str) -> str: - return f'\33[92m{text}\033[0m' + return Fore.GREEN + text + Style.RESET_ALL def yellow(text: str) -> str: - return f'\33[33m{text}\033[0m' + return Fore.YELLOW + text + Style.RESET_ALL def blue(text: str) -> str: - return f'\33[34m{text}\033[0m' - - -class PromptTheme(Theme): - def __init__(self): - super(PromptTheme, self).__init__() - - term = Terminal() - - self.Question.mark_color = term.yellow - self.Question.brackets_color = term.normal - self.Question.default_color = term.normal - self.Editor.opening_prompt_color = term.bright_black - self.Checkbox.selection_color = term.blue - self.Checkbox.selection_icon = '❯' - self.Checkbox.selected_icon = '◉' - self.Checkbox.selected_color = term.yellow + term.bold - self.Checkbox.unselected_color = term.normal - self.Checkbox.unselected_icon = '◯' - self.List.selection_color = term.blue - self.List.selection_cursor = '❯' - self.List.unselected_color = term.normal + return Fore.BLUE + text + Style.RESET_ALL + + +prompt_style = style_from_dict({ + Token.Separator: '#6C6C6C', + Token.QuestionMark: '#5F819D', + Token.Selected: '#48b5b5 bold', + Token.Pointer: '#48b5b5 bold', + Token.Instruction: '#77a371', + Token.Answer: '#48b5b5 bold', + Token.Question: '#289c64 bold', +}) diff --git a/nyaacli/nyaa_search.py b/nyaacli/nyaa_search.py index cb48b14..b7617de 100644 --- a/nyaacli/nyaa_search.py +++ b/nyaacli/nyaa_search.py @@ -1,3 +1,4 @@ +import sys from urllib import request from typing import Optional, List, Tuple from dataclasses import dataclass @@ -5,10 +6,9 @@ import feedparser from guessit import guessit -import inquirer +from PyInquirer import prompt -from nyaacli.colors import red, yellow, green, PromptTheme -from nyaacli.utils import clear_screen +from nyaacli.colors import red, green, prompt_style def get_file_extension(path: str) -> str: @@ -129,31 +129,38 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False if entry.seeders: seeders = f'{entry.seeders} Seeders' - if int(entry.seeders) > 40: - entry_title += f" - {green(seeders)}" - elif 40 > int(entry.seeders) > 20: - entry_title += f" - {yellow(seeders)}" - else: - entry_title += f" - {red(seeders)}" + # Colors on options are not working very well with PyInquirer + # if int(entry.seeders) > 40: + # entry_title += f" - {green(seeders)}" + # elif 40 > int(entry.seeders) > 20: + # entry_title += f" - {yellow(seeders)}" + # else: + # entry_title += f" - {red(seeders)}" + entry_title += f" - {seeders}" if entry.size: entry_title += f" - {entry.size}" entry.display_title = entry_title + choices = [{'name': entry.display_title, 'value': index} for index, entry in enumerate(entries[:5])] + questions = [ - inquirer.List( - 'entry', - message=green("Select one of the entries below"), - choices=[(str(entry.display_title), index) for index, entry in enumerate(entries[:5])], - ), + { + 'type': 'list', + 'choices': choices, + 'name': 'selection', + 'message': 'Select one of the entries below', + } ] - answer = inquirer.prompt(questions, theme=PromptTheme()) + answer = prompt(questions, style=prompt_style) - clear_screen() + if not answer: + # Cancelled with Ctrl + C + sys.exit(0) - index_choice = answer['entry'] - 1 + index_choice = answer['selection'] - 1 entry_choice = entries[index_choice] @@ -161,7 +168,7 @@ def search_torrent(search: str, episode: Optional[int] = None, dub: bool = False torrent_path = f'/tmp/{final_path}.torrent' - print(f"{green('[Downloading]')} '{torrent_path}'") + print(f"{green('[Downloading Torrent File]')} '{torrent_path}'") request.urlretrieve(entry_choice.link, torrent_path)