Python Forum
dynamically print based on terminal size
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamically print based on terminal size
#1
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?
Reply
#2
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.
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,124 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Print List to Terminal DaveG 2 1,453 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Using print command to send command to rasberry pi terminal MondazeBear 2 1,886 Aug-02-2021, 03:15 PM
Last Post: Larz60+
  Print the number of items in a list on ubuntu terminal buttercup 2 1,958 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  How to print out multiple drinks instead of just one based on the input? jayfre 4 4,237 Jul-01-2020, 06:53 PM
Last Post: menator01
  print result in OUTPUT not in TERMINAL picasso 1 2,080 Apr-15-2020, 03:51 PM
Last Post: deanhystad
  size of set vs size of dict zweb 0 2,161 Oct-11-2019, 01:32 AM
Last Post: zweb
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,504 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020