[PATCH 4 of 4] color: colorize diff --stat

Brodie Rao dackze at gmail.com
Thu Jul 30 10:06:27 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1248966182 14400
# Node ID 270e010c062006798b5e1c82f6d167339a44cefc
# Parent  5862bde342a377fa66d789d25d37626dc779626a
color: colorize diff --stat

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -192,9 +192,25 @@ def colorshowpatch(orig, self, node):
     finally:
         self.ui.write = oldwrite
 
+def colordiffstat(orig, s):
+    lines = s.split('\n')
+    for i, line in enumerate(lines):
+        if line and line[-1] in '+-':
+            name, graph = line.rsplit(' ', 1)
+            graph = graph.replace('-',
+                        render_effects('-', _diff_effects['deleted']))
+            graph = graph.replace('+',
+                        render_effects('+', _diff_effects['inserted']))
+            lines[i] = ' '.join([name, graph])
+    orig('\n'.join(lines))
+
 def colordiff(orig, ui, repo, *pats, **opts):
     '''run the diff command with colored output'''
-    oldwrite = extensions.wrapfunction(ui, 'write', colorwrap)
+    if opts['stat']:
+        wrapper = colordiffstat
+    else:
+        wrapper = colorwrap
+    oldwrite = extensions.wrapfunction(ui, 'write', wrapper)
     try:
         orig(ui, repo, *pats, **opts)
     finally:


More information about the Mercurial-devel mailing list