How many characters per line on the console?

How can I find out how a character can be present in a line before the end line in an interactive shell using python? (Usually 80)

+3
source share
4 answers

You can use the utility tputto query the number of rows and columns available in the terminal. You can execute it with subprocess.Popen:

>>> import subprocess
>>> tput = subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE)
>>> int(tput.communicate()[0].strip())
180

The same principle can also be applied to query a variable $COLUMNSas mentioned by gregseth:

>>> tput = subprocess.Popen(['echo $COLUMNS'], shell=True, stdout=subprocess.PIPE)
>>> int(tput.communicate()[0].strip())
180

, curses , -, , , :

>>> import curses
>>> curses.setupterm()
>>> curses.tigetnum('cols')
180

, setupterm , , tigetnum.

+2

python,
$COLUMNS .

+3

only * nix

>>> import sys,struct,fnctl,termios
>>> fd = sys.stdin.fileno()
>>> s = struct.pack("HH", 0,0)
>>> size=fcntl.ioctl(fd, termios.TIOCGWINSZ,s)
>>> struct.unpack("HH", size)[-1]
157
0
source

Source: https://habr.com/ru/post/1729293/