[PATCH 3 of 3] color: removed --no-color, added auto colorization
Brodie Rao
dackze at gmail.com
Wed Nov 12 09:28:59 CST 2008
# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1226502761 18000
# Node ID 8471ab51b5df2bd8d2e8dbb277fb810d85e5ba5c
# Parent 2fbcee0be37f7e9c2ccd27805dadb30e4296291c
color: removed --no-color, added auto colorization
This works like GNU grep --color. --color=auto only colorizes output for non-dumb terminals that are TTYs
diff -r 2fbcee0be37f -r 8471ab51b5df hgext/color.py
--- a/hgext/color.py Wed Nov 12 10:01:41 2008 -0500
+++ b/hgext/color.py Wed Nov 12 10:12:41 2008 -0500
@@ -56,7 +56,7 @@
diff.whitespace = bold red_background
'''
-import re, sys
+import os, re, sys
from mercurial import commands, extensions
from mercurial.i18n import _
@@ -232,13 +232,27 @@
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):
+ # Duplicate stdout in case sys.stdout has been reassigned
+ try:
+ stdout = os.fdopen(os.dup(1), 'w')
+ try:
+ isatty = stdout.isatty()
+ finally:
+ stdout.close()
+ except Exception:
+ isatty = sys.stdout.isatty()
+
+ if (opts['color'] == 'never' or
+ (opts['color'] == 'auto' and
+ (os.environ.get('TERM') == 'dumb' or not isatty))):
+ return orig(*args, **opts)
+ else:
+ return func(orig, *args, **opts)
entry = extensions.wrapcommand(table, cmd, nocolor)
- entry[1].append(('', 'no-color', None, _("don't colorize output")))
+ entry[1].append(('', 'color', 'auto',
+ _("when to colorize (always, auto, or never)")))
for status in effectsmap:
effects = ui.config('color', cmd + '.' + status)
More information about the Mercurial-devel
mailing list