def get_terminal_width():
 try:
  import termios
  import fcntl
  import struct
  call = fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('hhhh', 0, 0, 0, 0))
  (height, width) = struct.unpack('hhhh', call)[:2]
  terminal_width = width
 except Exception:
  terminal_width = (int(os.environ.get('COLUMNS', '80')) - 1)
 return terminal_width

def get_terminal_width ( ) : try : import termios import fcntl import struct call = fcntl . ioctl ( 0 , termios . TIOCGWINSZ , struct . pack ( 'hhhh' , 0 , 0 , 0 , 0 ) ) ( height , width ) = struct . unpack ( 'hhhh' , call ) [ : 2 ] terminal_width = width except Exception : terminal_width = ( int ( os . environ . get ( 'COLUMNS' , '80' ) ) - 1 ) return terminal_width
borrowed from the py lib .