получить размер терминала в питоне

May 22, 2010 11:21

Copy Source | Copy HTML
  1. # a bit modified code from  here
  2. import os
  3.  
  4. def getTerminalSize():
  5.     def ioctl_GWINSZ(fd):
  6.         try:
  7.             import fcntl, termios, struct
  8.             cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
  9.         except:
  10.             return None
  11.         return cr
  12.     cr = ioctl_GWINSZ( 0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
  13.     if cr is None :
  14.         try:
  15.             fd = os.open(os.ctermid(), os.O_RDONLY)
  16.             cr = ioctl_GWINSZ(fd)
  17.             os.close(fd)
  18.         except:
  19.             pass
  20.     if cr is None :
  21.         try:
  22.             cr = (os.environ['LINES'], os.environ['COLUMNS'])
  23.         except:
  24.             cr = (25, 80)
  25.     return int(cr[1]), int(cr[ 0])
  26.  

python, bookmark, code

Previous post Next post
Up