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

Brodie Rao dackze at gmail.com
Mon Aug 10 09:24:12 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1249914212 14400
# Node ID 1eced7efc4d9da8c5295240fe4f001680174af58
# Parent  2987e61c1eb731601a16e5284b790c6bab3e6259
color: colorize diff --stat

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -189,9 +189,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:
diff --git a/tests/test-diffstat-color b/tests/test-diffstat-color
new file mode 100755
--- /dev/null
+++ b/tests/test-diffstat-color
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+echo "[extensions]" >> $HGRCPATH
+echo "color=" >> $HGRCPATH
+
+hg init repo
+cd repo
+
+cat > a <<EOF
+a
+b
+c
+d
+e
+f
+g
+EOF
+
+hg ci -Am adda
+
+cat > a <<EOF
+a
+d
+e
+f
+g
+h
+i
+j
+EOF
+
+echo '% diffstat color'
+hg diff --stat --color=always
diff --git a/tests/test-diffstat-color.out b/tests/test-diffstat-color.out
new file mode 100644
--- /dev/null
+++ b/tests/test-diffstat-color.out
@@ -0,0 +1,4 @@
+adding a
+% diffstat color
+ a |  5 +++--
+ 1 files changed, 3 insertions(+), 2 deletions(-)


More information about the Mercurial-devel mailing list