Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/cmdict/run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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]:
Expand Down