~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to colordiff.py

  • Committer: Aaron Bentley
  • Date: 2006-06-18 02:57:01 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060618025701-0aeb369dff5c6c22
Implement cdiff (based on old Fai code)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Aaron Bentley
 
2
# <aaron.bentley@utoronto.ca>
 
3
#
 
4
#    This program is free software; you can redistribute it and/or modify
 
5
#    it under the terms of the GNU General Public License as published by
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
7
#    (at your option) any later version.
 
8
#
 
9
#    This program is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#    GNU General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU General Public License
 
15
#    along with this program; if not, write to the Free Software
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
import sys
 
19
 
 
20
from bzrlib.builtins import cmd_diff
 
21
from bzrlib.patches import (hunk_from_header, InsertLine, RemoveLine,
 
22
                            ContextLine, Hunk)
 
23
 
 
24
from terminal import colorstring
 
25
 
 
26
class LineParser(object):
 
27
    def parse_line(self, line):
 
28
        if line.startswith("@"):
 
29
            return hunk_from_header(line)
 
30
        elif line.startswith("+"):
 
31
            return InsertLine(line[1:])
 
32
        elif line.startswith("-"):
 
33
            return RemoveLine(line[1:])
 
34
        elif line.startswith(" "):
 
35
            return ContextLine(line[1:])
 
36
        else:
 
37
            return line
 
38
 
 
39
class DiffWriter(object):
 
40
    def __init__(self, target):
 
41
        self.target = target
 
42
        self.lp = LineParser()
 
43
 
 
44
    def write(self, text):
 
45
        item = self.lp.parse_line(text)
 
46
        if isinstance(item, Hunk):
 
47
            self.target.write(colorstring(str(item), 'blue'))
 
48
        elif isinstance(item, InsertLine):
 
49
            self.target.write(colorstring(str(item), 'green'))
 
50
        elif isinstance(item, RemoveLine):
 
51
            self.target.write(colorstring(str(item), 'red', True))
 
52
        elif isinstance(item, basestring):
 
53
            if item.startswith('==='):
 
54
                self.target.write(colorstring(item, 'yellow', False))
 
55
            else:
 
56
                self.target.write(item)
 
57
        else:
 
58
            self.target.write(str(item))
 
59
 
 
60
    def flush(self):
 
61
        self.target.flush()
 
62
 
 
63
def colordiff(self, *args, **kwargs):
 
64
    real_stdout = sys.stdout
 
65
    sys.stdout = DiffWriter(real_stdout)
 
66
    try:
 
67
        cmd_diff().run(*args, **kwargs)
 
68
    finally:
 
69
        sys.stdout = real_stdout