+
+
+def wrap_string(text: str, n: int) -> str:
+ chunks = text.split()
+ out = ''
+ width = 0
+ for chunk in chunks:
+ if width + len(string_utils.strip_ansi_sequences(chunk)) > n:
+ out += '\n'
+ width = 0
+ out += chunk + ' '
+ width += len(string_utils.strip_ansi_sequences(chunk)) + 1
+ return out
+
+
+class Indenter(contextlib.AbstractContextManager):
+ """
+ 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) -> Literal[False]:
+ self.level -= 1
+ if self.level < -1:
+ self.level = -1
+ return False
+
+ def print(self, *arg, **kwargs):
+ 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(string_utils.strip_ansi_sequences(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 ''
+
+
+def box(
+ title: Optional[str] = None, text: Optional[str] = None, *, width: int = 80, color: str = ''
+) -> str:
+ assert width > 4
+ ret = ''
+ if color == '':
+ rset = ''
+ else:
+ rset = reset()
+ w = width - 2
+ ret += color + '╭' + '─' * w + '╮' + rset + '\n'
+ if title is not None:
+ ret += (
+ color
+ + '│'
+ + rset
+ + justify_string(title, width=w, alignment='c')
+ + color
+ + '│'
+ + rset
+ + '\n'
+ )
+ ret += color + '│' + ' ' * w + '│' + rset + '\n'
+ if text is not None:
+ for line in justify_text(text, width=w - 2, alignment='l').split('\n'):
+ tw = len(string_utils.strip_ansi_sequences(line))
+ assert tw < w
+ ret += color + '│ ' + rset + line + ' ' * (w - tw - 2) + color + ' │' + rset + '\n'
+ ret += color + '╰' + '─' * w + '╯' + rset + '\n'
+ return ret
+
+
+def print_box(
+ title: Optional[str] = None, text: Optional[str] = None, *, width: int = 80, color: str = ''
+) -> None:
+ """Draws a box with nice rounded corners.
+
+ >>> print_box('Title', 'This is text', width=30)
+ ╭────────────────────────────╮
+ │ Title │
+ │ │
+ │ This is text │
+ ╰────────────────────────────╯
+
+ >>> print_box(None, 'OK', width=6)
+ ╭────╮
+ │ OK │
+ ╰────╯
+
+ """
+ print(box(title, text, width=width, color=color), end='')
+
+
+if __name__ == '__main__':
+ import doctest
+
+ doctest.testmod()