Get the size of the terminal window. For each of the two dimensions, the environment variable, COLUMNS and LINES respectively, is checked. If the variable is defined and the value is a positive integer, it is used. When COLUMNS or LINES is not defined, which is the common case,
(fallback=(80, 24))
| 1512 | follow_symlinks=follow_symlinks) |
| 1513 | |
| 1514 | def get_terminal_size(fallback=(80, 24)): |
| 1515 | """Get the size of the terminal window. |
| 1516 | |
| 1517 | For each of the two dimensions, the environment variable, COLUMNS |
| 1518 | and LINES respectively, is checked. If the variable is defined and |
| 1519 | the value is a positive integer, it is used. |
| 1520 | |
| 1521 | When COLUMNS or LINES is not defined, which is the common case, |
| 1522 | the terminal connected to sys.__stdout__ is queried |
| 1523 | by invoking os.get_terminal_size. |
| 1524 | |
| 1525 | If the terminal size cannot be successfully queried, either because |
| 1526 | the system doesn't support querying, or because we are not |
| 1527 | connected to a terminal, the value given in fallback parameter |
| 1528 | is used. Fallback defaults to (80, 24) which is the default |
| 1529 | size used by many terminal emulators. |
| 1530 | |
| 1531 | The value returned is a named tuple of type os.terminal_size. |
| 1532 | """ |
| 1533 | # columns, lines are the working values |
| 1534 | try: |
| 1535 | columns = int(os.environ['COLUMNS']) |
| 1536 | except (KeyError, ValueError): |
| 1537 | columns = 0 |
| 1538 | |
| 1539 | try: |
| 1540 | lines = int(os.environ['LINES']) |
| 1541 | except (KeyError, ValueError): |
| 1542 | lines = 0 |
| 1543 | |
| 1544 | # only query if necessary |
| 1545 | if columns <= 0 or lines <= 0: |
| 1546 | try: |
| 1547 | size = os.get_terminal_size(sys.__stdout__.fileno()) |
| 1548 | except (AttributeError, ValueError, OSError): |
| 1549 | # stdout is None, closed, detached, or not a terminal, or |
| 1550 | # os.get_terminal_size() is unsupported |
| 1551 | size = os.terminal_size(fallback) |
| 1552 | if columns <= 0: |
| 1553 | columns = size.columns or fallback[0] |
| 1554 | if lines <= 0: |
| 1555 | lines = size.lines or fallback[1] |
| 1556 | |
| 1557 | return os.terminal_size((columns, lines)) |
| 1558 | |
| 1559 | |
| 1560 | # Check that a given file can be accessed with the correct mode. |
nothing calls this directly
no test coverage detected
searching dependent graphs…