Skip to content

Instantly share code, notes, and snippets.

@scottferg
Created October 9, 2009 22:01
Show Gist options
  • Save scottferg/206387 to your computer and use it in GitHub Desktop.
Save scottferg/206387 to your computer and use it in GitHub Desktop.
def _getFormattedDiffBuffer(self, commit):
'''
Formats a commit diff message with proper styling
'''
textBuffer = gtk.TextBuffer()
textBuffer.set_text(commit)
startIter, endIter = textBuffer.get_bounds()
# Setup text formatting tags
tagTable = textBuffer.get_tag_table()
defaultTag = gtk.TextTag('default')
defaultTag.set_property('font', 'Courier')
defaultTag.set_property('foreground', '#003399')
defaultTag.set_property('background', '#ffffff')
addTag = gtk.TextTag('add')
addTag.set_property('font', 'Courier')
addTag.set_property('foreground', '#666600')
addTag.set_property('background', '#99ff66')
removeTag = gtk.TextTag('remove')
removeTag.set_property('font', 'Courier')
removeTag.set_property('foreground', '#cc0033')
removeTag.set_property('background', '#ffcccc')
patchTag = gtk.TextTag('patch')
patchTag.set_property('font', 'Courier')
patchTag.set_property('foreground', '#ffffff')
patchTag.set_property('background', '#003399')
fileTag = gtk.TextTag('file')
fileTag.set_property('font', 'Courier')
fileTag.set_property('foreground', '#ffffff')
fileTag.set_property('background', '#cc6600')
tagTable.add(defaultTag)
tagTable.add(addTag)
tagTable.add(removeTag)
tagTable.add(patchTag)
tagTable.add(fileTag)
textBuffer.apply_tag(defaultTag, startIter, endIter)
# Read each line and style it accordingly
lineCount = textBuffer.get_line_count()
count = 0
while (count < lineCount):
count += 1
currentIter = textBuffer.get_iter_at_line(count)
try:
currentEndIter = textBuffer.get_iter_at_line(count + 1)
if currentIter.get_text(currentEndIter)[0:1] == '+':
textBuffer.apply_tag(addTag, currentIter, currentEndIter)
elif currentIter.get_text(currentEndIter)[0:1] == '-':
textBuffer.apply_tag(removeTag, currentIter, currentEndIter)
elif currentIter.get_text(currentEndIter)[0:2] == '@@':
textBuffer.apply_tag(patchTag, currentIter, currentEndIter)
elif currentIter.get_text(currentEndIter)[0:4] == 'diff':
nextLineIter = textBuffer.get_iter_at_line(count + 2)
textBuffer.apply_tag(fileTag, currentIter, nextLineIter)
except TypeError:
pass
return textBuffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment