Win32 README, build scripts
[ugit.git] / py / syntax.py
blob552b86dccbe88a92c67c2b12c8c642e301e01e76
1 #!/usr/bin/python
2 import re
3 from PyQt4.QtCore import Qt
4 from PyQt4.QtGui import QFont
5 from PyQt4.QtGui import QSyntaxHighlighter
6 from PyQt4.QtGui import QTextCharFormat
8 BEGIN = 0
9 ADD = 1
10 REMOVE = 2
11 TEXT = 3
13 class GitSyntaxHighlighter (QSyntaxHighlighter):
15 def __init__ (self, doc):
16 QSyntaxHighlighter.__init__ (self, doc)
18 begin = self._mkformat (QFont.Bold, Qt.cyan)
19 addition = self._mkformat (QFont.Bold, Qt.green)
20 removal = self._mkformat (QFont.Bold, Qt.red)
21 message = self._mkformat (QFont.Bold, Qt.yellow, Qt.black)
23 self._rules = (
24 ( re.compile ('^(@@|\+\+\+|---)'), begin ),
25 ( re.compile ('^\+'), addition ),
26 ( re.compile ('^-'), removal ),
27 ( re.compile ('^:'), message ),
30 def getFormat (self, line):
31 for regex, rule in self._rules:
32 if regex.match (line):
33 return rule
34 return None
36 def highlightBlock (self, qstr):
37 ascii = qstr.toAscii().data()
38 if not ascii: return
39 format = self.getFormat (ascii)
40 if format: self.setFormat (0, len (ascii), format)
42 def _mkformat (self, weight, color, bgcolor=None):
43 format = QTextCharFormat()
44 format.setFontWeight (weight)
45 format.setForeground (color)
46 if bgcolor: format.setBackground (bgcolor)
47 return format
50 if __name__ == '__main__':
51 import sys
52 from PyQt4 import QtCore, QtGui
54 class SyntaxTestDialog(QtGui.QDialog):
55 def __init__ (self, parent):
56 QtGui.QDialog.__init__ (self, parent)
57 self.setupUi (self)
59 def setupUi(self, CommandDialog):
60 CommandDialog.resize(QtCore.QSize(QtCore.QRect(0,0,720,512).size()).expandedTo(CommandDialog.minimumSizeHint()))
62 self.vboxlayout = QtGui.QVBoxLayout(CommandDialog)
63 self.vboxlayout.setObjectName("vboxlayout")
65 self.commandText = QtGui.QTextEdit(CommandDialog)
67 font = QtGui.QFont()
68 font.setFamily("Monospace")
69 font.setPointSize(13)
70 self.commandText.setFont(font)
71 self.commandText.setAcceptDrops(False)
72 #self.commandText.setReadOnly(True)
73 self.vboxlayout.addWidget(self.commandText)
75 GitSyntaxHighlighter (self.commandText.document())
78 app = QtGui.QApplication (sys.argv)
79 dialog = SyntaxTestDialog (app.activeWindow())
80 dialog.show()
81 dialog.exec_()