Skip to content
Myr

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

5 files+71 added−2 removed

modified:click/_compat.py+27 added
425 unmodified lines
426 def __repr__(self):427 return repr(self._f)428429430_auto_wrap_for_ansi = lambda x: x431432433try:434 import colorama435except ImportError:436 colorama = None437else:438 from weakref import WeakKeyDictionary439 _ansi_stream_wrappers = WeakKeyDictionary()440441 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 rv455456457binary_streams = {458 'stdin': get_binary_stdin,459 'stdout': get_binary_stdout,460 'stderr': get_binary_stderr,
7 unmodified lines
modified:click/_termui_impl.py+2 added-1 removed
183 unmodified lines
184 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()192193 def make_step(self):
33 unmodified lines
modified:click/utils.py+17 added-1 removed
1 unmodified line
2import sys3from collections import deque45from ._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_ansi89if not PY2:10 from ._compat import _find_binary_writer11
190 unmodified lines
202 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.205206 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:209210 - add transparent handling of ANSI color codes on Windows211 - hide ANSI codes automatically if the destination file is not a212 terminal.213214 .. _colorama: http://pypi.python.org/pypi/colorama215216 .. versionmodified:: 2.0217 Starting with version 2.0 of Click, the echo function will work218 with colorama if it's installed.219220 :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)227228 if message is not None and not isinstance(message, echo_native_types):229 message = text_type(message)230
119 unmodified lines
modified:docs/utils.rst+23 added
29 unmodified lines
3031 click.echo(b'\xe2\x98\x83', nl=False)323334ANSI Colors35-----------3637.. versionadded:: 2.03839Starting with click 2.0 the :func:`echo` function gained a bit of extra40functionality to deal with ANSI colors. This functionality is only41available if `colorama`_ is installed. If it is installed then ANSI codes42are intelligently handled.4344Primarily this means that:4546- 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.525354.. _colorama: https://pypi.python.org/pypi/colorama555657Printing Filenames58------------------5960Because filenames might not be unicode, formatting them can be a bit
113 unmodified lines
modified:CHANGES+2 added
15 unmodified lines
16 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`.2223Version 1.124-----------25
11 unmodified lines