Fix get_terminal_width on Windows (#10842)

`shutil.get_terminal_size` works as a drop-in replacement for
`get_terminal_width`, enabling accurate widths on Windows.
This commit is contained in:
Steffen Rehberg
2022-09-20 23:06:25 +02:00
committed by GitHub
parent 276f430b57
commit 56fd7fb8ae

View File

@@ -2,6 +2,7 @@
import os
import re
import shutil
import sys
from typing import Dict, Pattern
@@ -22,21 +23,8 @@ def terminal_safe(s: str) -> str:
def get_terminal_width() -> int:
"""Borrowed from the py lib."""
if sys.platform == "win32":
# For static typing, as fcntl & termios never exist on Windows.
return int(os.environ.get('COLUMNS', 80)) - 1
try:
import fcntl
import struct
import termios
call = fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('hhhh', 0, 0, 0, 0))
height, width = struct.unpack('hhhh', call)[:2]
terminal_width = width
except Exception:
# FALLBACK
terminal_width = int(os.environ.get('COLUMNS', 80)) - 1
return terminal_width
"""Return the width of the terminal in columns."""
return shutil.get_terminal_size().columns - 1
_tw: int = get_terminal_width()