X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=text_utils.py;h=36cfe2fd720d9e0cb479992495a70d66161ae132;hb=eb9e6df32ed696158bf34dba6464277b648f5c74;hp=8ea6e196001e795daec223e166e01d9aed33a009;hpb=709370b2198e09f1dbe195fe8813602a3125b7f6;p=python_utils.git diff --git a/text_utils.py b/text_utils.py index 8ea6e19..36cfe2f 100644 --- a/text_utils.py +++ b/text_utils.py @@ -232,7 +232,7 @@ def wrap_string(text: str, n: int) -> str: return out -class Indenter: +class Indenter(object): """ with Indenter(pad_count = 8) as i: i.print('test') @@ -268,6 +268,28 @@ class Indenter: 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()