From 1d6f9eee10604f02d1bbc6559904cbaf4c46c9c0 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 11 Aug 2021 01:12:43 -0700 Subject: [PATCH] Add niceprints.py. --- voussoirkit/niceprints.py | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 voussoirkit/niceprints.py diff --git a/voussoirkit/niceprints.py b/voussoirkit/niceprints.py new file mode 100644 index 0000000..fd069b1 --- /dev/null +++ b/voussoirkit/niceprints.py @@ -0,0 +1,74 @@ +''' +niceprints +========== + +This module provides functions which add visual flair to your text, to make +your print statements more interesting. + +These functions only do the minimum amount of transformation for their effect. +You should do your uppercase/lowercase, text wrap, etc. before calling +these functions. +''' +import shutil + +from voussoirkit import dotdict + +SINGLE_BOX = dotdict.DotDict( + upper_left='┌', + upper_right='┐', + top='─', + lower_left='└', + lower_right='┘', + side='│', +) + +DOUBLE_BOX = dotdict.DotDict( + upper_left='╔', + upper_right='╗', + top='═', + lower_left='╚', + lower_right='╝', + side='║', +) + +def equals_header(text): + ''' + Sample text + =========== + ''' + return text + '\n' + ('=' * len(text)) + +def in_box(text, *, boxchars=SINGLE_BOX, title=''): + ''' + ┌───────────┐ + │Sample text│ + └───────────┘ + ╔═══════════╗ + ║Sample text║ + ╚═══════════╝ + ┌Sample Title────────────────────────────────┐ + │There is breaking news about an urgent topic│ + │and you'll never guess what it is │ + └────────────────────────────────────────────┘ + ''' + lines = text.splitlines() + longest_line = max(max(len(line) for line in lines), len(title)) + top = title + boxchars.top * (longest_line - len(title)) + bottom = boxchars.top * longest_line + + new_lines = [] + new_lines.append(boxchars.upper_left + top + boxchars.upper_right) + for line in lines: + new_lines.append(boxchars.side + line.ljust(longest_line, ' ') + boxchars.side) + new_lines.append(boxchars.lower_left + bottom + boxchars.lower_right) + return '\n'.join(new_lines) + +def solid_hash_header(text): + ''' + # Sample text ############################################################## + ''' + cli_width = shutil.get_terminal_size()[0] + # One left hash, space, and space after text. + right_count = cli_width - (len(text) + 3) + right_hashes = '#' * right_count + return f'# {text} {right_hashes}'