summaryrefslogtreecommitdiff
path: root/text_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'text_utils.py')
-rw-r--r--text_utils.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/text_utils.py b/text_utils.py
index 76b5db6..93e4b63 100644
--- a/text_utils.py
+++ b/text_utils.py
@@ -167,3 +167,30 @@ def generate_padded_columns(text: List[str]) -> str:
word = justify_string(word, width=width, alignment='l')
out += f'{word} '
yield out
+
+
+class Indenter:
+ """
+ with Indenter() as i:
+ i.print('test')
+ with i:
+ i.print('-ing')
+ with i:
+ i.print('1, 2, 3')
+ """
+ def __init__(self):
+ self.level = -1
+
+ def __enter__(self):
+ self.level += 1
+ return self
+
+ def __exit__(self, exc_type, exc_value, exc_tb):
+ self.level -= 1
+ if self.level < -1:
+ self.level = -1
+
+ def print(self, *arg, **kwargs):
+ import string_utils
+ text = string_utils.sprintf(*arg, **kwargs)
+ print(" " * self.level + text)