Improved windows terminal support.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d5b2191 · Browse files · 3 files·+21 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+21 added−2 removed
- other:+21 added−2 removed
Provenance
- Commit
13aa9c5- Tree
e67e83e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d5b2191
3 files changed+21 added−2 removed
modified:click/_compat.py+11 added
5 unmodified lines6 from weakref import WeakKeyDictionary7 8 9 PY2 = sys.version_info[0] == 210+DEFAULT_COLUMNS = 8011 12 13 _ansi_re = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')14 432 unmodified lines447 448 449 auto_wrap_for_ansi = None450 colorama = None451+get_winterm_size = None452 453 454 # If we're on Windows, we provide transparent integration through455 # colorama. This will make ANSI colors through the echo function456 # work automatically.457 if sys.platform.startswith('win'):458+ # Windows has a smaller terminal459+ DEFAULT_COLUMNS = 79460+461 try:462 import colorama463 except ImportError:464 pass30 unmodified lines495 except Exception:496 pass497 return rv498 499+ def get_winterm_size():500+ win = colorama.win32.GetConsoleScreenBufferInfo(501+ colorama.win32.STDOUT).srWindow502+ return win.Right - win.Left, win.Bottom - win.Top503 504+505+506 def strip_ansi(value):507 return _ansi_re.sub('', value)508 509 44 unmodified linesmodified:click/termui.py+6 added-2 removed
1 unmodified line2 import sys3 import struct4 5 from ._compat import raw_input, text_type, string_types, \6- colorama, isatty, strip_ansi6+ colorama, isatty, strip_ansi, get_winterm_size, \7+ DEFAULT_COLUMNS8 from .utils import echo9 from .exceptions import Abort, UsageError10 from .types import convert_type11 134 unmodified lines146 if shutil_get_terminal_size:147 sz = shutil_get_terminal_size()148 return sz.columns, sz.lines149 150+ if get_winterm_size is not None:151+ return get_winterm_size()152+153 def ioctl_gwinsz(fd):154 try:155 import fcntl156 import termios14 unmodified lines171 except Exception:172 pass173 if not cr or not cr[0] or not cr[1]:174 cr = (os.environ.get('LINES', 25),171- os.environ.get('COLUMNS', 80))175+ os.environ.get('COLUMNS', DEFAULT_COLUMNS))176 return int(cr[1]), int(cr[0])177 178 179 def echo_via_pager(text):312 unmodified linesmodified:CHANGES+4 added
9 unmodified lines10 11 - fixed the :func:`launch` function on windows.12 - improved the colorama support on windows to try hard to not13 screw up the console if the application is interrupted.14+- fixed windows terminals incorrectly being reported to be 8015+ characters wide instead of 7916+- use colorama win32 bindings if available to get the correct17+ dimensions of a windows terminal.18 19 Version 2.020 -----------21 53 unmodified lines