def terminal_size():
    """
    Detect the current size of terminal window as a numer of rows and columns.
    """
    try:
        (rows, columns) = os.popen('stty size', 'r').read().split()
        rows    = int(rows)
        columns = int(columns)
        return (columns, rows)

    # Currently ignore any errors and return some reasonable default values.
    # Errors may occur, when the library is used in non-terminal application
    # like daemon.
    except:
        pass
    return (80, 24)
Detect the current size of terminal window as a numer of rows and columns.