Skip to content

Instantly share code, notes, and snippets.

@iacchus
Created October 14, 2017 21:22
Show Gist options
  • Save iacchus/4ac028e0a926e46552419e0958e4f020 to your computer and use it in GitHub Desktop.
Save iacchus/4ac028e0a926e46552419e0958e4f020 to your computer and use it in GitHub Desktop.
Center fortunes in terminal
#!/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