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
Provenance
- Commit
1cceb6d- Tree
de171f9- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
bb7b2df
5 files changed+24 added−18 removed
modified:click/_compat.py+3 added-3 removed
5 unmodified lines6 from weakref import WeakKeyDictionary7 8 9 PY2 = sys.version_info[0] == 210+WIN = sys.platform.startswith('win')11 DEFAULT_COLUMNS = 8012 13 14 _ansi_re = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')137 unmodified lines152 # 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 lines458 459 # If we're on Windows, we provide transparent integration through460 # colorama. This will make ANSI colors through the echo function461 # work automatically.461-if sys.platform.startswith('win'):462+if WIN:463 # Windows has a smaller terminal464 DEFAULT_COLUMNS = 79465 466 try:39 unmodified lines506 colorama.win32.STDOUT).srWindow507 return win.Right - win.Left, win.Bottom - win.Top508 509 509-510 def strip_ansi(value):511 return _ansi_re.sub('', value)512 513 44 unmodified linesmodified:click/_termui_impl.py+7 added-7 removed
12 unmodified lines13 import sys14 import time15 import math16 from ._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, WIN18 from .utils import echo19 from .exceptions import ClickException20 21 231 unmodified lines253 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 lines320 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 editor24 unmodified lines353 text += '\n'354 355 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 lines405 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 lines439 if ch == '\x04':440 raise EOFError()441 442 443-if sys.platform.startswith('win'):443+if WIN:444 import msvcrt445 446 def getchar(echo):447 rv = msvcrt.getch()37 unmodified linesmodified:click/termui.py+2 added-2 removed
2 unmodified lines3 import struct4 5 from ._compat import raw_input, text_type, string_types, \6 colorama, isatty, strip_ansi, get_winterm_size, \7- DEFAULT_COLUMNS7+ DEFAULT_COLUMNS, WIN8 from .utils import echo9 from .exceptions import Abort, UsageError10 from .types import convert_type11 261 unmodified lines273 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 linesmodified:click/utils.py+10 added-6 removed
3 unmodified lines4 5 from ._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, WIN9 10 if not PY2:11 from ._compat import _find_binary_writer12 232 unmodified lines245 return246 247 # 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)260 261 if message:262 file.write(message)263 if nl:85 unmodified lines349 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 linesmodified:CHANGES+2 added
7 unmodified lines8 9 (bugfix release, release date to be decided)10 11 - Fixed an incorrectly formatted help record for count options.'12+- Add support for ansi code stripping on Windows if colorama13+ is not available.14 15 Version 2.216 -----------17 78 unmodified lines