Separate box and print_box.
authorScott Gasch <[email protected]>
Wed, 23 Feb 2022 19:09:57 +0000 (11:09 -0800)
committerScott Gasch <[email protected]>
Wed, 23 Feb 2022 19:09:57 +0000 (11:09 -0800)
text_utils.py

index 39b694c2721b1c92c73036554a50a6a2194bbc21..55b057569b531f211e0073c0dc931ae4fd239bd4 100644 (file)
@@ -221,6 +221,7 @@ def justify_text(text: str, *, width: int = 80, alignment: str = "c") -> str:
     >>> justify_text('This is a test of the emergency broadcast system.  This is only a test.',
     ...              width=40, alignment='j')  #doctest: +NORMALIZE_WHITESPACE
     'This  is    a  test  of   the  emergency\\nbroadcast system. This is only a test.'
     >>> justify_text('This is a test of the emergency broadcast system.  This is only a test.',
     ...              width=40, alignment='j')  #doctest: +NORMALIZE_WHITESPACE
     'This  is    a  test  of   the  emergency\\nbroadcast system. This is only a test.'
+
     """
     retval = ""
     line = ""
     """
     retval = ""
     line = ""
@@ -272,6 +273,7 @@ class Indenter(contextlib.AbstractContextManager):
             i.print('-ing')
             with i:
                 i.print('1, 2, 3')
             i.print('-ing')
             with i:
                 i.print('1, 2, 3')
+
     """
 
     def __init__(
     """
 
     def __init__(
@@ -329,35 +331,55 @@ def header(title: str, *, width: int = 80, color: str = ''):
 
 def box(
     title: Optional[str] = None, text: Optional[str] = None, *, width: int = 80, color: str = ''
 
 def box(
     title: Optional[str] = None, text: Optional[str] = None, *, width: int = 80, color: str = ''
-):
-    """Draws a box with nice rounded corners.
-
-    >>> box('Title', 'This is text', width=30)
-    ╭────────────────────────────╮
-    │            Title           │
-    │                            │
-    │ This is text               │
-    ╰────────────────────────────╯
-
-    """
+) -> str:
     assert width > 4
     assert width > 4
+    ret = ''
     if color == '':
         rset = ''
     else:
         rset = reset()
     w = width - 2
     if color == '':
         rset = ''
     else:
         rset = reset()
     w = width - 2
-    print(color + '╭' + '─' * w + '╮' + rset)
+    ret += color + '╭' + '─' * w + '╮' + rset + '\n'
     if title is not None:
     if title is not None:
-        print(
-            color + '│' + rset + justify_string(title, width=w, alignment='c') + color + '│' + rset
+        ret += (
+            color
+            + '│'
+            + rset
+            + justify_string(title, width=w, alignment='c')
+            + color
+            + '│'
+            + rset
+            + '\n'
         )
         )
-        print(color + '│' + ' ' * w + '│' + rset)
+        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(line)
             assert tw < w
     if text is not None:
         for line in justify_text(text, width=w - 2, alignment='l').split('\n'):
             tw = len(line)
             assert tw < w
-            print(color + '│ ' + rset + line + ' ' * (w - tw - 2) + color + ' │' + rset)
-    print(color + '╰' + '─' * w + '╯' + rset)
+            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__':
 
 
 if __name__ == '__main__':