-
Notifications
You must be signed in to change notification settings - Fork 1
/
commentator.py
executable file
·46 lines (34 loc) · 1.49 KB
/
commentator.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
#!/usr/bin/env python3
# Author: github.com/danielhoherd
# License: MIT
# TODO:
# - Handle line breaks on input (seems hard if we use textwrap)
# - Handle stdin (typer doesn't handle defaults for nargs)
import textwrap
import typer
def commentator(
input: list[str] = typer.Argument(..., help="Input text"),
right_side: bool = typer.Option(False, help="Whether or not to show the right-hand-side border"),
delimiter: str = typer.Option("#", "--delimiter", "-d", help="Character or string to use for border"),
width: int = typer.Option(75, "--width", "-w", help="Width of the final padded line"),
):
"""Generate a block of text inside of a border, for example to use as a
template header."""
if width < len(delimiter) * 4: # just a rough minimum because this is an edge case
raise SystemExit("ERROR: delimiter is too wide relative to width")
print((delimiter * width)[:width])
if right_side:
text_width = width - (len(delimiter) * 2) - 2
wrapper = textwrap.TextWrapper(width=text_width)
lines = wrapper.wrap(text=" ".join(input))
for line in lines:
print(delimiter, f"{line:{text_width}}", delimiter)
else:
text_width = width - len(delimiter) - 1
wrapper = textwrap.TextWrapper(width=text_width)
lines = wrapper.wrap(text=" ".join(input))
for line in lines:
print(delimiter, line)
print((delimiter * width)[:width])
if __name__ == "__main__":
typer.run(commentator)