forked from OlehL/cuda_differ
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ui.py
291 lines (247 loc) · 9.77 KB
/
ui.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import os
import cudatext as ct
import cudax_lib as appx
from cudax_lib import get_translation
_ = get_translation(__file__) # I18N
class FileHistory:
items = []
section = 'recents'
def __init__(self):
self.filename = os.path.join(ct.app_path(ct.APP_DIR_SETTINGS), 'cuda_differ_history.ini')
self.max_size = appx.get_opt('ui_max_history_files', 25)
# print('Differ history max_size:', self.max_size)
def load(self):
self.items = []
for i in range(self.max_size):
fn = ct.ini_read(self.filename, self.section, str(i), '')
if not fn:
break
self.items.append(fn)
def save(self):
ct.ini_proc(ct.INI_DELETE_SECTION, self.filename, self.section, '')
for (i, item) in enumerate(self.items):
ct.ini_write(self.filename, self.section, str(i), item)
def add(self, item):
if not item:
return
if item in self.items:
self.items.remove(item)
self.items.insert(0, item)
if len(self.items) > self.max_size:
self.items = self.items[:self.max_size]
def clear(self):
self.items = []
file_history = FileHistory()
file_history.load()
def center_ct():
"""get coordinates (x, y) of center CudaText"""
x1, y1, x2, y2 = ct.app_proc(ct.PROC_COORD_WINDOW_GET, "")
x = (x1+x2)//2
y = (y1+y2)//2
return (x, y)
class DifferDialog:
def __init__(self):
self.f1 = ''
self.f2 = ''
def run(self):
global file_history
self.ready = False
open_files = []
for h in ct.ed_handles():
f = ct.Editor(h).get_filename()
if os.path.isfile(f):
open_files.append(f)
items = "\t".join(open_files+file_history.items)
self.f1 = ct.ed.get_filename()
self.f2 = ''
if ct.app_proc(ct.PROC_GET_GROUPING, '') == ct.GROUPS_ONE:
# if 2 files opened in group-1, suggest these 2 files
hh = ct.ed_handles()
if len(hh) == 2:
name1 = ct.Editor(hh[0]).get_filename()
name2 = ct.Editor(hh[1]).get_filename()
if name1 and name2:
self.f1 = name1
self.f2 = name2
else:
e1 = ct.ed_group(0)
e2 = ct.ed_group(1)
if e1 and e2:
self.f1 = e1.get_filename()
self.f2 = e2.get_filename()
dlg = self.dialog(items)
ct.dlg_proc(dlg, ct.DLG_SHOW_MODAL)
ct.dlg_proc(dlg, ct.DLG_FREE)
if self.ready:
return (self.f1, self.f2)
def dialog(self, items):
self.h = ct.dlg_proc(0, ct.DLG_CREATE)
ct.dlg_proc(self.h, ct.DLG_PROP_SET,
prop={'cap': _('Differ: Choose files...'),
'x': center_ct()[0]-275,
'y': center_ct()[1]-90,
'w': 550,
'h': 180,
'border': ct.DBORDER_DIALOG,
"keypreview": True,
'on_key_down': self.press_key
}
)
g1 = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'group')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=g1,
prop={
'name': 'g1',
'cap': _('First file:'),
'h': 60,
'a_l': ('', '['),
'a_r': ('', ']'),
'a_t': ('', '['),
'sp_a': 5
}
)
n = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'button')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=n,
prop={
'name': 'browse_1',
'cap': _('Browse...'),
'w': 80,
'a_l': None,
'a_t': ('', '['),
'a_r': ('', ']'),
'sp_a': 5,
'p': 'g1',
'tab_order': 1,
'on_change': self.open_1_file
}
)
n = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'combo')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=n,
prop={'name': 'f1_combo',
"items": items,
'val': self.f1,
'a_l': ('', '['),
'a_r': ('browse_1', '['),
'a_t': ('', '['),
'sp_a': 5,
'p': 'g1',
'tab_order': 0
}
)
g1 = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'group')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=g1,
prop={
'name': 'g2',
'cap': _('Second file:'),
'h': 60,
'a_l': ('', '['),
'a_t': ('g1', ']'),
'a_r': ('', ']'),
'sp_a': 5
}
)
n = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'button')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=n,
prop={
'name': 'browse_2',
'cap': _('Browse...'),
'w': 80,
'a_l': None,
'a_t': ('', '['),
'a_r': ('', ']'),
'sp_a': 5,
'p': 'g2',
'tab_order': 1,
'on_change': self.open_2_file
}
)
n = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'combo')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=n,
prop={'name': 'f2_combo',
"items": items,
'val': self.f2,
'a_l': ('', '['),
'a_r': ('browse_2', '['),
'a_t': ('', '['),
'sp_a': 5,
'p': 'g2',
'tab_order': 0,
}
)
n = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'button')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=n,
prop={
'name': 'cancel',
'cap': _('Cancel'),
'w': 80,
'a_t': None,
'a_b': ('', ']'),
'a_l': None,
'a_r': ('g1', ']'),
'sp_a': 5,
'tab_order': 3,
'on_change': self.press_exit
}
)
n = ct.dlg_proc(self.h, ct.DLG_CTL_ADD, 'button')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET, index=n,
prop={
'name': 'ok',
'cap': _('OK'),
'w': 80,
'a_t': None,
'a_b': ('', ']'),
'a_l': None,
'a_r': ('cancel', '['),
'sp_a': 5,
'ex0': True, # press button by Enter
'tab_order': 2,
'on_change': self.press_ok
}
)
# print(ct.dlg_proc(self.h, ct.DLG_CTL_PROP_GET, name='browse_1'))
# print(ct.dlg_proc(self.h, ct.DLG_CTL_PROP_GET, name='f1_combo'))
# print(ct.dlg_proc(self.h, ct.DLG_CTL_PROP_GET, name='browse_2'))
# print(ct.dlg_proc(self.h, ct.DLG_CTL_PROP_GET, name='f2_combo'))
return self.h
def open_1_file(self, id_dlg, id_ctl, data='', info=''):
f = ct.dlg_file(True, '', '', '')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET,
name='f1_combo',
prop={'val': f}
)
def open_2_file(self, id_dlg, id_ctl, data='', info=''):
f = ct.dlg_file(True, '', '', '')
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET,
name='f2_combo',
prop={'val': f}
)
def press_ok(self, id_dlg, id_ctl, data='', info=''):
global file_history
def set_cap(name, cap):
ct.dlg_proc(self.h, ct.DLG_CTL_PROP_SET,
name=name,
prop={'cap': cap}
)
f1_prop = ct.dlg_proc(self.h, ct.DLG_CTL_PROP_GET, name='f1_combo')
f1 = f1_prop.get('val')
if not os.path.isfile(f1):
set_cap('g1', _('First file: (Please set correct path)'))
else:
set_cap('g1', _('First file:'))
f2_prop = ct.dlg_proc(self.h, ct.DLG_CTL_PROP_GET, name='f2_combo')
f2 = f2_prop.get('val')
if not os.path.isfile(f2):
set_cap('g2', _('Second file: (Please set correct path)'))
else:
set_cap('g2', _('Second file:'))
if os.path.isfile(f1) and os.path.isfile(f2):
self.ready = True
self.f1, self.f2 = os.path.abspath(f1), os.path.abspath(f2)
file_history.add(self.f1)
file_history.add(self.f2)
file_history.save()
ct.dlg_proc(id_dlg, ct.DLG_HIDE)
def press_exit(self, id_dlg, id_ctl, data='', info=''):
ct.dlg_proc(id_dlg, ct.DLG_HIDE)
def press_key(self, id_dlg, id_ctl, data='', info=''):
pass