-
Notifications
You must be signed in to change notification settings - Fork 0
/
center
executable file
·31 lines (28 loc) · 1.06 KB
/
center
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
#!/usr/bin/env python3
import click
import sys
import shutil
@click.command()
@click.argument('input', required=False, type=click.File('r'))
@click.argument('output', required=False, type=click.File('w'))
@click.option('-l', '--length', help='maximum line length [default:current terminal size]', type=click.INT)
@click.option('-i', '--indent', help='indent text instead of centering', is_flag=True)
def cli(input, output, length, indent):
"""Simple, pipeable tool for centering text"""
columns = shutil.get_terminal_size()[0]
source = input.readlines() if input else sys.stdin
if indent:
source = list(source)
max_line = len(sorted(source, key=len, reverse=True)[0])
indent_size = int((columns - max_line) / 2)
format_ = '{}{{}}\n'.format(indent_size * ' ')
else:
format_ = '{{:^{}}}\n'.format(length or int(columns))
for line in source:
text = format_.format(line.strip())
if output:
output.write(text)
else:
sys.stdout.write(text)
if __name__ == "__main__":
cli()