Skip to content

Commit

Permalink
Add [Y/n] prompt to suggestion (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Feb 28, 2023
2 parents df9a13a + 5c201ee commit 48d4a8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/norwegianblue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime as dt
import json
import logging
from functools import lru_cache

from dateutil.relativedelta import relativedelta
from termcolor import colored
Expand Down Expand Up @@ -66,8 +67,9 @@ def norwegianblue(

logging.info("HTTP status code: %d", r.status_code)
if r.status_code == 404:
suggestion = _suggest_product(product)
raise ValueError(ERROR_404_TEXT.format(product, suggestion))
suggestion = suggest_product(product)
msg = ERROR_404_TEXT.format(product, suggestion)
raise ValueError(msg)

# Raise if we made a bad request
# (4XX client error or 5XX server error response)
Expand Down Expand Up @@ -98,7 +100,8 @@ def norwegianblue(
return output


def _suggest_product(product: str) -> str:
@lru_cache(maxsize=None)
def suggest_product(product: str) -> str:
import warnings

with warnings.catch_warnings():
Expand Down
16 changes: 15 additions & 1 deletion src/norwegianblue/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import platform
import sys

from termcolor import colored

import norwegianblue
from norwegianblue import _cache

Expand Down Expand Up @@ -103,7 +105,19 @@ def main() -> None:
show_title=multiple_products,
)
except ValueError as e:
sys.exit(e)
prompt = f"{e} [Y/n] "
if args.color != "no":
prompt = colored(prompt, "yellow")
answer = input(prompt)
if answer not in ("", "y", "Y"):
sys.exit()
suggestion = norwegianblue.suggest_product(product)
output = norwegianblue.norwegianblue(
product=suggestion,
format=args.format,
color=args.color,
show_title=multiple_products,
)
print(output)
print()
if args.web:
Expand Down

0 comments on commit 48d4a8c

Please sign in to comment.