-
Notifications
You must be signed in to change notification settings - Fork 341
/
center.py
executable file
·126 lines (110 loc) · 4.4 KB
/
center.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
#!/usr/bin/env python3
# vim:ts=4:sts=4:sw=4:et
# args: -s <<< "Auth & Config"
# args: -s <<< "GKE Clusters"
#
# Author: Hari Sekhon
# Date: 2016-01-29 21:05:38 +0000 (Fri, 29 Jan 2016)
#
# https://github.com/HariSekhon/DevOps-Python-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
# to help improve or steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
"""
Tool to re-center text args or text from standard input to a given width
while ignoring the leading comment char such as the common unix # symbol
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#from __future__ import unicode_literals
import os
import re
import sys
libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib'))
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import isChars, log_option
from harisekhon import CLI
except ImportError as _:
print('module import failed: %s' % _, file=sys.stderr)
print("Did you remember to build the project by running 'make'?", file=sys.stderr)
print("Alternatively perhaps you tried to copy this program out without it's adjacent libraries?", file=sys.stderr)
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.5.1'
class Center(CLI):
def __init__(self):
# Python 2.x
super(Center, self).__init__()
# Python 3.x
# super().__init__()
# this doesn't put enough spaces around ampersands, eg. in "Auth & Config"
#self.re_bound = re.compile(r'(\b)')
self.re_spaces = re.compile(r'(\s)')
self.re_multiple_spaces = re.compile(r'(\s){2}')
self.re_chars = re.compile(r'([^\s])(?!\s)')
self.re_chars_spaced = re.compile(r'([^\s])\s')
self.timeout_default = None
def add_options(self):
self.add_opt('-w', '--width', default=80, type='int', metavar='<num_chars>',
help='Target line width to center for in chars')
self.add_opt('-n', '--no-comment', action='store_true',
help='No comment prefix handling')
self.add_opt('-s', '--space', action='store_true', default=False,
help='Space all chars out, makes bigger headings')
self.add_opt('-u', '--unspace', action='store_true', default=False,
help='Removes spaces betweeen chars out, the inverse of --space')
def run(self):
log_option('width', self.get_opt('width'))
log_option('no comment prefix', self.get_opt('no_comment'))
log_option('space chars', self.get_opt('space'))
log_option('unspace chars', self.get_opt('unspace'))
if self.get_opt('space') and self.get_opt('unspace'):
self.usage("--space and --unspace are mutually exclusive!")
if self.args:
self.process_line(' '.join(self.args))
else:
for line in sys.stdin:
self.process_line(line)
def space(self, line):
#line = self.re_bound.sub(r' ', line)
line = self.re_spaces.sub(r'\1\1\1', line)
line = self.re_chars.sub(r'\1 ', line)
return line
def unspace(self, line):
line = self.re_chars_spaced.sub(r'\1', line)
line = self.re_multiple_spaces.sub(r'\1', line)
return line
def process_line(self, line):
char = ''
if not line:
return
if not self.get_opt('no_comment'):
char = ' '
# preliminary strip() to be able to pick up # if it isn't the first char and their are spaces before it
line = line.strip()
if line and isChars(line[0], '#'):
char = line[0]
line = line.lstrip(char)
elif len(line) > 1 and isChars(line[0:1], '/'):
char = '//'
line = line.lstrip(char)
elif len(line) > 1 and isChars(line[0:1], '-'):
char = '--'
line = line.lstrip(char)
if self.get_opt('space'):
line = self.space(line)
if self.get_opt('unspace'):
line = self.unspace(line)
line = line.strip()
side = int(max((self.get_opt('width') - len(line)) / 2, 0))
print(char + ' ' * side + line)
if __name__ == '__main__':
Center().main()