diff options
| author | Scott Gasch <[email protected]> | 2021-07-12 20:52:49 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2021-07-12 20:52:49 -0700 |
| commit | 97fbe845e5dfdbda22521117c1783e1fd8515952 (patch) | |
| tree | 8b0ac61be1d670621abc994beef84e249f0ad95a /text_utils.py | |
| parent | a838c154135b2420d9047a101caf24a2c9f593c2 (diff) | |
Random changes.
Diffstat (limited to 'text_utils.py')
| -rw-r--r-- | text_utils.py | 27 |
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) |
