Skip to content

Instantly share code, notes, and snippets.

@ajeddeloh
Last active July 20, 2016 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajeddeloh/023f0646d4c1570c4f2bf99bd62e27cd to your computer and use it in GitHub Desktop.
Save ajeddeloh/023f0646d4c1570c4f2bf99bd62e27cd to your computer and use it in GitHub Desktop.
jq for go structs
#!/usr/bin/python3
import sys
indent = 0
# characters to increase the indent
indentchars = '{['
# characters to decrease the indent
unindentchars = '}]'
# characters to cause a line break
nlchars = ' \n'
# where to have indentchars on the same line
sameline = True
while True:
c = sys.stdin.read(1)
if c == '':
break
if c in indentchars:
if not sameline:
print('\n', ' ' * indent, sep='', end='')
print(c)
indent += 2
print(' ' * indent, end='')
elif c in unindentchars:
print('')
indent -= 2
print(' ' * indent, end='')
print(c, end='')
elif c in nlchars:
print('')
print(' ' * indent, end='')
else:
print(c, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment