[PATCH 2 of 3] color: colorize diff --stat

Brodie Rao dackze at gmail.com
Tue Sep 22 08:05:32 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1253624419 14400
# Node ID 52ab347c5cde528ccf1a925894fc0b482327157a
# Parent  bd0a07c03d2a9c83e5a8803928aa29790352cff3
color: colorize diff --stat

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -188,9 +188,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