From 97fbe845e5dfdbda22521117c1783e1fd8515952 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Mon, 12 Jul 2021 20:52:49 -0700 Subject: Random changes. --- text_utils.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'text_utils.py') 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) -- cgit v1.3