Added better color support for pagers.
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 537e51e · Browse files · 3 files·+51 added−22 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+51 added−22 removed
- other:+51 added−22 removed
Provenance
- Commit
52f9d9b- Tree
33e3517- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
537e51e
3 files changed+51 added−22 removed
modified:click/_termui_impl.py+42 added-20 removed
249 unmodified lines250 __next__ = next251 del next252 253 254-def pager(text):254+def pager(text, color=None):255 """Decide what method to use for paging through text."""256 stdout = _default_text_stdout()257 if not isatty(sys.stdin) or not isatty(stdout):258- return _nullpager(stdout, text)258+ return _nullpager(stdout, text, color)259 if 'PAGER' in os.environ:260 if sys.platform == 'win32':261- return _tempfilepager(strip_ansi(text), os.environ['PAGER'])262- elif os.environ.get('TERM') in ('dumb', 'emacs'):263- return _pipepager(strip_ansi(text), os.environ['PAGER'])264- else:265- return _pipepager(text, os.environ['PAGER'])261+ return _tempfilepager(text, os.environ['PAGER'], color)262+ return _pipepager(text, os.environ['PAGER'], color)263 if os.environ.get('TERM') in ('dumb', 'emacs'):267- return _nullpager(stdout, text)264+ return _nullpager(stdout, text, color)265 if sys.platform == 'win32' or sys.platform.startswith('os2'):269- return _tempfilepager(strip_ansi(text), 'more <')266+ return _tempfilepager(text, 'more <', color)267 if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0:271- return _pipepager(text, 'less')268+ return _pipepager(text, 'less', color)269 270 import tempfile271 fd, filename = tempfile.mkstemp()272 os.close(fd)273 try:274 if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0:278- return _pipepager(text, 'more')279- return _nullpager(stdout, text)275+ return _pipepager(text, 'more', color)276+ return _nullpager(stdout, text, color)277 finally:278 os.unlink(filename)279 280 284-def _pipepager(text, cmd):285- """Page through text by feeding it to another program."""286- pipe = os.popen(cmd, 'w')281+def _pipepager(text, cmd, color):282+ """Page through text by feeding it to another program. Invoking a283+ pager through this might support colors.284+ """285+ import subprocess286+ env = dict(os.environ)287+288+ # If we're piping to less we might support colors under the289+ # condition that290+ cmd_detail = cmd.rsplit('/', 1)[-1].split()291+ if color is None and cmd_detail[0] == 'less':292+ less_flags = os.environ.get('LESS', '') + ' '.join(cmd_detail[1:])293+ if not less_flags:294+ env['LESS'] = '-R'295+ color = True296+ elif 'r' in less_flags or 'R' in less_flags:297+ color = True298+299+ if not color:300+ text = strip_ansi(text)301+302+ c = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,303+ env=env)304 try:288- pipe.write(text)289- pipe.close()305+ c.stdin.write(text)306+ c.stdin.close()307 except IOError:308 pass309+ c.wait()310 311 294-def _tempfilepager(text, cmd):312+def _tempfilepager(text, cmd, color):313 """Page through text by invoking a program on a temporary file."""314 import tempfile315 filename = tempfile.mktemp()316+ if not color:317+ text = strip_ansi(text)318 with open_stream(filename, 'w')[0] as f:319 f.write(text)320 try:321 os.system(cmd + ' "' + filename + '"')322 finally:323 os.unlink(filename)324 325 306-def _nullpager(stream, text):326+def _nullpager(stream, text, color):327 """Simply print unformatted text. This is the ultimate fallback."""308- stream.write(strip_ansi(text))328+ if not color:329+ text = strip_ansi(text)330+ stream.write(text)331 332 333 class Editor(object):334 175 unmodified linesmodified:click/termui.py+7 added-2 removed
174 unmodified lines175 os.environ.get('COLUMNS', DEFAULT_COLUMNS))176 return int(cr[1]), int(cr[0])177 178 179-def echo_via_pager(text):179+def echo_via_pager(text, color=None):180 """This function takes a text and shows it via an environment specific181 pager on stdout.182 183+ .. versionchanged:: 3.0184+ Added the `color` flag.185+186 :param text: the text to page.187+ :param color: controls if the pager supports ANSI colors or not. The188+ default is autodetection.189 """190 if not isinstance(text, string_types):191 text = text_type(text)192 from ._termui_impl import pager188- return pager(text + '\n')193+ return pager(text + '\n', color)194 195 196 def progressbar(iterable=None, length=None, label=None, show_eta=True,197 show_percent=None, show_pos=False,299 unmodified linesmodified:CHANGES+2 added
10 unmodified lines11 - formatter now no longer attempts to accomodate for terminals12 smaller than 50 characters. If that happens it just assumes13 a minimal width.14 - added a way to not swallow exceptions in the test system.15+- added better support for colors with pagers and ways to16+ override the autodetection.17 18 Version 2.119 -----------20 69 unmodified lines