+
+
+def wrap_string(text: str, n: int) -> str:
+ """
+ Args:
+ text: the string to be wrapped
+ n: the width after which to wrap text
+
+ Returns:
+ The wrapped form of text
+ """
+ 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):
+ """
+ Context manager that indents stuff (even recursively). e.g.::
+
+ with Indenter(pad_count = 8) as i:
+ i.print('test')
+ with i:
+ i.print('-ing')
+ with i:
+ i.print('1, 2, 3')
+
+ Yields::
+
+ test
+ -ing
+ 1, 2, 3
+ """
+
+ def __init__(
+ self,
+ *,
+ pad_prefix: Optional[str] = None,
+ pad_char: str = ' ',
+ pad_count: int = 4,
+ ):
+ """Construct an Indenter.
+
+ Args:
+ pad_prefix: an optional prefix to prepend to each line
+ pad_char: the character used to indent
+ pad_count: the number of pad_chars to use to indent
+ """
+ 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: Optional[int] = None,
+ align: Optional[str] = None,
+ style: Optional[str] = 'solid',
+ color: Optional[str] = None,
+):
+ """
+ Creates a nice header line with a title.
+
+ Args:
+ title: the title
+ width: how wide to make the header
+ align: "left" or "right"
+ style: "ascii", "solid" or "dashed"
+
+ Returns:
+ The header as a string.
+
+ >>> header('title', width=60, style='ascii')
+ '----[ title ]-----------------------------------------------'
+ """
+ if not width:
+ try:
+ width = get_console_rows_columns().columns
+ except Exception:
+ width = 80
+ if not align:
+ align = 'left'
+ if not style:
+ style = 'ascii'
+
+ text_len = len(string_utils.strip_ansi_sequences(title))
+ if align == 'left':
+ left = 4
+ right = width - (left + text_len + 4)
+ elif align == 'right':
+ right = 4
+ left = width - (right + text_len + 4)
+ else:
+ left = int((width - (text_len + 4)) / 2)
+ right = left
+ while left + text_len + 4 + right < width:
+ right += 1
+
+ if style == 'solid':
+ line_char = '━'
+ begin = ''
+ end = ''
+ elif style == 'dashed':
+ line_char = '┅'
+ begin = ''
+ end = ''
+ else:
+ line_char = '-'
+ begin = '['
+ end = ']'
+ if color:
+ col = color
+ reset_seq = reset()
+ else:
+ col = ''
+ reset_seq = ''
+ return line_char * left + begin + col + ' ' + title + ' ' + reset_seq + end + line_char * right
+
+
+def box(
+ title: Optional[str] = None, text: Optional[str] = None, *, width: int = 80, color: str = ''
+) -> str:
+ """
+ Make a nice unicode box (optionally with color) around some text.
+
+ Args:
+ title: the title of the box
+ text: the text in the box
+ width: the box's width
+ color: the box's color
+
+ Returns:
+ the box as a string
+
+ >>> print(box('title', 'this is some text', width=20).strip())
+ ╭──────────────────╮
+ │ title │
+ │ │
+ │ this is some │
+ │ text │
+ ╰──────────────────╯
+ """
+ assert width > 4
+ if text is not None:
+ text = justify_text(text, width=width - 4, alignment='l')
+ return preformatted_box(title, text, width=width, color=color)
+
+
+def preformatted_box(
+ title: Optional[str] = None, text: Optional[str] = None, *, width=80, color: str = ''
+) -> str:
+ """Creates a nice box with rounded corners and returns it as a string.
+
+ Args:
+ title: the title of the box
+ text: the text inside the box
+ width: the width of the box
+ color: the box's color
+
+ Returns:
+ the box as a string
+
+ >>> print(preformatted_box('title', 'this\\nis\\nsome\\ntext', width=20).strip())
+ ╭──────────────────╮
+ │ title │
+ │ │
+ │ this │
+ │ is │
+ │ some │
+ │ text │
+ ╰──────────────────╯
+ """
+ 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 text.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(preformatted_box(title, text, width=width, color=color), end='')
+
+
+if __name__ == '__main__':
+ import doctest
+
+ doctest.testmod()