[PATCH 2 of 2] diff colorization: finish highlighting trailing whitespace, use sys.__stdout__

Georg Brandl georg at python.org
Wed Nov 26 15:58:56 CST 2008


# HG changeset patch
# User Georg Brandl <georg at python.org>
# Date 1227736687 -3600
# Node ID 2216b5f73cddb2c5c684b882c47555e73e05ac7b
# Parent  443121fba9f40c3c014b89868ba6b68fec35126e
diff colorization: finish highlighting trailing whitespace, use sys.__stdout__.

diff -r 443121fba9f4 -r 2216b5f73cdd hgext/color.py
--- a/hgext/color.py	Wed Nov 26 08:58:31 2008 -0500
+++ b/hgext/color.py	Wed Nov 26 22:58:07 2008 +0100
@@ -21,11 +21,12 @@
 This extension modifies the status command to add color to its output to
 reflect file status, the qseries command to add color to reflect patch status
 (applied, unapplied, missing), and to diff-related commands to highlight
-additions, removals, diff headers, and trailing whitespace.  Other effects in
-addition to color, like bold and underlined text, are also available.
-Effects are rendered with the ECMA-48 SGR control function (aka ANSI escape
-codes).  This module also provides the render_text function, which can be
-used to add effects to any text.
+additions, removals, diff headers, and trailing whitespace.
+
+Other effects in addition to color, like bold and underlined text, are also
+available.  Effects are rendered with the ECMA-48 SGR control function (aka
+ANSI escape codes).  This module also provides the render_text function,
+which can be used to add effects to any text.
 
 To enable this extension, add this to your .hgrc file:
 [extensions]
@@ -57,7 +58,7 @@
 diff.deleted = red
 diff.inserted = green
 diff.changed = white
-diff.whitespace = bold red_background
+diff.trailingwhitespace = bold red_background
 '''
 
 import os, re, sys
@@ -176,11 +177,17 @@
     '''wrap ui.write for colored diff output'''
     lines = s.split('\n')
     for i, line in enumerate(lines):
+        stripline = line
+        if line and line[0] in '+-':
+            # highlight trailing whitespace, but only in changed lines
+            stripline = line.rstrip()
         for prefix, style in _diff_prefixes:
-            if line.startswith(prefix):
-                effects = _diff_effects[style]
-                lines[i] = render_effects(line, *_diff_effects[style])
+            if stripline.startswith(prefix):
+                lines[i] = render_effects(stripline, *_diff_effects[style])
                 break
+        if line != stripline:
+            lines[i] += render_effects(
+                line[len(stripline):], *_diff_effects['trailingwhitespace'])
     orig('\n'.join(lines))
 
 def colorshowpatch(orig, self, node):
@@ -217,7 +224,8 @@
                  'hunk': ('magenta',),
                  'deleted': ('red',),
                  'inserted': ('green',),
-                 'changed': ('white',)}
+                 'changed': ('white',),
+                 'trailingwhitespace': ('bold', 'red_background'),}
 
 def uisetup(ui):
     '''Initialize the extension.'''
@@ -233,26 +241,12 @@
         _setupcmd(ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects)
         _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
 
-def isatty():
-    '''check sys.stdout.isatty(), even when sys.stdout has been reassigned'''
-    if sys.stdout.fileno() != 1:
-        try:
-            stdout = os.fdopen(os.dup(1), 'w')
-            try:
-                return stdout.isatty()
-            finally:
-                stdout.close()
-        except Exception:
-            pass
-    return sys.stdout.isatty()
-
 def _setupcmd(ui, cmd, table, func, effectsmap):
     '''patch in command to command table and load effect map'''
     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 isatty()))):
+            (opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb'
+                                          or not sys.__stdout__.isatty()))):
             return orig(*args, **opts)
 
         oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer,
@@ -266,7 +260,7 @@
 
     entry = extensions.wrapcommand(table, cmd, nocolor)
     entry[1].extend([
-        ('', 'color', 'auto', _("when to colorize (always, auto, or never)")),
+        ('', 'color', 'auto', _("when to colorize (always, auto, or never)")),   
         ('', 'no-color', None, _("don't colorize output")),
     ])
 


More information about the Mercurial-devel mailing list