#NEWLINE# def get_terminal_size( ) : #NEWLINE# #INDENT# 'Returns a tuple (x, y) representing the width(x) and the height(x)\n    in characters of the terminal window.' #NEWLINE# def ioctl_GWINSZ( fd ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# import fcntl #NEWLINE# import termios #NEWLINE# import struct #NEWLINE# cr = struct . unpack ( 'hh' , fcntl . ioctl ( fd , termios . TIOCGWINSZ , '1234' ) ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if ( cr == ( 0 , 0 ) ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if ( cr == ( 0 , 0 ) ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# return cr #NEWLINE# #UNINDENT# cr = ( ioctl_GWINSZ ( 0 ) or ioctl_GWINSZ ( 1 ) or ioctl_GWINSZ ( 2 ) ) #NEWLINE# if ( not cr ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# fd = os . open ( os . ctermid ( ) , os . O_RDONLY ) #NEWLINE# cr = ioctl_GWINSZ ( fd ) #NEWLINE# os . close ( fd ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# if ( not cr ) : #NEWLINE# #INDENT# cr = ( os . environ . get ( 'LINES' , 25 ) , os . environ . get ( 'COLUMNS' , 80 ) ) #NEWLINE# #UNINDENT# return ( int ( cr [ 1 ] ) , int ( cr [ 0 ] ) )