Use cross-platform shutil.get_terminal_size to get term_width

This fixes Python 3.14 incompatibility.
This commit is contained in:
Šarūnas Nejus 2025-10-26 20:37:08 +00:00
parent 1ea3879aae
commit 3eb68ef830
No known key found for this signature in database
3 changed files with 7 additions and 22 deletions

View file

@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
platform: [ubuntu-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
runs-on: ${{ matrix.platform }}
env:
IS_MAIN_PYTHON: ${{ matrix.python-version == '3.9' && matrix.platform == 'ubuntu-latest' }}

View file

@ -23,8 +23,8 @@ import errno
import optparse
import os.path
import re
import shutil
import sqlite3
import struct
import sys
import textwrap
import traceback
@ -699,27 +699,11 @@ def get_replacements():
return replacements
def term_width():
@cache
def term_width() -> int:
"""Get the width (columns) of the terminal."""
fallback = config["ui"]["terminal_width"].get(int)
# The fcntl and termios modules are not available on non-Unix
# platforms, so we fall back to a constant.
try:
import fcntl
import termios
except ImportError:
return fallback
try:
buf = fcntl.ioctl(0, termios.TIOCGWINSZ, " " * 4)
except OSError:
return fallback
try:
height, width = struct.unpack("hh", buf)
except struct.error:
return fallback
return width
columns, _ = shutil.get_terminal_size(fallback=(0, 0))
return columns if columns else config["ui"]["terminal_width"].get(int)
def split_into_lines(string, width_tuple):

View file

@ -22,6 +22,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: Implementation :: CPython",
]
packages = [