From 635a136ec06c00efadc46a9d364f15d771f4036e Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Tue, 12 Dec 2023 21:25:02 +0100 Subject: Update for Python3 File "attoconf/help.py", line 113, in detect_terminal_width fd = getattr(fd, 'fileno', lambda: -1)() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ io.UnsupportedOperation: fileno --- attoconf/help.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/attoconf/help.py b/attoconf/help.py index 777becb..a2379a6 100644 --- a/attoconf/help.py +++ b/attoconf/help.py @@ -109,23 +109,16 @@ class HelpSection(object): def detect_terminal_width(fd, DEFAULT_WIDTH=float('inf')): ''' Detect the width of a terminal. ''' - if not isinstance(fd, int): - fd = getattr(fd, 'fileno', lambda: -1)() - if fd == -1: - return DEFAULT_WIDTH - - import fcntl - import termios - try: - buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, b'xx' * 4) - except IOError as e: - import errno - if e.errno != errno.ENOTTY: - raise - return DEFAULT_WIDTH - import struct - ws_row, ws_col, ws_xpixel, ws_ypixel = struct.unpack('HHHH', buf) - return ws_col + import os + import shutil + + if isinstance(fd, int): + try: + return os.get_terminal_size(fd) + except OSError(...): + return DEFAULT_WIDTH + else: + return shutil.get_terminal_size(fallback=(DEFAULT_WIDTH, 24)).columns class Help(object): -- cgit v1.2.3-70-g09d2