Skip to content

Instantly share code, notes, and snippets.

@duckinator
Created February 1, 2020 05:31
Show Gist options
  • Save duckinator/ca034878ab83322bd9b3da1422ead4f8 to your computer and use it in GitHub Desktop.
Save duckinator/ca034878ab83322bd9b3da1422ead4f8 to your computer and use it in GitHub Desktop.
~/test$ printf " a\n b\n c\n"
a
b
c
~/test$ printf " a\n b\n c\n" | python3 -m textwrap-tool dedent
a
b
c
~/test$ printf " a\n b\n c\n" | python3 -m textwrap-tool indent '....'
.... a
.... b
.... c
~/test$ printf "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | python3 -m textwrap-tool wrap 10
1234567890
abcdefghij
klmnopqrst
uvwxyzABCD
EFGHIJKLMN
OPQRSTUVWX
YZ
~/test$
import sys
from textwrap import wrap, fill, shorten, dedent, indent
args = sys.argv[2:]
if len(args) > 0 and sys.argv[1] in ['wrap', 'fill', 'shorten']:
args[0] = int(args[0])
fns = {'wrap': wrap, 'fill': fill, 'shorten': shorten, 'dedent': dedent, 'indent': indent}
ret = fns[sys.argv[1]](sys.stdin.read(), *args)
if isinstance(ret, list):
# Handle things like wrap(), which returns a list.
print('\n'.join(ret))
else:
print(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment