Skip to content
Myr

Merge pull request #1737 from jdufresne/get-terminal-size

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Deprecate click.get_terminal_size() in favor of stdlib shutil.

authored by David Lord · committed by GitHub · parents b28bc4c, 5ab1ac2 · 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

5 files+19 added−53 removed

modified:src/click/_compat.py-11 removed
10 unmodified lines
11APP_ENGINE = "APPENGINE_RUNTIME" in os.environ and "Development/" in os.environ.get(12 "SERVER_SOFTWARE", ""13)14WIN = sys.platform.startswith("win") and not APP_ENGINE and not MSYS215DEFAULT_COLUMNS = 8015auto_wrap_for_ansi = None16colorama = None18get_winterm_size = None17_ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")181920def get_filesystem_encoding():
472 unmodified lines
493# If we're on Windows, we provide transparent integration through494# colorama. This will make ANSI colors through the echo function495# work automatically.496if WIN:499 # Windows has a smaller terminal500 DEFAULT_COLUMNS = 79501497 from ._winconsole import _get_windows_console_stream498499 def _get_argv_encoding():500 import locale
37 unmodified lines
538 except Exception:539 pass540 return rv541547 def get_winterm_size():548 win = colorama.win32.GetConsoleScreenBufferInfo(549 colorama.win32.STDOUT550 ).srWindow551 return win.Right - win.Left, win.Bottom - win.Top542553543else:544545 def _get_argv_encoding():546 return getattr(sys.stdin, "encoding", None) or get_filesystem_encoding()
54 unmodified lines
modified:src/click/_termui_impl.py+2 added-2 removed
222 unmodified lines
223 }224 ).rstrip()225226 def render_progress(self):227 from .termui import get_terminal_size227 import shutil228229 if self.is_hidden:230 return231
2 unmodified lines
234 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_width
444 unmodified lines
modified:src/click/formatting.py+3 added-2 removed
1from contextlib import contextmanager23from ._compat import term_len4from .parser import split_opt5from .termui import get_terminal_size56# Can force a width. This is used by the test system7FORCED_WIDTH = None8
93 unmodified lines
102 width clamped to a maximum of 78.103 """104105 def __init__(self, indent_increment=2, width=None, max_width=None):106 import shutil107108 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 lines
modified:src/click/termui.py+12 added-38 removed
1import inspect2import io3import itertools4import os5import struct5import sys68from ._compat import DEFAULT_COLUMNS9from ._compat import get_winterm_size7from ._compat import is_bytes8from ._compat import isatty9from ._compat import strip_ansi10from ._compat import WIN
200 unmodified lines
211212def get_terminal_size():213 """Returns the current size of the terminal as tuple in the form214 ``(width, height)`` in columns and rows.215216 .. deprecated:: 8.0217 Will be removed in Click 8.1. Use218 :func:`shutil.get_terminal_size` instead.219 """220 import shutil221 import warnings222221 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()230224 # 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 size231233 def ioctl_gwinsz(fd):234 try:235 import fcntl236 import termios237238 cr = struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))239 except Exception:240 return241 return cr242243 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])256257232def 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 lines
modified:CHANGES.rst+2 added
157 unmodified lines
158- 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`164165166Version 7.1.2167-------------
635 unmodified lines