/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: Adeodato Simó
  • Date: 2007-07-04 16:57:07 UTC
  • mto: This revision was merged to the branch mainline in revision 234.
  • Revision ID: dato@net.com.org.es-20070704165707-2w5sb6l8c8zis7f6
Read ~/.colordiffrc to set colors in the diff window.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
import gtk
16
16
import pango
 
17
import os
 
18
import re
17
19
import sys
18
20
 
19
21
try:
26
28
 
27
29
from bzrlib.diff import show_diff_trees
28
30
from bzrlib.errors import NoSuchFile
 
31
from bzrlib.trace import warning
29
32
 
30
33
 
31
34
class DiffWindow(gtk.Window):
92
95
            self.buffer = gtksourceview.SourceBuffer()
93
96
            slm = gtksourceview.SourceLanguagesManager()
94
97
            gsl = slm.get_language_from_mime_type("text/x-patch")
 
98
            self.apply_colordiffrc(gsl)
95
99
            self.buffer.set_language(gsl)
96
100
            self.buffer.set_highlight(True)
97
101
 
167
171
        s = StringIO()
168
172
        show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
169
173
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
 
174
 
 
175
    @staticmethod
 
176
    def apply_colordiffrc(lang):
 
177
        """Set style colors in lang to that specified in colordiff config file.
 
178
 
 
179
        Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
 
180
 
 
181
        :param lang: a gtksourceview.SourceLanguage object.
 
182
        """
 
183
        def parse_colordiffrc(fileobj):
 
184
            """Parse fileobj as a colordiff configuration file.
 
185
            
 
186
            :return: A dict with the key -> value pairs.
 
187
            """
 
188
            colors = {}
 
189
            for line in fileobj:
 
190
                if re.match(r'^\s*#', line):
 
191
                    continue
 
192
                key, val = line.split('=')
 
193
                colors[key.strip()] = val.strip()
 
194
            return colors
 
195
 
 
196
        colors = {}
 
197
 
 
198
        for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
 
199
            f = os.path.expanduser(f)
 
200
            if os.path.exists(f):
 
201
                try:
 
202
                    f = file(f)
 
203
                except IOError, e:
 
204
                    warning('could not open file %s: %s' % (f, str(e)))
 
205
                else:
 
206
                    colors.update(parse_colordiffrc(f))
 
207
                    f.close()
 
208
 
 
209
        if not colors:
 
210
            # ~/.colordiffrc does not exist
 
211
            return
 
212
 
 
213
        mapping = {
 
214
                # map GtkSourceView tags to colordiff names
 
215
                # since GSV is richer, accept new names for extra bits,
 
216
                # defaulting to old names if they're not present
 
217
                'Added@32@line': ['newtext'],
 
218
                'Removed@32@line': ['oldtext'],
 
219
                'Location': ['location', 'diffstuff'],
 
220
                'Diff@32@file': ['file', 'diffstuff'],
 
221
                'Special@32@case': ['specialcase', 'diffstuff'],
 
222
        }
 
223
 
 
224
        for tag in lang.get_tags():
 
225
            tag_id = tag.get_id()
 
226
            keys = mapping.get(tag_id, [])
 
227
            color = None
 
228
 
 
229
            for key in keys:
 
230
                color = colors.get(key, None)
 
231
                if color is not None:
 
232
                    break
 
233
 
 
234
            if color is None:
 
235
                continue
 
236
 
 
237
            style = gtksourceview.SourceTagStyle()
 
238
            try:
 
239
                style.foreground = gtk.gdk.color_parse(color)
 
240
            except ValueError:
 
241
                warning('not a valid color: %s' % color)
 
242
            else:
 
243
                lang.set_tag_style(tag_id, style)