Created
October 14, 2017 21:22
-
-
Save iacchus/4ac028e0a926e46552419e0958e4f020 to your computer and use it in GitHub Desktop.
Center fortunes in terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys, os | |
linelist = list(sys.stdin) | |
# gets the biggest line | |
biggest_line_size = 0 | |
for line in linelist: | |
line_lenght = len(line.expandtabs()) | |
if line_lenght > biggest_line_size: | |
biggest_line_size = line_lenght | |
columns = int(os.popen('tput cols', 'r').read()) | |
offset = biggest_line_size / 2 | |
perfect_center = columns / 2 | |
padsize = int(perfect_center - offset) | |
spacing = ' ' * padsize # space char | |
text = str() | |
for line in linelist: | |
text += (spacing + line) | |
divider = spacing + ('─' * int(biggest_line_size)) # unicode 0x2500 | |
text += divider | |
print(text, end="\n"*2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment