-
Notifications
You must be signed in to change notification settings - Fork 84
/
utils.py
61 lines (53 loc) · 1.28 KB
/
utils.py
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
#!/usr/bin/env python3
"""
Hulk v3
Collection of Utitlity functions.
"""
from typing import Optional
def bordered(
text: str,
num_internal_colors: Optional[int] = 0
) -> str:
"""
Returns a string with a border around the text.
:param text: The text to be bordered.
:type text: str
:param num_internal_colors: The number of internal colors.
:type num_internal_colors: Optional[int]
:return: The bordered text.
:rtype: str
"""
sentences = trim_lines(text).splitlines()
hor = max(len(line) for line in sentences) + 2
pad = [
'┌' + ('─' * 4)
+ ('─' * (
hor - 5 - (num_internal_colors * 8)
)) + '┐'
]
pad.extend(
'│ ' + (line + ' ' * hor)[
:hor - 2
+ num_internal_colors
] + '│'
for line in sentences
)
pad.append(
'└' + ('─' * 4)
+ ('─' * (
hor - 5 - (num_internal_colors * 8)
)) + '┘'
)
return '\n'.join(pad)
def trim_lines(text: str) -> str:
"""
Trims the lines of the text.
:param text: The text to be trimmed.
:type text: str
:return: The trimmed text.
:rtype: str
"""
return '\n'.join(
line.strip()
for line in text.splitlines()
)