Skip to content

Commit

Permalink
added --unspace functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
HariSekhon committed May 1, 2023
1 parent 3680e4c commit 8865ba8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion center.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
sys.exit(4)

__author__ = 'Hari Sekhon'
__version__ = '0.4.2'
__version__ = '0.5.0'


class Center(CLI):

Expand All @@ -56,7 +57,9 @@ def __init__(self):
# 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):
Expand All @@ -66,11 +69,16 @@ def add_options(self):
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:
Expand All @@ -83,6 +91,11 @@ def space(self, 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:
Expand All @@ -102,9 +115,12 @@ def process_line(self, line):
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()

0 comments on commit 8865ba8

Please sign in to comment.