Hello. I'm trying to center some text in CMD but it's not completely in the middle.
from shutil import get_terminal_size
from termcolor import colored
columns = get_terminal_size().columns
title = [
' ____ _ _ ',
'__ _|___ \ | |__ ___ | |_ ',
"\ \ / / __) | | '_ \ / _ \| __|",
' \ V / / __/ | |_) | (_) | |_ ',
' \_/ |_____| |_.__/ \___/ \__|'
]
for i in title:
print(colored(i, 'red').center(columns))
I tried to add some numbers to columns
like columns = get_terminal_size().columns 50
but then it strangely adds space between the lines like following:
Still not in the middle..
CodePudding user response:
Why don't you try using the rich
Python library? Using rich
, you can do all sorts of things with UI!
Install rich
using: python -m pip install rich
Now, you can use rich.align.Align
to align your text (wow, that was a mouthful)!
from rich.console import Console
from rich.text import Text
from rich.align import Align
rich_console = Console()
title = [
' ____ _ _ ',
'__ _|___ \ | |__ ___ | |_ ',
"\ \ / / __) | | '_ \ / _ \| __|",
' \ V / / __/ | |_) | (_) | |_ ',
' \_/ |_____| |_.__/ \___/ \__|'
]
# So first we are converting your list into a string separated by newlines
# Then we align to the center
# And then we turn your text red!
console.print(Align("\n".join(title), align="center", style="red"))
CodePudding user response:
Solution found, I tried:
columns = get_terminal_size().columns 10
I've tested 9 now, it works. It's a solution, but not a good one because it's not dynamic enough. Still welcome if anyone has a better idea.