Skip to content

Commit 1ced4f8

Browse files
authored
Split code for alignment
Probably more performant, even if it introduces some repetition.
1 parent d5478e7 commit 1ced4f8

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

align_yaml.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
def align_yaml(str, pad=0, aligned_colons=False):
44
props = re.findall(r'^\s*[\S]+:', str, re.MULTILINE)
55
longest = max([len(i) for i in props]) + pad
6-
return ''.join([i+'\n' for i in map(lambda str:
7-
re.sub(r'^(\s*.+?[^:#]): \s*(.*)' if aligned_colons else r'^(\s*.+?[^:#]: )\s*(.*)', lambda m:
8-
m.group(1) + ''.ljust(longest-len(m.group(1))+(-1-pad if aligned_colons else 1)) + (':'.ljust(pad+1) if aligned_colons else '') + m.group(2),
9-
str, re.MULTILINE)
10-
, str.split('\n'))])
6+
if aligned_colons:
7+
return ''.join([i+'\n' for i in map(lambda str:
8+
re.sub(r'^(\s*.+?[^:#]): \s*(.*)', lambda m:
9+
m.group(1) + ''.ljust(longest-len(m.group(1))-1-pad) + ':'.ljust(pad+1) + m.group(2),
10+
str, re.MULTILINE)
11+
, str.split('\n'))])
12+
else:
13+
return ''.join([i+'\n' for i in map(lambda str:
14+
re.sub(r'^(\s*.+?[^:#]: )\s*(.*)', lambda m:
15+
m.group(1) + ''.ljust(longest-len(m.group(1))+1) + m.group(2),
16+
str, re.MULTILINE)
17+
, str.split('\n'))])

0 commit comments

Comments
 (0)