-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (44 loc) · 1.64 KB
/
main.py
File metadata and controls
64 lines (44 loc) · 1.64 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
import re
import textwrap
from .characters import CHARS
__version__ = '6.0'
CHARS = dict(sorted(CHARS.items()))
char_names = list(CHARS.keys())
class CowsayError(LookupError):
pass
def generate_bubble(text: str) -> list:
text = textwrap.fill(text, width=49)
lines = [line.strip() for line in text.splitlines()]
text_width = max([len(line) for line in lines])
output = [" " + "_" * text_width]
if len(lines) > 1:
output.append(" /" + " " * text_width + "\\")
for line in lines:
output.append("| " + line + " " * (text_width - len(line) + 1) + "|")
if len(lines) > 1:
output.append(" \\" + " " * text_width + "/")
output.append(" " + "=" * text_width)
return output
def generate_char(char_lines: str, text_width: int) -> list:
output = []
char_lines = char_lines.split('\n')
char_lines = [i for i in char_lines if len(i) != 0]
for line in char_lines:
output.append(' ' * text_width + line)
return output
def draw(text: str, char_lines: str, to_console: bool = True) -> str:
if len(re.sub(r'\s', '', text)) == 0:
raise CowsayError('Pass something meaningful to cowsay')
output = generate_bubble(text)
text_width = max([len(line) for line in output]) - 4 # 4 is the frame
output += generate_char(char_lines, text_width)
if to_console:
for line in output:
print(line)
else:
return '\n'.join(output)
def get_output_string(char: str, text: str) -> str:
if char in CHARS:
return draw(text, CHARS[char], to_console=False)
else:
raise CowsayError(f'Available Characters: {char_names}')