Skip to content

Instantly share code, notes, and snippets.

@USLTD
Last active January 18, 2022 04:11
Show Gist options
  • Save USLTD/9b64c971da5426f59a0c488267466cca to your computer and use it in GitHub Desktop.
Save USLTD/9b64c971da5426f59a0c488267466cca to your computer and use it in GitHub Desktop.
from wcwidth import wcswidth
import shutil
def print_centre(text: str):
width = shutil.get_terminal_size()[0]
length = wcswidth(text)
l = len(text)
d = length // l
space = width - length
if space >= 0:
pads = " " * (space // 2)
print(f"{pads}{text}{pads}")
else:
print(text[:(width//d)])
left = text[(width//d):]
print_centre(left)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment