Skip to content

Instantly share code, notes, and snippets.

@daknuett
Last active March 11, 2020 14:53
Show Gist options
  • Save daknuett/44e768128ad41b22542f57f36f55cd93 to your computer and use it in GitHub Desktop.
Save daknuett/44e768128ad41b22542f57f36f55cd93 to your computer and use it in GitHub Desktop.
Print stuff with hearth
import shutil, math, sys
"""
Share love on your computer.
Usage:
::
python3 hearth.py {line}
This print all lines in italic yellow
between two bold red hearths
Note: Change ``italic = ''`` if your terminal emulator messes it up.
Example:
::
python3 hearth.py "Dear Sandra," "happy Valentines Day!" "See you soon."
Will print the following (+ ascii escapes):
::
/#\ /#\ /#\ /#\
/ \ / \ / \ / \
# # # Dear Sandra, # # #
\ / happy Valentines Day! \ /
\ / See you soon. \ /
\ / \ /
\ / \ /
\#/ \#/
"""
#
# Copyright(c) Daniel Knüttel 2017
#
# This program is free software.
# Anyways if you think this program is worth it
# and we meet shout a drink for me.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
# der GNU Affero General Public License, wie von der Free Software Foundation,
# Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
# veröffentlichten Version, weiterverbreiten und/oder modifizieren.
#
# Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
# OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
# Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
# Siehe die GNU Affero General Public License für weitere Details.
#
# Sie sollten eine Kopie der GNU Affero General Public License zusammen mit diesem
# Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
red = '\033[91m'
end = '\033[0m'
bold = '\033[1m'
italic = '\033[3m'
yellow = '\033[93m'
hearth = [\
" /#\ /#\ ",
" / \ / \ ",
" # # # ",
" \ / ",
" \ / ",
" \ / ",
" \ / ",
" \#/ "]
def msgs_to_block(space, *msgs):
"""
Cut too long lines into several lines.
"""
msgs = list(msgs)
msgs = [""] * ((len(hearth) - len(msgs)) // 2) + msgs + [""] * ((len(hearth) - len(msgs)) // 2)
if(len(msgs) < len(hearth)):
msgs += [" " * space]
for msg in msgs:
while(len(msg) > space):
yield msg[:space]
msg = msg[space:]
yield center_msg(msg, space)
def center_msg(msg, space):
"""
Centering.
"""
if(msg.isspace()):
return msg
msg = msg.replace("\t", " ")
msg = (" " * ((space - len(msg)) // 2)) + msg + (" " * ((space - len(msg)) // 2))
if(len(msg) < space):
msg += " "
return msg
def print_with_hearth(*msgs, hearth=hearth):
"""
Print the given messages *msgs with the given hearth.
"""
if(len(msgs) > len(hearth)):
hearth += [" " * len(hearth[0])] * (len( msgs) - len(hearth))
print()
for row1, msg, row2 in zip(hearth, msgs, hearth):
print(red + bold + row1 + end + italic + yellow + msg + end + red + bold + row2)
print(end)
if(__name__ == "__main__"):
width, height = shutil.get_terminal_size()
if(len(sys.argv) == 1):
msgs = []
else:
msgs = sys.argv[1:]
left_space = width - (2 * len(hearth[0]))
print_with_hearth(*msgs_to_block(left_space, *msgs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment