mirror of
https://codeberg.org/hyperreal/get-def
synced 2025-01-18 16:33:45 +01:00
feat: add pager
This commit is contained in:
parent
89240d27a7
commit
006f808dca
128
get_def.py
128
get_def.py
@ -4,24 +4,89 @@
|
|||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
get-def (WORD)
|
get-def (WORD)
|
||||||
|
get-def -n (WORD)
|
||||||
get-def -h
|
get-def -h
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
get-def hello
|
get-def hello
|
||||||
|
get-def -n hello
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-n, --no-pager do not use a pager
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
from rich import box, print
|
from rich import box
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.padding import Padding
|
from rich.padding import Padding
|
||||||
from rich.table import Table
|
from rich.table import Table
|
||||||
from rich.text import Text
|
from rich.text import Text
|
||||||
|
|
||||||
|
|
||||||
|
def print_def(console: Console, response: requests.Response):
|
||||||
|
word = response.json()[0].get("word")
|
||||||
|
|
||||||
|
console.print()
|
||||||
|
console.print(" :arrow_forward: ", Text(word, style="bold red", justify="center"))
|
||||||
|
console.print()
|
||||||
|
|
||||||
|
phonetics = response.json()[0].get("phonetics")
|
||||||
|
phonetics_table = Table(box=box.SQUARE)
|
||||||
|
phonetics_table.add_column("Phonetic Text", style="cyan")
|
||||||
|
phonetics_table.add_column("Phonetic Audio")
|
||||||
|
if len(phonetics) > 0:
|
||||||
|
for item in phonetics:
|
||||||
|
text = item.get("text") if item.get("text") else "None"
|
||||||
|
audio = item.get("audio") if item.get("audio") else "None"
|
||||||
|
phonetics_table.add_row(text, audio)
|
||||||
|
console.print(phonetics_table)
|
||||||
|
|
||||||
|
console.print(
|
||||||
|
"IPA chart: https://www.internationalphoneticassociation.org/IPAcharts/inter_chart_2018/IPA_2018.html"
|
||||||
|
)
|
||||||
|
console.print()
|
||||||
|
|
||||||
|
meanings = response.json()[0].get("meanings")
|
||||||
|
|
||||||
|
for item in meanings:
|
||||||
|
console.print(
|
||||||
|
f"[bold]{meanings.index(item) + 1}. [underline]{item["partOfSpeech"]}"
|
||||||
|
)
|
||||||
|
for definition in item["definitions"]:
|
||||||
|
console.print(
|
||||||
|
Padding(
|
||||||
|
f"[bold blue]Definition:[/bold blue] {definition.get("definition")}",
|
||||||
|
(0, 0, 0, 3),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if definition.get("example") is not None:
|
||||||
|
console.print(
|
||||||
|
Padding(
|
||||||
|
f"[bold magenta]Example:[/bold magenta] {definition.get("example")}",
|
||||||
|
(0, 0, 0, 3),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if definition.get("synonyms"):
|
||||||
|
console.print(
|
||||||
|
Padding(
|
||||||
|
f"[bold yellow]Synonyms:[/bold yellow] "
|
||||||
|
+ ", ".join(definition.get("synonyms")),
|
||||||
|
(0, 0, 0, 3),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if definition.get("antonyms"):
|
||||||
|
console.print(
|
||||||
|
Padding(
|
||||||
|
f"[bold yellow]Antonyms:[/bold yellow] "
|
||||||
|
+ ", ".join(definition.get("antonyms")),
|
||||||
|
(0, 0, 0, 3),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
console.print()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = docopt(__doc__)
|
args = docopt(__doc__)
|
||||||
|
|
||||||
@ -38,64 +103,13 @@ def main():
|
|||||||
"The connection has timed out. This might indicate an issue with DNS, firewall, or your internet connection."
|
"The connection has timed out. This might indicate an issue with DNS, firewall, or your internet connection."
|
||||||
)
|
)
|
||||||
|
|
||||||
word = response.json()[0].get("word")
|
|
||||||
|
|
||||||
console = Console(width=100)
|
console = Console(width=100)
|
||||||
print()
|
|
||||||
print(" :arrow_forward: ", Text(word, style="bold red", justify="center"))
|
|
||||||
print()
|
|
||||||
|
|
||||||
phonetics = response.json()[0].get("phonetics")
|
if not args["--no-pager"]:
|
||||||
phonetics_table = Table(box=box.SQUARE)
|
with console.pager(styles=True):
|
||||||
phonetics_table.add_column("Phonetic Text", style="cyan")
|
print_def(console, response)
|
||||||
phonetics_table.add_column("Phonetic Audio")
|
else:
|
||||||
if len(phonetics) > 0:
|
print_def(console, response)
|
||||||
for item in phonetics:
|
|
||||||
text = item.get("text") if item.get("text") else "None"
|
|
||||||
audio = item.get("audio") if item.get("audio") else "None"
|
|
||||||
phonetics_table.add_row(text, audio)
|
|
||||||
console.print(phonetics_table)
|
|
||||||
|
|
||||||
print(
|
|
||||||
"Click to view [link=https://www.internationalphoneticassociation.org/IPAcharts/inter_chart_2018/IPA_2018.html]Interactive IPA chart[/link]"
|
|
||||||
)
|
|
||||||
print()
|
|
||||||
|
|
||||||
meanings = response.json()[0].get("meanings")
|
|
||||||
|
|
||||||
for item in meanings:
|
|
||||||
print(f"[bold]{meanings.index(item) + 1}. [underline]{item["partOfSpeech"]}")
|
|
||||||
for definition in item["definitions"]:
|
|
||||||
print(
|
|
||||||
Padding(
|
|
||||||
f"[bold blue]Definition:[/bold blue] {definition.get("definition")}",
|
|
||||||
(0, 0, 0, 3),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if definition.get("example") is not None:
|
|
||||||
print(
|
|
||||||
Padding(
|
|
||||||
f"[bold magenta]Example:[/bold magenta] {definition.get("example")}",
|
|
||||||
(0, 0, 0, 3),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if definition.get("synonyms"):
|
|
||||||
print(
|
|
||||||
Padding(
|
|
||||||
f"[bold yellow]Synonyms:[/bold yellow] "
|
|
||||||
+ ", ".join(definition.get("synonyms")),
|
|
||||||
(0, 0, 0, 3),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if definition.get("antonyms"):
|
|
||||||
print(
|
|
||||||
Padding(
|
|
||||||
f"[bold yellow]Antonyms:[/bold yellow] "
|
|
||||||
+ ", ".join(definition.get("antonyms")),
|
|
||||||
(0, 0, 0, 3),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
print()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "get-def"
|
name = "get-def"
|
||||||
version = "0.4"
|
version = "0.5"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="Jeffrey Serio", email="hyperreal@fedoraproject.org" },
|
{ name="Jeffrey Serio", email="hyperreal@fedoraproject.org" },
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user