[PATCH 1 of 2] color: add colored output to diff command

Georg Brandl georg at python.org
Sat Nov 22 13:26:22 CST 2008


# HG changeset patch
# User Georg Brandl <georg at python.org>
# Date 1227378665 -3600
# Node ID 39692450743df595714eb748f525db24ee6c1887
# Parent  e2048f5c7bf5304a9756fac356864282d9835ddb
color: add colored output to diff command

diff -r e2048f5c7bf5 -r 39692450743d hgext/color.py
--- a/hgext/color.py	Sat Nov 22 16:57:49 2008 +0100
+++ b/hgext/color.py	Sat Nov 22 19:31:05 2008 +0100
@@ -1,6 +1,7 @@
 # color.py color output for the status and qseries commands
 #
 # Copyright (C) 2007 Kevin Christen <kevin.christen at gmail.com>
+# Copyright (C) 2008 Georg Brandl <georg at python.org>
 #
 # This program is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License as published by the
@@ -30,7 +31,7 @@
 [extensions]
 color =
 
-Default effects my be overriden from the .hgrc file:
+Default effects may be overriden from the .hgrc file:
 
 [color]
 status.modified = blue bold underline red_background
@@ -47,6 +48,15 @@
 qseries.applied = blue bold underline
 qseries.unapplied = black bold
 qseries.missing = red bold
+
+diff.diffline = bold
+diff.extended = cyan bold
+diff.file_a = red bold
+diff.file_b = green bold
+diff.hunk = magenta
+diff.deleted = red
+diff.inserted = green
+diff.changed = white
 '''
 
 import re, sys
@@ -139,6 +149,43 @@
                     'clean': ('none', ),
                     'copied': ('none', ), }
 
+def colordiff(orig, ui, repo, *pats, **opts):
+    '''run the diff command with colored output'''
+
+    ui.pushbuffer()
+    retval = orig(ui, repo, *pats, **opts)
+    lines = ui.popbuffer().splitlines()
+    for line in lines:
+        for prefix, style in _diff_prefixes:
+            if line.startswith(prefix):
+                effects = _diff_effects[style]
+                sys.stdout.write(
+                    render_effects(line, *_diff_effects[style]) + '\n')
+                break
+        else:
+            sys.stdout.write(line + '\n')
+    return retval
+
+_diff_prefixes = [ ('diff', 'diffline'),
+                   ('copy', 'extended'),
+                   ('rename', 'extended'),
+                   ('new', 'extended'),
+                   ('deleted', 'extended'),
+                   ('---', 'file_a'),
+                   ('+++', 'file_b'),
+                   ('@', 'hunk'),
+                   ('-', 'deleted'),
+                   ('+', 'inserted'), ]
+
+_diff_effects = { 'diffline': ('bold', ),
+                  'extended': ('cyan', 'bold'),
+                  'file_a': ('red', 'bold'),
+                  'file_b': ('green', 'bold'),
+                  'hunk': ('magenta', ),
+                  'deleted': ('red', ),
+                  'inserted': ('green', ),
+                  'changed': ('white', ), }
+
 def colorqseries(orig, ui, repo, *dummy, **opts):
     '''run the qseries command with colored output'''
     ui.pushbuffer()
@@ -169,6 +216,7 @@
 def uisetup(ui):
     '''Initialize the extension.'''
     _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
+    _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
     if ui.config('extensions', 'hgext.mq') is not None or \
             ui.config('extensions', 'mq') is not None:
         from hgext import mq


More information about the Mercurial-devel mailing list