Deprecate click.get_terminal_size() in favor of stdlib shutil.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Since Python 3.3, the stdlib provides the function shutil.get_terminal_size(). Now that the project has dropped Python 2 support, the compatibility shim is no longer necessary. The stdlib version returns a named tuple. So rather than indexing "0", can use the more self-documenting attribute "columns". Docs available at: https://docs.python.org/3/library/shutil.html#shutil.get_terminal_size
authored by Jon Dufresne · committed by David Lord · parent b28bc4c · Browse files · 5 files·+19 added−53 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+19 added−53 removed
- other:+19 added−53 removed
Provenance
- Commit
5ab1ac2- Tree
96cc344- Author
Jon Dufresne <jon.dufresne@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
b28bc4c
5 files changed+19 added−53 removed
modified:src/click/_compat.py-11 removed
10 unmodified lines11 APP_ENGINE = "APPENGINE_RUNTIME" in os.environ and "Development/" in os.environ.get(12 "SERVER_SOFTWARE", ""13 )14 WIN = sys.platform.startswith("win") and not APP_ENGINE and not MSYS215-DEFAULT_COLUMNS = 8015 auto_wrap_for_ansi = None16 colorama = None18-get_winterm_size = None17 _ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")18 19 20 def get_filesystem_encoding():472 unmodified lines493 # If we're on Windows, we provide transparent integration through494 # colorama. This will make ANSI colors through the echo function495 # work automatically.496 if WIN:499- # Windows has a smaller terminal500- DEFAULT_COLUMNS = 79501-497 from ._winconsole import _get_windows_console_stream498 499 def _get_argv_encoding():500 import locale37 unmodified lines538 except Exception:539 pass540 return rv541 547- def get_winterm_size():548- win = colorama.win32.GetConsoleScreenBufferInfo(549- colorama.win32.STDOUT550- ).srWindow551- return win.Right - win.Left, win.Bottom - win.Top542 553-543 else:544 545 def _get_argv_encoding():546 return getattr(sys.stdin, "encoding", None) or get_filesystem_encoding()54 unmodified linesmodified:src/click/_termui_impl.py+2 added-2 removed
222 unmodified lines223 }224 ).rstrip()225 226 def render_progress(self):227- from .termui import get_terminal_size227+ import shutil228 229 if self.is_hidden:230 return231 2 unmodified lines234 if self.autowidth:235 old_width = self.width236 self.width = 0237 clutter_length = term_len(self.format_progress_line())238- new_width = max(0, get_terminal_size()[0] - clutter_length)238+ new_width = max(0, shutil.get_terminal_size().columns - clutter_length)239 if new_width < old_width:240 buf.append(BEFORE_BAR)241 buf.append(" " * self.max_width)242 self.max_width = new_width444 unmodified linesmodified:src/click/formatting.py+3 added-2 removed
1 from contextlib import contextmanager2 3 from ._compat import term_len4 from .parser import split_opt5-from .termui import get_terminal_size5 6 # Can force a width. This is used by the test system7 FORCED_WIDTH = None8 93 unmodified lines102 width clamped to a maximum of 78.103 """104 105 def __init__(self, indent_increment=2, width=None, max_width=None):106+ import shutil107+108 self.indent_increment = indent_increment109 if max_width is None:110 max_width = 80111 if width is None:112 width = FORCED_WIDTH113 if width is None:113- width = max(min(get_terminal_size()[0], max_width) - 2, 50)114+ width = max(min(shutil.get_terminal_size().columns, max_width) - 2, 50)115 self.width = width116 self.current_indent = 0117 self.buffer = []118 162 unmodified linesmodified:src/click/termui.py+12 added-38 removed
1 import inspect2 import io3 import itertools4 import os5-import struct5 import sys6 8-from ._compat import DEFAULT_COLUMNS9-from ._compat import get_winterm_size7 from ._compat import is_bytes8 from ._compat import isatty9 from ._compat import strip_ansi10 from ._compat import WIN200 unmodified lines211 212 def get_terminal_size():213 """Returns the current size of the terminal as tuple in the form214 ``(width, height)`` in columns and rows.215+216+ .. deprecated:: 8.0217+ Will be removed in Click 8.1. Use218+ :func:`shutil.get_terminal_size` instead.219 """220 import shutil221+ import warnings222 221- if hasattr(shutil, "get_terminal_size"):222- return shutil.get_terminal_size()223+ warnings.warn(224+ "'click.get_terminal_size()' is deprecated and will be removed"225+ " in Click 8.1. Use 'shutil.get_terminal_size()' instead.",226+ DeprecationWarning,227+ stacklevel=2,228+ )229+ return shutil.get_terminal_size()230 224- # We provide a sensible default for get_winterm_size() when being invoked225- # inside a subprocess. Without this, it would not provide a useful input.226- if get_winterm_size is not None:227- size = get_winterm_size()228- if size == (0, 0):229- return (79, 24)230- else:231- return size231 233- def ioctl_gwinsz(fd):234- try:235- import fcntl236- import termios237-238- cr = struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))239- except Exception:240- return241- return cr242-243- cr = ioctl_gwinsz(0) or ioctl_gwinsz(1) or ioctl_gwinsz(2)244- if not cr:245- try:246- fd = os.open(os.ctermid(), os.O_RDONLY)247- try:248- cr = ioctl_gwinsz(fd)249- finally:250- os.close(fd)251- except Exception:252- pass253- if not cr or not cr[0] or not cr[1]:254- cr = (os.environ.get("LINES", 25), os.environ.get("COLUMNS", DEFAULT_COLUMNS))255- return int(cr[1]), int(cr[0])256-257-232 def echo_via_pager(text_or_generator, color=None):233 """This function takes a text and shows it via an environment specific234 pager on stdout.235 480 unmodified linesmodified:CHANGES.rst+2 added
157 unmodified lines158 - Use ``mkstemp()`` instead of ``mktemp()`` in pager implementation.159 :issue:`1752`160 - If ``Option.show_default`` is a string, it is displayed even if161 ``default`` is ``None``. :issue:`1732`162+- ``click.get_terminal_size()`` is deprecated and will be removed in163+ 8.1. Use :func:`shutil.get_terminal_size` instead. :issue:`1736`164 165 166 Version 7.1.2167 -------------635 unmodified lines