Add niceprints.py.

master
voussoir 2021-08-11 01:12:43 -07:00
parent 72fdcb2b04
commit 1d6f9eee10
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 74 additions and 0 deletions

74
voussoirkit/niceprints.py Normal file
View File

@ -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}'