# HG changeset patch # User Brodie Rao # Date 1227707911 18000 # Node ID c9fd5474a707eae0ce96a768c6303f2818ae1fc6 # Parent 1d5ecaa47abb6bbfe23102a26c22a214487a1658 color: add --color switch --color works like GNU grep --color. --color=auto only colorizes output for non-dumb terminals that are TTYs. diff -r 1d5ecaa47abb -r c9fd5474a707 hgext/color.py --- a/hgext/color.py Mon Dec 01 20:10:02 2008 -0800 +++ b/hgext/color.py Wed Nov 26 08:58:31 2008 -0500 @@ -49,7 +49,7 @@ qseries.missing = red bold ''' -import re, sys +import os, re, sys from mercurial import commands, extensions from mercurial.i18n import _ @@ -113,7 +113,7 @@ effects = _status_effects[status] if effects: lines[i] = render_effects(lines[i], *effects) - sys.stdout.write(lines[i] + delimiter) + ui.write(lines[i] + delimiter) return retval _status_abbreviations = { 'M': 'modified', @@ -154,7 +154,7 @@ effects = _patch_effects['applied'] else: effects = _patch_effects['unapplied'] - sys.stdout.write(render_effects(patch, *effects) + '\n') + ui.write(render_effects(patch, *effects) + '\n') return retval _patch_effects = { 'applied': ('blue', 'bold', 'underline'), @@ -171,13 +171,22 @@ def _setupcmd(ui, cmd, table, func, effectsmap): '''patch in command to command table and load effect map''' - def nocolor(orig, *args, **kwargs): - if kwargs['no_color']: - return orig(*args, **kwargs) - return func(orig, *args, **kwargs) + def nocolor(orig, *args, **opts): + + if (opts['no_color'] or opts['color'] == 'never' or + (opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb' + or not sys.__stdout__.isatty()))): + return orig(*args, **opts) + + if func is not None: + return func(orig, *args, **opts) + return orig(*args, **opts) entry = extensions.wrapcommand(table, cmd, nocolor) - entry[1].append(('', 'no-color', None, _("don't colorize output"))) + entry[1].extend([ + ('', 'color', 'auto', _("when to colorize (always, auto, or never)")), + ('', 'no-color', None, _("don't colorize output")), + ]) for status in effectsmap: effects = ui.config('color', cmd + '.' + status)