def get_terminal_size():
<EOL>if sys.version_info >= (<NUM_LIT:3>, <NUM_LIT:3>):<EOL><INDENT>import shutil<EOL>shutil_get_terminal_size = getattr(shutil, '<STR_LIT>', None)<EOL>if shutil_get_terminal_size:<EOL><INDENT>sz = shutil_get_terminal_size()<EOL>return sz.columns, sz.lines<EOL><DEDENT><DEDENT>if get_winterm_size is not None:<EOL><INDENT>return get_winterm_size()<EOL><DEDENT>def ioctl_gwinsz(fd):<EOL><INDENT>try:<EOL><INDENT>import fcntl<EOL>import termios<EOL>cr = struct.unpack(<EOL>'<STR_LIT>', fcntl.ioctl(fd, termios.TIOCGWINSZ, '<STR_LIT>'))<EOL><DEDENT>except Exception:<EOL><INDENT>return<EOL><DEDENT>return cr<EOL><DEDENT>cr = ioctl_gwinsz(<NUM_LIT:0>) or ioctl_gwinsz(<NUM_LIT:1>) or ioctl_gwinsz(<NUM_LIT:2>)<EOL>if not cr:<EOL><INDENT>try:<EOL><INDENT>fd = os.open(os.ctermid(), os.O_RDONLY)<EOL>try:<EOL><INDENT>cr = ioctl_gwinsz(fd)<EOL><DEDENT>finally:<EOL><INDENT>os.close(fd)<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if not cr or not cr[<NUM_LIT:0>] or not cr[<NUM_LIT:1>]:<EOL><INDENT>cr = (os.environ.get('<STR_LIT>', <NUM_LIT>),<EOL>os.environ.get('<STR_LIT>', DEFAULT_COLUMNS))<EOL><DEDENT>return int(cr[<NUM_LIT:1>]), int(cr[<NUM_LIT:0>])<EOL>
Returns the current size of the terminal as tuple in the form
    ``(width, height)`` in columns and rows.