def get_terminal_size(fallback=(<NUM_LIT>, <NUM_LIT>)):
for stream in [sys.__stdout__, sys.__stderr__, sys.__stdin__]:<EOL><INDENT>try:<EOL><INDENT>data = fcntl.ioctl(stream.fileno(), TIOCGWINSZ, b"<STR_LIT>")<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>lines, columns = struct.unpack("<STR_LIT>", data)<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>columns, lines = fallback<EOL><DEDENT>return columns, lines<EOL>
Return tuple containing columns and rows of controlling terminal, trying harder
than shutil.get_terminal_size to find a tty before returning fallback.

Theoretically, stdout, stderr, and stdin could all be different ttys that could
cause us to get the wrong measurements (instead of using the fallback) but the much more
common case is that IO is piped.