scripts: Added <^> to punescape
Mainly for ^ for centering:
"%{hi!%}^8s" -> " hi! "
Note we can right bias with the nested modifier!
"%{%{hi!%}^7s%}>8s" -> " hi! "
Also note this does _not_ support fill characters like python's format.
I'm not sure it's tractable with %-modifiers, python's format must get
up to some funky parsing to make this work ("%s<s"?).
I think this also fixed the behavior of left-aligning numbers? Seems we
weren't handling that before.
This commit is contained in:
@@ -428,10 +428,10 @@ def punescape(s, attrs=None, start=None, end=None, submatch=None):
|
||||
'|' 'u....'
|
||||
'|' 'U........'
|
||||
'|' '\((?P<field>[^)]*)\)'
|
||||
'(?P<format>[+\- #0-9\.]*[siIdboxXfFeEgG])'
|
||||
'(?P<format>[<^>+\- #0-9\.]*[siIdboxXfFeEgG])'
|
||||
'|' '\{'
|
||||
'|' '\}'
|
||||
'(?P<subformat>[+\- #0-9\.]*[siIdboxXfFeEgG])' ')')
|
||||
'(?P<subformat>[<^>+\- #0-9\.]*[siIdboxXfFeEgG])' ')')
|
||||
|
||||
def format(f, v):
|
||||
if f[-1] in 'dboxX':
|
||||
@@ -447,10 +447,14 @@ def punescape(s, attrs=None, start=None, end=None, submatch=None):
|
||||
f = f.replace('i', 's').replace('I', 's')
|
||||
if '+' in f and not v.startswith('-'):
|
||||
v = '+'+v
|
||||
f = f.replace('+', '').replace('-', '')
|
||||
f = f.replace('+', '')
|
||||
else:
|
||||
f = ('<' if '-' in f else '>') + f.replace('-', '')
|
||||
v = str(v)
|
||||
|
||||
if '-' in f:
|
||||
f = '<' + f.replace('-', '')
|
||||
elif not any(d in f for d in '<^>'):
|
||||
f = '>' + f
|
||||
# note we need Python's new format syntax for binary
|
||||
return ('{:%s}' % f).format(v)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user