We saw the Aligning text recipe in Chapter 2, Text Management, which showcased a possible solution to align text within a fixed space. The amount of space available was defined in a COLSIZE
constant that was chosen to fit most terminals with three columns (most terminals fit 80 columns).
But what happened if the user had a terminal window smaller than 60 columns? Our alignment would have been broken badly. Also, on very big windows, while the text wouldn't be broken, it would have looked too small compared to the window.
For this reason, it's usually better to also take into consideration the size of the user terminal window whenever displaying text that should retain proper alignment properties.
The steps are as follows:
- The
shutil.get_terminal_size
function can give guidance on the terminal window size and provide a fallback for cases where it's not available. We will adapt themaketable
function from the Aligning text recipe of Chapter 2, Text Management...