terminalwriter: simplify get_terminal_width()

The shutil.get_terminal_size() handles everything this did already.
This commit is contained in:
Ran Benita 2020-04-29 15:12:32 +03:00
parent c749e44efc
commit 6c1b6a09b8
1 changed files with 4 additions and 23 deletions

View File

@ -23,35 +23,16 @@ if sys.platform == "win32":
win32_and_ctypes = True win32_and_ctypes = True
def _getdimensions(): def get_terminal_width() -> int:
size = shutil.get_terminal_size() width, _ = shutil.get_terminal_size(fallback=(80, 24))
return size.lines, size.columns
# The Windows get_terminal_size may be bogus, let's sanify a bit.
def get_terminal_width():
width = 0
try:
_, width = _getdimensions()
except (KeyboardInterrupt, SystemExit, MemoryError, GeneratorExit):
raise
except BaseException:
# pass to fallback below
pass
if width == 0:
# FALLBACK:
# * some exception happened
# * or this is emacs terminal which reports (0,0)
width = int(os.environ.get("COLUMNS", 80))
# XXX the windows getdimensions may be bogus, let's sanify a bit
if width < 40: if width < 40:
width = 80 width = 80
return width return width
terminal_width = get_terminal_width()
char_width = { char_width = {
"A": 1, # "Ambiguous" "A": 1, # "Ambiguous"
"F": 2, # Fullwidth "F": 2, # Fullwidth