Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/utils/console.py
327 views
1
import re
2
3
from rich.columns import Columns
4
from rich.console import Console
5
from rich.markdown import Markdown
6
from rich.padding import Padding
7
from rich.panel import Panel
8
from rich.text import Text
9
10
console = Console()
11
12
13
def print_markdown(text) -> None:
14
"""Prints a rich info message. Support Markdown syntax."""
15
16
md = Padding(Markdown(text), 2)
17
console.print(md)
18
19
20
def print_step(text) -> None:
21
"""Prints a rich info message."""
22
23
panel = Panel(Text(text, justify="left"))
24
console.print(panel)
25
26
27
def print_table(items) -> None:
28
"""Prints items in a table."""
29
30
console.print(Columns([Panel(f"[yellow]{item}", expand=True) for item in items]))
31
32
33
def print_substep(text, style="") -> None:
34
"""Prints a rich colored info message without the panelling."""
35
console.print(text, style=style)
36
37
38
def handle_input(
39
message: str = "",
40
check_type=False,
41
match: str = "",
42
err_message: str = "",
43
nmin=None,
44
nmax=None,
45
oob_error="",
46
extra_info="",
47
options: list = None,
48
default=NotImplemented,
49
optional=False,
50
):
51
if optional:
52
console.print(message + "\n[green]This is an optional value. Do you want to skip it? (y/n)")
53
if input().casefold().startswith("y"):
54
return default if default is not NotImplemented else ""
55
if default is not NotImplemented:
56
console.print(
57
"[green]"
58
+ message
59
+ '\n[blue bold]The default value is "'
60
+ str(default)
61
+ '"\nDo you want to use it?(y/n)'
62
)
63
if input().casefold().startswith("y"):
64
return default
65
if options is None:
66
match = re.compile(match)
67
console.print("[green bold]" + extra_info, no_wrap=True)
68
while True:
69
console.print(message, end="")
70
user_input = input("").strip()
71
if check_type is not False:
72
try:
73
user_input = check_type(user_input)
74
if (nmin is not None and user_input < nmin) or (
75
nmax is not None and user_input > nmax
76
):
77
# FAILSTATE Input out of bounds
78
console.print("[red]" + oob_error)
79
continue
80
break # Successful type conversion and number in bounds
81
except ValueError:
82
# Type conversion failed
83
console.print("[red]" + err_message)
84
continue
85
elif match != "" and re.match(match, user_input) is None:
86
console.print("[red]" + err_message + "\nAre you absolutely sure it's correct?(y/n)")
87
if input().casefold().startswith("y"):
88
break
89
continue
90
else:
91
# FAILSTATE Input STRING out of bounds
92
if (nmin is not None and len(user_input) < nmin) or (
93
nmax is not None and len(user_input) > nmax
94
):
95
console.print("[red bold]" + oob_error)
96
continue
97
break # SUCCESS Input STRING in bounds
98
return user_input
99
console.print(extra_info, no_wrap=True)
100
while True:
101
console.print(message, end="")
102
user_input = input("").strip()
103
if check_type is not False:
104
try:
105
isinstance(eval(user_input), check_type)
106
return check_type(user_input)
107
except:
108
console.print(
109
"[red bold]"
110
+ err_message
111
+ "\nValid options are: "
112
+ ", ".join(map(str, options))
113
+ "."
114
)
115
continue
116
if user_input in options:
117
return user_input
118
console.print(
119
"[red bold]" + err_message + "\nValid options are: " + ", ".join(map(str, options)) + "."
120
)
121
122