Created
February 8, 2018 12:38
-
-
Save taikedz/eceb8edad642cc0d25ffeb1150a6d7e2 to your computer and use it in GitHub Desktop.
Fold text blocks at nearest character before hardwrap limit. Created because I wanted to wrap mysql dumps (that "anonymous" one was mine)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import sys | |
import re | |
DEBUG_ON = False | |
TRUNCATE = True | |
def printhelp(): | |
print(""" | |
blockfolder.py CHAR COLS | |
Split a multi-line block of string TEXT as one conceptual "line" at nearest occurrenc of CHAR before string column COLS, and print them | |
Say a variable mytext contains: | |
+-------+--------+-------+ | |
| first | second | third | | |
+-------+--------+-------+" | |
This could be re-printed via: | |
blockfolder.py '+' 20 | |
+-------+-------- | |
| first | second | |
+-------+-------- | |
+-------+ | |
| third | | |
+-------+ | |
If the block section is wider than the hardwrap width, then the section is forcibly truncated at the max width, | |
unless TRUNCATE is set to False in the script, in which case the full section is provided, non-truncated | |
""") | |
def find_nearest(char, text, maxl=0): | |
if len(text) < maxl: | |
return len(text) | |
# Find after initial char | |
# else we necessarily have an infinite loop | |
i = text.find(char, 1) | |
if i < 0: | |
raise IndexError("Could not find '%s' in [%s] "%(char,text) ) | |
if i > maxl: | |
if TRUNCATE: | |
return maxl | |
else: | |
return i | |
x = 0 | |
while text.find(char, i+1) > -1 and i < maxl: | |
j = text.find(char, i+1) | |
if j == 0: | |
debuge("Inifinite loop (j)") | |
exit(1) | |
if j < maxl: | |
i = j | |
else: | |
break | |
x += 1 | |
if i == 0: | |
debuge("Inifinite loop (i)") | |
exit(1) | |
return i | |
def debuge(text, insert=""): | |
if DEBUG_ON: | |
print("\033[33;1m%s\033[0m %s"%(text, str(insert)) ) | |
def fold_block(char, text, maxl=64): | |
lines = text.split(os.linesep) | |
sections = [] | |
while len(lines[0]) > 0: | |
sections.append([]) | |
section_id = len(sections)-1 | |
debuge("Section", section_id) | |
idx = find_nearest(char, lines[0], maxl) | |
debuge("Cut index:",idx) | |
for i in range(len(lines)): | |
current_line = lines[i] | |
before, after = splitline(current_line,idx) | |
debuge("Line:",current_line) | |
sections[section_id].append(before) | |
lines[i] = after | |
sections[section_id] = os.linesep.join(sections[section_id] ) | |
return sections | |
def splitline(text, idx): | |
if len(text) < idx: | |
return text,"" | |
return text[:idx],text[idx:] | |
def main(args): | |
if "--help" in args: | |
printhelp() | |
exit(0) | |
inlines = [] | |
while True: | |
gotline = sys.stdin.readline() | |
if gotline == os.linesep: | |
break | |
inlines.append(gotline) | |
maxl = 64 | |
if len(args) > 2: | |
maxl = int(args[2]) | |
section_blocks = fold_block(args[1] , "".join(inlines) , maxl) | |
pretext = "" | |
for block in section_blocks: | |
print(re.sub("^",pretext,block, flags=re.M) ) | |
pretext = " " | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment