[diffoscope] 02/02: Avoid shelling out to colordiff by implementing color in Python directly.

Chris Lamb chris at chris-lamb.co.uk
Sat Dec 3 11:44:59 CET 2016


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit dea5a1bf4af893ae657d5c106ea7df6c0bbc78fe
Author: Chris Lamb <lamby at debian.org>
Date:   Sat Dec 3 10:44:25 2016 +0000

    Avoid shelling out to colordiff by implementing color in Python directly.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/difference.py | 18 +++++++++++-------
 diffoscope/exc.py        |  4 ----
 diffoscope/main.py       |  5 ++---
 3 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index edbc5f9..76a02a0 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -450,13 +450,17 @@ def reverse_unified_diff(diff):
             res.append(line)
     return ''.join(res)
 
+re_diff_change = re.compile(r'^([+-@]).*', re.MULTILINE)
 
- at tool_required('colordiff')
 def color_unified_diff(diff):
-    with subprocess.Popen(["colordiff"],
-                          stdin=subprocess.PIPE,
-                          stdout=subprocess.PIPE,
-                          universal_newlines=True) as proc:
-        diff_output, _ = proc.communicate(diff)
-        return diff_output
+    RESET = '\033[0m'
+    RED, GREEN, CYAN = '\033[31m', '\033[32m', '\033[0;36m'
 
+    def repl(m):
+        return '{}{}{}'.format({
+            '-': RED,
+            '@': CYAN,
+            '+': GREEN,
+        }[m.group(1)], m.group(0), RESET)
+
+    return re_diff_change.sub(repl, diff)
diff --git a/diffoscope/exc.py b/diffoscope/exc.py
index 4cdea92..116e813 100644
--- a/diffoscope/exc.py
+++ b/diffoscope/exc.py
@@ -40,10 +40,6 @@ class RequiredToolNotFound(Exception):
             'debian': 'diffutils',
             'arch': 'diffutils',
         },
-        'colordiff': {
-            'debian': 'colordiff',
-            'arch': 'colordiff',
-        },
         'cpio': {
             'debian': 'cpio',
             'arch': 'cpio',
diff --git a/diffoscope/main.py b/diffoscope/main.py
index a8256da..4275d76 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -75,9 +75,8 @@ def create_parser():
                         help='Write plain text output to given file (use - for stdout)')
     group1.add_argument('--text-color', metavar='WHEN', default='auto',
                         choices=['never', 'auto', 'always'],
-                        help='When to output color diff, using colordiff(1). '
-                        'WHEN is one of {%(choices)s}. Default: auto, meaning '
-                        'yes if the output is a terminal, otherwise no.')
+                        help='When to output color diff. WHEN is one of {%(choices)s}. '
+                        'Default: auto, meaning yes if the output is a terminal, otherwise no.')
     group1.add_argument('--output-empty', action='store_true',
                         help='If there was no difference, then output an empty '
                         'diff for each output type that was specified. (For '

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list