Added colorama support into the echo function.
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 4e099e5 · Browse files · 5 files·+71 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
5 files+71 added−2 removed
- other:+71 added−2 removed
Provenance
- Commit
ba19550- Tree
ee39319- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
4e099e5
5 files changed+71 added−2 removed
modified:click/_compat.py+27 added
425 unmodified lines426 def __repr__(self):427 return repr(self._f)428 429 430+_auto_wrap_for_ansi = lambda x: x431+432+433+try:434+ import colorama435+except ImportError:436+ colorama = None437+else:438+ from weakref import WeakKeyDictionary439+ _ansi_stream_wrappers = WeakKeyDictionary()440+441+ def _auto_wrap_for_ansi(stream):442+ cached = _ansi_stream_wrappers.get(stream)443+ if cached is not None:444+ return cached445+ try:446+ strip = not stream.isatty()447+ except Exception:448+ strip = True449+ rv = colorama.AnsiToWin32(stream, strip=strip).stream450+ try:451+ _ansi_stream_wrappers[stream] = rv452+ except Exception:453+ pass454+ return rv455+456+457 binary_streams = {458 'stdin': get_binary_stdin,459 'stdout': get_binary_stdout,460 'stderr': get_binary_stderr,7 unmodified linesmodified:click/_termui_impl.py+2 added-1 removed
183 unmodified lines184 line = self.format_progress_line()185 line_len = len(line)186 if self.max_width is None or self.max_width < line_len:187 self.max_width = line_len188- self.file.write(line)188+ # Use echo here so that we get colorama support.189+ echo(line, file=self.file, nl=False)190 self.file.write(' ' * (clear_width - line_len))191 self.file.flush()192 193 def make_step(self):33 unmodified linesmodified:click/utils.py+17 added-1 removed
1 unmodified line2 import sys3 from collections import deque4 5 from ._compat import text_type, open_stream, get_streerror, string_types, \6- PY2, get_best_encoding, binary_streams, text_streams, filename_to_ui6+ PY2, get_best_encoding, binary_streams, text_streams, filename_to_ui, \7+ _auto_wrap_for_ansi8 9 if not PY2:10 from ._compat import _find_binary_writer11 190 unmodified lines202 data on both 2.x and 3.x to the given file in the most appropriate way203 possible. This is a very carefree function as in that it will try its204 best to not fail.205 206+ In addition to that if `colorama`_ is installed the echo function will207+ also support clever handling of ansi codes. Essentially it will then208+ do the following:209+210+ - add transparent handling of ANSI color codes on Windows211+ - hide ANSI codes automatically if the destination file is not a212+ terminal.213+214+ .. _colorama: http://pypi.python.org/pypi/colorama215+216+ .. versionmodified:: 2.0217+ Starting with version 2.0 of Click, the echo function will work218+ with colorama if it's installed.219+220 :param message: the message to print221 :param file: the file to write to (defaults to ``stdout``)222 :param nl: if set to `True` (the default) a newline is printed afterwards.223 """224 if file is None:225 file = sys.stdout226+ file = _auto_wrap_for_ansi(file)227 228 if message is not None and not isinstance(message, echo_native_types):229 message = text_type(message)230 119 unmodified linesmodified:docs/utils.rst+23 added
29 unmodified lines30 31 click.echo(b'\xe2\x98\x83', nl=False)32 33 34+ANSI Colors35+-----------36+37+.. versionadded:: 2.038+39+Starting with click 2.0 the :func:`echo` function gained a bit of extra40+functionality to deal with ANSI colors. This functionality is only41+available if `colorama`_ is installed. If it is installed then ANSI codes42+are intelligently handled.43+44+Primarily this means that:45+46+- click's echo function will automatically strip ANSI color codes if the47+ stream is not connected to a terminal.48+- the echo function will transparently connect to the terminal on49+ Windows and translate ANSI codes to terminal API calls. This means50+ that colors will work on Windows the same way they do on other51+ operating systems.52+53+54+.. _colorama: https://pypi.python.org/pypi/colorama55+56+57 Printing Filenames58 ------------------59 60 Because filenames might not be unicode, formatting them can be a bit113 unmodified linesmodified:CHANGES+2 added
15 unmodified lines16 custom validation with the same error messages as in the type system.17 - added :func:`progressbar`; a function to show progress bars.18 - added :func:`get_app_dir`; a function to calculate the home folder19 for configs.20+- Added transparent handling for ANSI codes into the :func:`echo`21+ function through `colorama`.22 23 Version 1.124 -----------25 11 unmodified lines