-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbangen.py
More file actions
211 lines (169 loc) · 5.58 KB
/
bangen.py
File metadata and controls
211 lines (169 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from __future__ import annotations
import time
from pathlib import Path
import pyfiglet
from rich import box
from rich.console import Console
from rich.live import Live
from rich.panel import Panel
from rich.prompt import Confirm, Prompt
from rich.text import Text
APP_NAME = "Bangen"
DEFAULT_TEXT = "Bangen"
DEFAULT_FONT = "ansi_shadow"
DEFAULT_COLOR = "cyan"
COLORS = ["cyan", "red", "green", "yellow", "magenta"]
PRESET_FONTS = [
"ansi_shadow",
"slant",
"standard",
"block",
"big",
"banner3-D",
"speed",
"doom",
"starwars",
"small",
"smslant",
]
def welcome(console: Console) -> None:
message = (
"Welcome to Bangen, a terminal ASCII banner generator.\n"
"Choose a font, pick a color, and render your text inside a panel."
)
console.print(
Panel(
message,
title=APP_NAME,
border_style="cyan",
box=box.ROUNDED,
padding=(1, 2),
)
)
def prompt_text(console: Console) -> str:
text = Prompt.ask("Text to render", default=DEFAULT_TEXT).strip()
if not text:
console.print(f"[yellow]Empty input. Using {DEFAULT_TEXT}.[/yellow]")
return DEFAULT_TEXT
return text
def get_all_fonts() -> set[str]:
try:
return set(pyfiglet.FigletFont.getFonts())
except Exception:
return set(PRESET_FONTS)
def select_font(console: Console) -> str:
console.print("[bold]Font presets[/bold]")
for idx, font in enumerate(PRESET_FONTS, start=1):
console.print(f"{idx}. {font}")
choice = Prompt.ask("Font (name or number)", default=DEFAULT_FONT).strip()
if choice.isdigit():
index = int(choice)
if 1 <= index <= len(PRESET_FONTS):
return PRESET_FONTS[index - 1]
if choice in get_all_fonts():
return choice
console.print(f"[yellow]Unknown font '{choice}'. Using {DEFAULT_FONT}.[/yellow]")
return DEFAULT_FONT
def select_color(console: Console) -> str:
console.print(f"[bold]Colors[/bold]: {', '.join(COLORS)}")
choice = Prompt.ask("Color", default=DEFAULT_COLOR).strip().lower()
if choice in COLORS:
return choice
console.print(f"[yellow]Unknown color '{choice}'. Using {DEFAULT_COLOR}.[/yellow]")
return DEFAULT_COLOR
def select_title(console: Console) -> str | None:
title = Prompt.ask("Panel title (optional)", default="", show_default=False).strip()
return title or None
def select_border(console: Console) -> bool:
return Confirm.ask("Show panel border?", default=True)
def select_animation(console: Console) -> tuple[bool, float]:
animate = Confirm.ask("Animate line by line?", default=False)
if not animate:
return False, 0.0
default_delay = 0.03
delay_input = Prompt.ask(
"Delay per line in seconds", default=f"{default_delay}"
).strip()
try:
delay = float(delay_input)
if delay < 0:
raise ValueError
return True, delay
except ValueError:
console.print(
f"[yellow]Invalid delay '{delay_input}'. Using {default_delay}.[/yellow]"
)
return True, default_delay
def render_banner(text: str, font: str) -> str:
try:
figlet = pyfiglet.Figlet(font=font)
except pyfiglet.FontNotFound:
figlet = pyfiglet.Figlet(font=DEFAULT_FONT)
return figlet.renderText(text)
def build_panel(banner: str, color: str, title: str | None, border: bool) -> Panel:
banner_text = Text(banner.rstrip("\n"), style=color, justify="left", no_wrap=True)
chosen_box = box.HEAVY if border else getattr(box, "NONE", box.MINIMAL)
return Panel(
banner_text,
title=title,
border_style=color,
box=chosen_box,
padding=(1, 2),
)
def animate_banner(
console: Console,
banner: str,
color: str,
title: str | None,
border: bool,
delay: float,
) -> None:
lines = banner.rstrip("\n").splitlines()
if not lines:
console.print(build_panel("", color, title, border))
return
revealed: list[str] = []
with Live(console=console, refresh_per_second=30) as live:
for line in lines:
revealed.append(line)
live.update(build_panel("\n".join(revealed), color, title, border))
time.sleep(delay)
def show_banner(
console: Console,
banner: str,
color: str,
title: str | None,
border: bool,
animate: bool,
delay: float,
) -> None:
if animate:
animate_banner(console, banner, color, title, border, delay)
else:
console.print(build_panel(banner, color, title, border))
def maybe_save_banner(console: Console, banner: str) -> None:
if not Confirm.ask("Save banner to a .txt file?", default=False):
return
path_input = Prompt.ask("Output path", default="banner.txt").strip()
path = Path(path_input).expanduser()
if path.suffix == "":
path = path.with_suffix(".txt")
try:
path.write_text(banner.rstrip("\n") + "\n", encoding="utf-8")
console.print(f"[green]Saved to {path}[/green]")
except OSError as exc:
console.print(f"[red]Failed to save banner: {exc}[/red]")
def main() -> None:
console = Console()
welcome(console)
text = prompt_text(console)
font = select_font(console)
color = select_color(console)
title = select_title(console)
border = select_border(console)
animate, delay = select_animation(console)
banner = render_banner(text, font)
show_banner(console, banner, color, title, border, animate, delay)
maybe_save_banner(console, banner)
if __name__ == "__main__":
main()