From 355fad2b3a63bec30e43e8034062027aba3b9799 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 28 Jul 2022 01:47:29 -0700 Subject: [PATCH] add handy re-indenting script --- reindent.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 reindent.py diff --git a/reindent.py b/reindent.py new file mode 100755 index 00000000..85a426ad --- /dev/null +++ b/reindent.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +""" handy script for re-indenting text to be in multiples of ` `""" +import sys + +indent_unit = " " + + +def ind_print(v): + print(indent_unit * (len(indent_stack) - 1) + v) + + +with open(sys.argv[1]) as f: + indent_stack = [""] + for line in f: + line = line.rstrip().expandtabs() + indent = line[:-len(line.lstrip())] + unindented = line[len(indent):] + if unindented == '': + print() + continue + while len(indent_stack[-1]) > len(indent): + indent_stack.pop() + assert len(indent_stack[-1]) >= len(indent), \ + "popped intermediate indentation" + if len(indent_stack[-1]) < len(indent): + indent_stack.append(indent) + ind_print(unindented) -- 2.30.2