~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to colordiff.py

  • Committer: Aaron Bentley
  • Date: 2006-06-27 13:36:54 UTC
  • mfrom: (400.1.4 bzrtools)
  • Revision ID: abentley@panoramicfeedback.com-20060627133654-92a4107490f4f1c7
Merge support for colordiff config files

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
        'plain' :       None,
 
46
        'newtext' :     'darkgreen',
 
47
        'oldtext' :     'red',
 
48
        'diffstuff' :   'darkblue'
 
49
    }
 
50
 
40
51
    def __init__(self, target):
41
52
        self.target = target
42
53
        self.lp = LineParser()
43
54
        self.chunks = []
 
55
        self._read_colordiffrc()
 
56
 
 
57
    def _read_colordiffrc(self):
 
58
        path = expanduser('~/.colordiffrc')
 
59
        try:
 
60
            f = open(path, 'r')
 
61
        except IOError:
 
62
            return
 
63
 
 
64
        for line in f.readlines():
 
65
            try:
 
66
                key, val = line.split('=')
 
67
            except ValueError:
 
68
                continue
 
69
 
 
70
            key = key.strip()
 
71
            val = val.strip()
 
72
 
 
73
            tmp = val
 
74
            if val.startswith('dark'):
 
75
                tmp = val[4:]
 
76
 
 
77
            if tmp not in terminal.colors:
 
78
                continue
 
79
 
 
80
            self.colors[key] = val
 
81
 
 
82
    def colorstring(self, type, string):
 
83
        color = self.colors[type]
 
84
        if color is not None:
 
85
            string = terminal.colorstring(str(string), color)
 
86
        self.target.write(string)
44
87
 
45
88
    def write(self, text):
46
89
        newstuff = text.split('\n')
52
95
    def _writeline(self, line):
53
96
        item = self.lp.parse_line(line)
54
97
        if isinstance(item, Hunk):
55
 
            self.target.write(colorstring(str(item), 'blue'))
 
98
            self.colorstring('diffstuff', str(item))
56
99
        elif isinstance(item, InsertLine):
57
 
            self.target.write(colorstring(str(item), 'green'))
 
100
            self.colorstring('newtext', str(item))
58
101
        elif isinstance(item, RemoveLine):
59
 
            self.target.write(colorstring(str(item), 'red', True))
60
 
        elif isinstance(item, basestring):
61
 
            if item.startswith('==='):
62
 
                self.target.write(colorstring(item, 'yellow', False))
63
 
            else:
64
 
                self.target.write(item)
 
102
            self.colorstring('oldtext', str(item))
 
103
        elif isinstance(item, basestring) and item.startswith('==='):
 
104
            self.colorstring('modline', str(item))
65
105
        else:
66
 
            self.target.write(str(item))
 
106
            self.colorstring('plain', str(item))
67
107
 
68
108
    def flush(self):
69
109
        self.target.flush()