-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_fence.py
238 lines (218 loc) · 6.77 KB
/
print_fence.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
print_fence - print ASCII character fences
References:
1. [ASCII Art](https://blog.csdn.net/u014636245/article/details/83661559)
2. [art · PyPI](https://pypi.org/project/art/)
3. [pprint — Data pretty printer](https://docs.python.org/3/library/pprint.html)
TODO:
1. Print colored fences.
2. Process multiple rows of strings.
"""
import argparse
from pathlib import Path
from time import sleep
from typing import Any, Union
def print_fence(contents: Any,
set_width: int = 4,
set_height: int = 5,
fence_style: str = "hyphen",
add_blank_line: bool = True,
sleep_time: Union[int, float, None] = None) -> None:
"""
Use ASCII characters to fence what you would like to print.
Args
----
contents: Any
The contents you want to print.
set_width: int
The augment to the width of the fence.
Default is `4`.
set_height: int
The augment to the height of the fence.
Default is `5`.
fence_style: str
The style of the printed fence.
Default is `"hyphen"`.
add_blank_line: bool
If `True`, a blank line will be inserted
at the end of the printed contents.
Default is `True`.
sleep_time: int, float, or `None`
The interval of each printing.
Default is `None`.
Returns
-------
Returns `None`: print the input contents.
"""
colors = {
"Black": "\033[30m",
"Red": "\033[31m",
"Green": "\033[32m",
"Yellow": "\033[33m",
"Blue": "\033[34m",
"Magenta": "\033[35m",
"Cyan": "\033[36m",
"White": "\033[37m",
"Default": "\033[39m"
}
def pause(interval: Union[int, float, None]) -> None:
"""
Pause printing.
"""
if interval is None:
pass
elif isinstance(interval, (int, float)):
sleep(interval)
else:
raise TypeError(
f"Invalid type of `sleep_time`: {type(interval)}"
)
# Define the width and height of the fence
#
# NOTE: Convert `contents` of `Any` type to string type.
contents = str(contents)
width = len(contents) + set_width
height = set_height
# Output ASCII character fences
if fence_style == "hyphen":
fence_top = '+' + '-' * (width - 2) + '+\n'
fence_side = '|' + ' ' * (width - 2) + '|\n'
elif fence_style == "asterisk":
fence_top = '*' + '*' * (width - 2) + '*\n'
fence_side = '*' + ' ' * (width - 2) + '*\n'
else:
raise ValueError(f'Invalid fence style: "{fence_style}"')
print(fence_top, end='')
for i in range(height - 2):
if i == (height - 2) // 2:
padding_left = ' ' * ((width - len(contents)) // - 1) # NOTE: The original form: ((width - len(content)) // 2 - 1)
padding_right = ' ' * ((width - len(contents)) // 2 - 1 + (width - len(contents)) % 2)
if fence_style == "hyphen":
print(f'| {padding_left}{contents}{padding_right}|\n', end='')
elif fence_style == "asterisk":
print(f'* {padding_left}{contents}{padding_right}*\n', end='')
else:
raise ValueError(f'Invalid fence style: "{fence_style}"')
pause(sleep_time)
else:
print(fence_side, end='')
pause(sleep_time)
if fence_style == "hyphen":
print(fence_top[:-2] + '+', end='')
elif fence_style == "asterisk":
print(fence_top[:-2] + '*', end='')
else:
raise ValueError(f'Invalid fence style: "{fence_style}"')
if add_blank_line:
print("\n")
elif not add_blank_line:
pass
def main() -> None:
"""
The main function.
"""
parser = argparse.ArgumentParser(
prog=f"{Path(__file__).name}",
description=(
"Use ASCII characters to fence what you would like to print."
),
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
"contents",
nargs="?",
type=str,
help="The printed contents."
)
parser.add_argument(
"-p",
"--printed-contents",
type=str,
help="The contents to be printed."
)
parser.add_argument(
"-s",
"--fence-style",
default="hyphen",
type=str,
choices=["hyphen", "asterisk"],
help=(
"The style of the fence.\n"
"The default is: %(default)s"
)
)
parser.add_argument(
"-b",
"--blank-line",
default="yes",
type=str,
choices=["yes", "no"],
help=(
"Whether to insert a blank line "
"at the end of the printed contents or not.\n"
"The default is: %(default)s"
)
)
parser.add_argument(
"-t",
"--sleep-time",
default="no",
type=str,
help=(
"The interval of each printing.\n"
"The default is: %(default)s"
)
)
parser.add_argument(
"-sw",
"--set-width",
default=4,
type=int,
help=(
"The width of the fence.\n"
"The default is: %(default)s"
)
)
parser.add_argument(
"-sh",
"--set-height",
default=5,
type=int,
help=(
"The height of the fence.\n"
"The default is: %(default)s"
)
)
parser.add_argument(
"-v",
"--version",
action="version",
help="Print the version number of %(prog)s and exit.",
version="%(prog)s 5.0.0"
)
command_args = parser.parse_args()
if command_args.contents is not None:
print_fence(
contents=command_args.contents,
set_width=command_args.set_width,
set_height=command_args.set_height,
fence_style=command_args.fence_style,
add_blank_line=True if command_args.blank_line == "yes" else False,
sleep_time=None if command_args.sleep_time == "no" else float(command_args.sleep_time)
)
elif command_args.contents is None:
if command_args.printed_contents is not None:
print_fence(
contents=command_args.printed_contents,
set_width=command_args.set_width,
set_height=command_args.set_height,
fence_style=command_args.fence_style,
add_blank_line=True if command_args.blank_line == "yes" else False,
sleep_time=None if command_args.sleep_time == "no" else float(command_args.sleep_time)
)
elif command_args.printed_contents is None:
parser.print_usage()
if __name__ == "__main__":
main()