-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection_format.py
102 lines (77 loc) · 2.29 KB
/
section_format.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
#!/bin/python
from __future__ import annotations
"""
Summary: Command-line script to make pretty Go comments for section headings.
Date: 2022-02-19
Examples
--------
>>> python section_format.py Function Demo 4
// ----------------------- //
// ---- Function Demo ---- //
// ----------------------- //
>>> python section_format.py Function Demo
// --------------------- //
// --- Function Demo --- //
// --------------------- //
"""
from sys import argv
def format_middle(string, buffer = 3):
pstr = f" {string} "
fstr = pstr.center(len(pstr) + (2 * buffer), "-")
return add_sides(fstr)
def format_top_bottom(middle_str: str):
slen = len(middle_str)
fstr = repeats("-", slen - 6)
return add_sides(fstr)
def add_sides(string, side="//"):
return f"{side} {string} {side}"
def repeats(s: str, n: int) -> str:
"""Return string of string repeated n times."""
return s * n
def striput(msg: str = None) -> str:
"""Input handler that strips excess whitespace
and allows for optional message.
"""
if msg is None:
return input().strip()
else:
return input(msg).strip()
if __name__ == "__main__":
lines = [""] * 2
phrase = " "
side_buffer = None
# If arguments given...
if len(argv) > 1:
args = argv[1:]
# print("Your args: ", args)
if len(args) > 1:
tmp = str(args[-1]).strip()
if tmp.isdigit():
side_buffer = int(tmp)
phrase = " ".join(args[:-1])
else:
side_buffer = 3
phrase = " ".join(args)
else:
side_buffer = 3
phrase = " ".join(args)
# ...otherwise, ask for arguments.
else:
phrase = striput("Enter phrase: ")
while side_buffer is None:
tmp = striput("Enter buffer amount [default: 3]: ")
if len(tmp) == 0:
side_buffer = 3
break
else:
try:
ntmp = int(tmp)
side_buffer = ntmp
except ValueError:
side_buffer = None
lines[1] = format_middle(phrase, side_buffer)
lines[0] = format_top_bottom(lines[1])
lines.append(lines[0])
print("\n")
print("\n".join(lines))
print("\n")