Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete after the eol command #216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dynamic = [
"version",
]
dependencies = [
"argcomplete>=3.4",
"httpx>=0.19",
"platformdirs",
"prettytable>=2.4",
Expand Down
11 changes: 7 additions & 4 deletions src/norwegianblue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ def norwegianblue(
return output


def all_products() -> list[str]:
"""Get all known products from the API or cache"""
return norwegianblue("all").splitlines()


@lru_cache(maxsize=None)
def suggest_product(product: str) -> str:
"""Provide the best suggestion based on a typed product"""
import difflib

# Get all known products from the API or cache
all_products = norwegianblue("all").splitlines()

# Find the closest match
result = difflib.get_close_matches(product, all_products, n=1)
result = difflib.get_close_matches(product, all_products(), n=1)
logging.info("Suggestion:\t%s (score: %d)", *result)
return result[0] if result else ""

Expand Down
11 changes: 9 additions & 2 deletions src/norwegianblue/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
"""
CLI to show end-of-life dates for a number of products, from https://endoflife.date

Expand All @@ -19,6 +20,7 @@
import platform
import sys

import argcomplete
from termcolor import colored

import norwegianblue
Expand All @@ -27,6 +29,11 @@
atexit.register(_cache.clear)


def ProductCompleter(**kwargs):
"""The list of all products to feed autocompletion"""
return norwegianblue.all_products()


def main() -> None:
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand All @@ -36,7 +43,7 @@ def main() -> None:
nargs="*",
default=["all"],
help="product to check, or 'all' to list all available (default: 'all')",
)
).completer = ProductCompleter
parser.add_argument(
"-f",
"--format",
Expand Down Expand Up @@ -115,7 +122,7 @@ def main() -> None:
help=f"output in {help_text}",
)
parser.set_defaults(formatter="pretty")

argcomplete.autocomplete(parser)
args = parser.parse_args()

if args.format:
Expand Down
Loading