Skip to content
Myr

Correctly strip ansi codes on windows without colorama.

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 bb7b2df · Browse files · 5 files+24 added−18 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+24 added−18 removed

  • other:+24 added−18 removed

5 files+24 added−18 removed

modified:click/_compat.py+3 added-3 removed
5 unmodified lines
6from weakref import WeakKeyDictionary789PY2 = sys.version_info[0] == 210WIN = sys.platform.startswith('win')11DEFAULT_COLUMNS = 80121314_ansi_re = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')
137 unmodified lines
152 # An option would be to do what Python 3 does and to open the file as153 # binary only, patch it back to the system, and then use a wrapper154 # stream that converts newlines. It's not quite clear what's the155 # correct option here.155 if sys.platform == 'win32':156 if WIN:157 import msvcrt158 def set_binary_mode(f):159 try:160 fileno = f.fileno()
297 unmodified lines
458459# If we're on Windows, we provide transparent integration through460# colorama. This will make ANSI colors through the echo function461# work automatically.461if sys.platform.startswith('win'):462if WIN:463 # Windows has a smaller terminal464 DEFAULT_COLUMNS = 79465466 try:
39 unmodified lines
506 colorama.win32.STDOUT).srWindow507 return win.Right - win.Left, win.Bottom - win.Top508509509510def strip_ansi(value):511 return _ansi_re.sub('', value)512513
44 unmodified lines
modified:click/_termui_impl.py+7 added-7 removed
12 unmodified lines
13import sys14import time15import math16from ._compat import _default_text_stdout, range_type, PY2, isatty, \17 open_stream, strip_ansi, term_len, get_best_encoding17 open_stream, strip_ansi, term_len, get_best_encoding, WIN18from .utils import echo19from .exceptions import ClickException2021
231 unmodified lines
253 stdout = _default_text_stdout()254 if not isatty(sys.stdin) or not isatty(stdout):255 return _nullpager(stdout, text)256 if 'PAGER' in os.environ:257 if sys.platform == 'win32':257 if WIN:258 return _tempfilepager(strip_ansi(text), os.environ['PAGER'])259 elif os.environ.get('TERM') in ('dumb', 'emacs'):260 return _pipepager(strip_ansi(text), os.environ['PAGER'])261 else:262 return _pipepager(text, os.environ['PAGER'])263 if os.environ.get('TERM') in ('dumb', 'emacs'):264 return _nullpager(stdout, text)265 if sys.platform == 'win32' or sys.platform.startswith('os2'):265 if WIN or sys.platform.startswith('os2'):266 return _tempfilepager(strip_ansi(text), 'more <')267 if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0:268 return _pipepager(text, 'less')269
50 unmodified lines
320 for key in 'VISUAL', 'EDITOR':321 rv = os.environ.get(key)322 if rv:323 return rv324 if sys.platform.startswith('win'):324 if WIN:325 return 'notepad'326 for editor in 'vim', 'nano':327 if os.system('which %s &> /dev/null' % editor) == 0:328 return editor
24 unmodified lines
353 text += '\n'354355 fd, name = tempfile.mkstemp(prefix='editor-', suffix=self.extension)356 try:357 if sys.platform.startswith('win'):357 if WIN:358 encoding = 'utf-8-sig'359 text = text.replace('\n', '\r\n')360 else:361 encoding = 'utf-8'
43 unmodified lines
405 try:406 return subprocess.Popen(args, stderr=null).wait()407 finally:408 null.close()409 elif sys.platform.startswith('win'):409 elif WIN:410 if locate:411 url = _unquote_file(url)412 args = 'explorer /select,"%s"' % _unquote_file(413 url.replace('"', ''))
25 unmodified lines
439 if ch == '\x04':440 raise EOFError()441442443if sys.platform.startswith('win'):443if WIN:444 import msvcrt445446 def getchar(echo):447 rv = msvcrt.getch()
37 unmodified lines
modified:click/termui.py+2 added-2 removed
2 unmodified lines
3import struct45from ._compat import raw_input, text_type, string_types, \6 colorama, isatty, strip_ansi, get_winterm_size, \7 DEFAULT_COLUMNS7 DEFAULT_COLUMNS, WIN8from .utils import echo9from .exceptions import Abort, UsageError10from .types import convert_type11
261 unmodified lines
273 return274 # If we're on Windows and we don't have colorama available, then we275 # clear the screen by shelling out. Otherwise we can use an escape276 # sequence.277 if sys.platform.startswith('win') and colorama is None:277 if WIN and colorama is None:278 os.system('cls')279 else:280 sys.stdout.write('\033[2J\033[1;1H')281
210 unmodified lines
modified:click/utils.py+10 added-6 removed
3 unmodified lines
45from ._compat import text_type, open_stream, get_streerror, string_types, \6 PY2, binary_streams, text_streams, filename_to_ui, \7 auto_wrap_for_ansi, strip_ansi, isatty, _default_text_stdout, \8 is_bytes8 is_bytes, WIN910if not PY2:11 from ._compat import _find_binary_writer12
232 unmodified lines
245 return246247 # ANSI-style support. If there is no message or we are dealing with248 # bytes nothing is happening. If we are connected to a file we want249 # to strip colors. If we have support for wrapping streams (windows250 # through colorama) we want to do that.249 # to strip colors. If we are on windows we either wrap the stream250 # to strip the color or we use the colorama support to translate the251 # ansi codes to API calls.252 if message and not is_bytes(message):253 if not isatty(file):254 message = strip_ansi(message)255 elif WIN:254 elif auto_wrap_for_ansi is not None:255 file = auto_wrap_for_ansi(file)256 if auto_wrap_for_ansi is not None:257 file = auto_wrap_for_ansi(file)258 else:259 message = strip_ansi(message)260261 if message:262 file.write(message)263 if nl:
85 unmodified lines
349 folder will be stored in the home folder with a leading350 dot instead of the XDG config home or darwin's351 application support folder.352 """349 if sys.platform.startswith('win'):353 if WIN:354 key = roaming and 'APPDATA' or 'LOCALAPPDATA'355 folder = os.environ.get(key)356 if folder is None:357 folder = os.path.expanduser('~')
9 unmodified lines
modified:CHANGES+2 added
7 unmodified lines
89(bugfix release, release date to be decided)1011- Fixed an incorrectly formatted help record for count options.'12- Add support for ansi code stripping on Windows if colorama13 is not available.1415Version 2.216-----------17
78 unmodified lines