Coverage for src/twofas/cli_support.py: 100%
22 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-22 21:51 +0100
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-22 21:51 +0100
1import os
2import typing
4import configuraptor
5import questionary
6from configuraptor import postpone, asjson
7from typing_extensions import Never
9from .cli_settings import CliSettings
10from configuraptor import beautify
13@beautify
14class AppState(configuraptor.TypedConfig, configuraptor.Singleton):
15 verbose: bool = False
16 settings: CliSettings = postpone()
19state = AppState.load({})
21P = typing.ParamSpec("P")
22R = typing.TypeVar("R")
25@typing.overload
26def clear(fn: typing.Callable[P, R]) -> typing.Callable[P, R]:
27 """
28 When calling clear with parens, you get the same callable back.
29 """
32@typing.overload
33def clear(fn: None = None) -> typing.Callable[[typing.Callable[P, R]], R]:
34 """
35 When calling clear without parens, you'll get the same callable back later.
36 """
39def clear(
40 fn: typing.Callable[P, R] | None = None
41) -> typing.Callable[P, R] | typing.Callable[[typing.Callable[P, R]], R]: # pragma: no cover
42 """
43 Clear the screen before executing a function.
45 Examples:
46 @clear
47 def some_fun(): ...
49 @clear()
50 def other_func(): ...
51 """
52 if fn:
54 def inner(*args: P.args, **kwargs: P.kwargs) -> R:
55 os.system("clear") # nosec: B605 B607
56 return fn(*args, **kwargs)
58 return inner
59 else:
60 return clear # type: ignore
63@clear
64def exit_with_clear(status_code: int) -> Never: # pragma: no cover
65 exit(status_code)
68def generate_custom_style(
69 main_color: str = "green", # "#673ab7"
70 secondary_color: str = "#673ab7", # "#f44336"
71) -> questionary.Style:
72 """
73 Reusable questionary style for all prompts of this tool.
75 Primary and secondary color can be changed, other styles stay the same for consistency.
76 """
77 return questionary.Style(
78 [
79 ("qmark", f"fg:{main_color} bold"), # token in front of the question
80 ("question", "bold"), # question text
81 ("answer", f"fg:{secondary_color} bold"), # submitted answer text behind the question
82 ("pointer", f"fg:{main_color} bold"), # pointer used in select and checkbox prompts
83 ("highlighted", f"fg:{main_color} bold"), # pointed-at choice in select and checkbox prompts
84 ("selected", "fg:#cc5454"), # style for a selected item of a checkbox
85 ("separator", "fg:#cc5454"), # separator in lists
86 ("instruction", ""), # user instructions for select, rawselect, checkbox
87 ("text", ""), # plain text
88 ("disabled", "fg:#858585 italic"), # disabled choices for select and checkbox prompts
89 ]
90 )