1
# Copyright (C) 2006 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
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.
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.
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
20
from bzrlib.builtins import cmd_diff
21
from bzrlib.patches import (hunk_from_header, InsertLine, RemoveLine,
24
from terminal import colorstring
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:])
39
class DiffWriter(object):
40
def __init__(self, target):
42
self.lp = LineParser()
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))
56
self.target.write(item)
58
self.target.write(str(item))
63
def colordiff(self, *args, **kwargs):
64
real_stdout = sys.stdout
65
sys.stdout = DiffWriter(real_stdout)
67
cmd_diff().run(*args, **kwargs)
69
sys.stdout = real_stdout