From 7bb44639475fb83ef459216743e4e137e2429163 Mon Sep 17 00:00:00 2001 From: edxu96 Date: Thu, 30 Jul 2020 15:47:28 +0200 Subject: [PATCH] preview extracted words in the highlight color (#62) --- src/cmdict/run_script.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cmdict/run_script.py b/src/cmdict/run_script.py index 592a394..7eb85e0 100644 --- a/src/cmdict/run_script.py +++ b/src/cmdict/run_script.py @@ -10,7 +10,7 @@ from tqdm import tqdm from cmdict.ecdict_connector import ecdict_engine -from cmdict.pdf_tools import extract_words +from cmdict.pdf_tools import extract_words, PREVIEW_COLORS from cmdict.txt_tools import scan_words DB_URL = "https://github.com/skywind3000/ECDICT/releases/download/1.0.28/ecdict-sqlite-28.zip" # noqa: E501 @@ -118,7 +118,9 @@ def extract(pdf_path, color): if _valid_db_exists(): words = extract_words(pdf_path, color) for i, word in enumerate(words): - _echo_item(word, ecdict_engine.query(word)) + c = PREVIEW_COLORS[color.lower()] + str_color = f"\033[38;2;{c[0]};{c[1]};{c[2]}m" + _echo_item(word, ecdict_engine.query(word), str_color) else: _echo_warn_download() @@ -133,16 +135,21 @@ def _tab_echo(s, tabs=4): click.echo(tabs * " " + s) -def _echo_item(word, res): +def _echo_item(word, res, str_color=None): """Echo word search result to cli. Args: word (str): The word. res (dict): The word search result. + str_color (str): Representing the color. """ _echo_divider() if res: - click.echo(Fore.CYAN + Style.BRIGHT + word + "\n") + if str_color: + click.echo(str_color + word + "\x1b[0m" + "\n") + else: + click.echo(Fore.CYAN + Style.BRIGHT + word + "\n") + for k in res: if k in ("definition", "trans"): if res[k]: