X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=src%2Fpyutils%2Ftext_utils.py;h=66f6f22dcd5dc8dcbd57777947002936e50e01f6;hb=06aed70c0ceefccc830d8f77e0f39a97ed05b898;hp=37d721b7bb027a0dff77d144eb7669328bd3ec20;hpb=b38920f24d1ac948958480c540bc4b8436186765;p=pyutils.git diff --git a/src/pyutils/text_utils.py b/src/pyutils/text_utils.py index 37d721b..66f6f22 100644 --- a/src/pyutils/text_utils.py +++ b/src/pyutils/text_utils.py @@ -25,7 +25,7 @@ import re import sys from collections import defaultdict from dataclasses import dataclass -from typing import Dict, Generator, List, Literal, Optional, Tuple +from typing import Dict, Generator, List, Literal, Optional, Tuple, Union from pyutils import string_utils from pyutils.ansi import fg, reset @@ -52,37 +52,26 @@ def get_console_rows_columns() -> RowsColumns: """ from pyutils.exec_utils import cmd - rows: Optional[str] = os.environ.get('LINES', None) - cols: Optional[str] = os.environ.get('COLUMNS', None) + rows: Union[Optional[str], int] = os.environ.get('LINES', None) + cols: Union[Optional[str], int] = os.environ.get('COLUMNS', None) if not rows or not cols: - logger.debug('Rows: %s, cols: %s, trying stty.', rows, cols) try: - rows, cols = cmd( - "stty size", - timeout_seconds=1.0, - ).split() + size = os.get_terminal_size() + rows = size.lines + cols = size.columns except Exception: rows = None cols = None - if rows is None: - logger.debug('Rows: %s, cols: %s, tput rows.', rows, cols) + if not rows or not cols: + logger.debug('Rows: %s, cols: %s, trying stty.', rows, cols) try: - rows = cmd( - "tput rows", + rows, cols = cmd( + "stty size", timeout_seconds=1.0, - ) + ).split() except Exception: rows = None - - if cols is None: - logger.debug('Rows: %s, cols: %s, tput cols.', rows, cols) - try: - cols = cmd( - "tput cols", - timeout_seconds=1.0, - ) - except Exception: cols = None if not rows or not cols: