+
+
+def wrap_string(text: str, n: int) -> str:
+ chunks = text.split()
+ out = ''
+ width = 0
+ for chunk in chunks:
+ if width + len(chunk) > n:
+ out += '\n'
+ width = 0
+ out += chunk + ' '
+ width += len(chunk) + 1
+ return out
+
+
+class Indenter(object):
+ """
+ with Indenter(pad_count = 8) as i:
+ i.print('test')
+ with i:
+ i.print('-ing')
+ with i:
+ i.print('1, 2, 3')
+ """
+ def __init__(self,
+ *,
+ pad_prefix: Optional[str] = None,
+ pad_char: str = ' ',
+ pad_count: int = 4):
+ self.level = -1
+ if pad_prefix is not None:
+ self.pad_prefix = pad_prefix
+ else:
+ self.pad_prefix = ''
+ self.padding = pad_char * pad_count
+
+ 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.pad_prefix + self.padding * self.level + text, end='')
+
+
+def header(title: str, *, width: int = 80, color: str = ''):
+ """
+ Returns a nice header line with a title.
+
+ >>> header('title', width=60, color='')
+ '----[ title ]-----------------------------------------------'
+
+ """
+ w = width
+ w -= (len(title) + 4)
+ if w >= 4:
+ left = 4 * '-'
+ right = (w - 4) * '-'
+ if color != '' and color is not None:
+ r = reset()
+ else:
+ r = ''
+ return f'{left}[ {color}{title}{r} ]{right}'
+ else:
+ return ''
+
+
+if __name__ == '__main__':
+ import doctest
+ doctest.testmod()