~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to colordiff.py

  • Committer: Michael Ellerman
  • Date: 2006-06-21 13:51:45 UTC
  • mto: This revision was merged to the branch mainline in revision 405.
  • Revision ID: michael@ellerman.id.au-20060621135145-8691d8247e9fcc80
Parse ~/.colordiffrc for colour information if it's there.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
import sys
 
19
from os.path import expanduser
19
20
 
20
21
from bzrlib.builtins import cmd_diff
21
22
from bzrlib.patches import (hunk_from_header, InsertLine, RemoveLine,
22
23
                            ContextLine, Hunk)
23
24
 
24
 
from terminal import colorstring
 
25
import terminal
25
26
 
26
27
class LineParser(object):
27
28
    def parse_line(self, line):
36
37
        else:
37
38
            return line
38
39
 
 
40
 
39
41
class DiffWriter(object):
 
42
 
 
43
    colors = {
 
44
        'modline' :     'darkyellow',
 
45
        'newtext' :     'darkgreen',
 
46
        'oldtext' :     'red',
 
47
        'diffstuff' :   'darkblue'
 
48
    }
 
49
 
40
50
    def __init__(self, target):
41
51
        self.target = target
42
52
        self.lp = LineParser()
43
53
        self.chunks = []
 
54
        self._read_colordiffrc()
 
55
 
 
56
    def _read_colordiffrc(self):
 
57
        path = expanduser('~/.colordiffrc')
 
58
        try:
 
59
            f = open(path, 'r')
 
60
        except IOError:
 
61
            return
 
62
 
 
63
        for line in f.readlines():
 
64
            try:
 
65
                key, val = line.split('=')
 
66
            except ValueError:
 
67
                continue
 
68
 
 
69
            key = key.strip()
 
70
            val = val.strip()
 
71
 
 
72
            tmp = val
 
73
            if val.startswith('dark'):
 
74
                tmp = val[4:]
 
75
 
 
76
            if tmp not in terminal.colors:
 
77
                continue
 
78
 
 
79
            self.colors[key] = val
 
80
 
 
81
    def colorstring(self, type, string):
 
82
        color = self.colors[type]
 
83
        self.target.write(terminal.colorstring(str(string), color))
44
84
 
45
85
    def write(self, text):
46
86
        newstuff = text.split('\n')
52
92
    def _writeline(self, line):
53
93
        item = self.lp.parse_line(line)
54
94
        if isinstance(item, Hunk):
55
 
            self.target.write(colorstring(str(item), 'darkblue'))
 
95
            self.colorstring('diffstuff', str(item))
56
96
        elif isinstance(item, InsertLine):
57
 
            self.target.write(colorstring(str(item), 'darkgreen'))
 
97
            self.colorstring('newtext', str(item))
58
98
        elif isinstance(item, RemoveLine):
59
 
            self.target.write(colorstring(str(item), 'red'))
 
99
            self.colorstring('oldtext', str(item))
60
100
        elif isinstance(item, basestring):
61
101
            if item.startswith('==='):
62
 
                self.target.write(colorstring(item, 'darkyellow'))
 
102
                self.colorstring('modline', str(item))
63
103
            else:
64
104
                self.target.write(item)
65
105
        else: