Python Forum

Full Version: dynamically print based on terminal size
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to print something like this:
help().............................................Display this help message.
 to the terminal window.
How can I make it adjust the amount of periods to fit the terminal window dynamically?
It's fairly safe to assume the terminal is 79 characters wide (that's where the python line length suggestion comes from), so you can pad using 79 as the benchmark.  If that's not good enough, then you could use shutil to get the actual width of the terminal.

>>> width = 79
>>> text_left = "help()"
>>> text_right = "Display this help message."
>>> remaining_space = width - len(text_left) - len(text_right)
>>> remaining_space
47
>>> print("{0}{1}{2}".format(text_left, ("."*remaining_space), text_right))
help()...............................................Display this help message.


>>> import shutil
>>> width = shutil.get_terminal_size().columns
>>> remaining_space = width - len(text_left) - len(text_right)
>>> width
120
>>> remaining_space
88
>>> print("{0}{1}{2}".format(text_left, ("."*remaining_space), text_right))
help()........................................................................................Display this help message.
>>>