forked from Tinkerforge/generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_view.py
executable file
·150 lines (108 loc) · 3.99 KB
/
diff_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
if sys.hexversion < 0x3040000:
print('Python >= 3.4 required')
sys.exit(1)
import os
import argparse
os.system('pyuic5 -o ui_diff_view.py diff_view.ui')
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont
from ui_diff_view import Ui_DiffView
class DiffHighlighter(QSyntaxHighlighter):
def highlightBlock(self, text):
if text.startswith('-'):
self.setFormat(0, len(text), Qt.red)
elif text.startswith('+'):
self.setFormat(0, len(text), Qt.darkGreen)
elif text.startswith('@'):
self.setFormat(0, len(text), Qt.darkCyan)
elif not text.startswith(' '):
self.setFormat(0, len(text), Qt.darkBlue)
class DiffBlock:
def __init__(self):
self.kind = None
self.text = None
self.visible = True
class DiffView(QMainWindow, Ui_DiffView):
def __init__(self, path):
super().__init__()
self.setupUi(self)
self.setWindowTitle(path +' - DiffView')
self.path = path
self.highlighter = DiffHighlighter(self.edit_diff.document())
self.blocks = []
with open(path, 'r') as f:
block = DiffBlock()
lines = []
for line in f.readlines():
if block.kind == 'hunk':
if line[0] not in [' ', '-', '+']:
if block.kind != None:
block.text = ''.join(lines)
self.blocks.append(block)
block = DiffBlock()
lines = []
if block.kind == 'meta':
if line[0] in ['@']:
if block.kind != None:
block.text = ''.join(lines)
self.blocks.append(block)
block = DiffBlock()
lines = []
if block.kind == None:
if line.startswith('@'):
block.kind = 'hunk'
else:
block.kind = 'meta'
lines.append(line)
self.update_edit()
self.button_hide_selection.clicked.connect(self.hide_selection)
self.button_overwrite_file.clicked.connect(self.overwrite_file)
def update_edit(self):
total = 0
visible = 0
text = ''
for block in self.blocks:
total += 1
if block.visible:
visible += 1
text += block.text
self.edit_diff.setPlainText(text)
self.label_status.setText('{0} / {1}'.format(visible, total))
def hide_selection(self):
text = self.edit_diff.textCursor().selectedText().replace('\u2029', '\n')
for block in self.blocks:
if block.kind == 'hunk' and text in block.text:
block.visible = False
if self.check_hide_empty_meta.isChecked():
meta = None
visible = False
for block in self.blocks:
if block.kind == 'meta':
if meta != None:
meta.visible = visible
meta = block
visible = False
elif block.kind == 'hunk':
if block.visible:
visible = True
if meta != None:
meta.visible = visible
self.update_edit()
def overwrite_file(self):
with open(self.path + '.tmp', 'w') as f:
f.write(self.edit_diff.document().toPlainText())
os.rename(self.path + '.tmp', self.path)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('patch')
args = parser.parse_args()
app = QApplication([])
window = DiffView(args.patch)
window.show()
return app.exec_()
if __name__ == '__main__':
sys.exit(main())