"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/ui/ascii/terminal_size.py" (11 Mar 2007, 1115 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # from http://pdos.csail.mit.edu/~cblake/cls/cls.py
    2 
    3 import os
    4 
    5 def _ioctl_GWINSZ(fd):                  #### TABULATION FUNCTIONS
    6     try:                                ### Discover terminal width
    7         import fcntl
    8         import termios
    9         import struct
   10         cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
   11     except:
   12         return
   13     return cr
   14 
   15 def terminal_size():                    ### decide on *some* terminal size
   16     """Return (lines, columns)."""
   17     cr = _ioctl_GWINSZ(0) or _ioctl_GWINSZ(1) or _ioctl_GWINSZ(2) # try open fds
   18     if not cr:                                                  # ...then ctty
   19         try:
   20             fd = os.open(os.ctermid(), os.O_RDONLY)
   21             cr = _ioctl_GWINSZ(fd)
   22             os.close(fd)
   23         except:
   24             pass
   25     if not cr:                            # env vars or finally defaults
   26         try:
   27             cr = os.environ['LINES'], os.environ['COLUMNS']
   28         except:
   29             cr = 25, 80
   30     return int(cr[1]), int(cr[0])         # reverse rows, cols
   31 
   32 if __name__ == '__main__':
   33     print terminal_size()