Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.9, 0.9.1, and 0.9.1.1

csinfo: renew embeddable changeset info widgets

Changeset 05faacea65ac

Parent f8277a149004

by Yuki KODAMA

Changes to one file · Browse files at 05faacea65ac Showing diff from parent f8277a149004 Diff from another changeset...

 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -7,49 +7,244 @@
   '''embeddable changeset summary'''   +import re  import os  import gtk    from tortoisehg.util.i18n import _ -from tortoisehg.util import hglib +from tortoisehg.util import hglib, paths    from tortoisehg.hgtk import gtklib   -def create(repo, revid, head=False): - def lbl(str, bold=True, right=True): - str = gtklib.markup_escape_text(str) - label = gtk.Label() - if bold: - str = '<b>%s</b>' % str - label.set_alignment((right and 1 or 0), 0) - label.set_markup(str) - return label - def val(str, bold=False): - return lbl(str, bold=bold, right=False) +PANEL_DEFAULT = ('rev', 'summary', 'user', 'date', 'branch', 'tags')   - # prepare data to display - table = gtklib.LayoutTable() - table.set_default_paddings(ypad=1) - ctx = repo[revid] - revstr = str(ctx.rev()) - summary = ctx.description().replace('\0', '').split('\n')[0][:80] - node = repo.lookup(revid) - tags = repo.nodetags(node) +def create(rev, style=None, repo=None, custom=None): + if style is None: + style = panelstyle() + if repo is None: + try: + rpath = paths.find_root() + repo = hg.repository(ui.ui(), path=rpath) + except hglib.RepoError: + raise _('failed to get repo: %s') % rpath + if custom is None: + custom = {} + if 'type' in style: + type = style['type'] + if type == 'panel': + if 'contents' not in style: + style['contents'] = PANEL_DEFAULT + return ChangesetPanel(rev, style, repo, custom) + elif type == 'label': + return ChangesetLabel(rev, style, repo, custom) + raise _('unknown csinfo type: %s') % type + raise _('no csinfo type specified')   - # construct gtk.Table - table.add_row(lbl(_('rev')), val(revstr)) - table.add_row(lbl(_('summary')), val(hglib.toutf(summary))) - table.add_row(lbl(_('user')), val(hglib.toutf(ctx.user()))) - table.add_row(lbl(_('date')), val(hglib.displaytime(ctx.date()))) - table.add_row(lbl(_('branch')), val(hglib.toutf(ctx.branch()))) - if tags: - table.add_row(lbl(_('tags')), val(hglib.toutf(', '.join(tags)))) - if head and node not in repo.heads(): - table.add_row(None, val(_('Not a head revision!'), bold=True)) +def factory(*base, **kbase): + def bind(*over, **kover): + args = base + over + kargs = kbase.copy() + # override base options + kargs.update(kover) + if 'style' in kargs and 'style' in kover: + # override style options + kargs['style'] = kbase['style'].copy() + kargs['style'].update(kover['style']) + return create(*args, **kargs) + return bind   - # just for padding - vbox = gtk.VBox() - vbox.pack_start(table, True, True, 3) - hbox = gtk.HBox() - hbox.pack_start(vbox, True, True, 4) - return revstr, hbox +def panelstyle(**kargs): + kargs['type'] = 'panel' + return kargs + +def labelstyle(**kargs): + kargs['type'] = 'label' + return kargs + +def custom(**kargs): + return kargs + +class ChangesetWidget(object): + + LABELS = {'rev': _('rev:'), 'revnum': _('rev:'), 'revid': _('rev:'), + 'summary': _('summary:'), 'user': _('user:'), + 'date': _('date:'), 'branch': _('branch:'), 'tags': _('tags:')} + + def __init__(self, rev, repo, custom): + self.rev = str(rev) + self.repo = repo + self.custom = custom + + def get_data(self, item): + def default_func(widget, ctx): + return None + def preset_func(widget, ctx): + if item == 'rev': + revnum = self.get_data('revnum') + revid = self.get_data('revid') + return '%s (%s)' % (revnum, revid) + elif item == 'revnum': + return str(ctx.rev()) + elif item == 'revid': + return str(ctx) + elif item == 'summary': + desc = ctx.description().replace('\0', '') + value = hglib.toutf(desc.split('\n')[0][:80]) + if len(value) == 0: + return None + return value + elif item == 'user': + return hglib.toutf(ctx.user()) + elif item == 'date': + return hglib.displaytime(ctx.date()) + elif item == 'branch': + return hglib.toutf(ctx.branch()) + elif item == 'tags': + value = hglib.toutf(', '.join(ctx.tags())) + if len(value) == 0: + return None + return value + return default_func(widget, ctx) + ctx = self.repo[self.rev] + if self.custom.has_key(item) and self.custom[item].has_key('data'): + return self.custom[item]['data'](self, ctx) + return preset_func(self, ctx) + + def get_label(self, item): + def default_func(widget): + return '' + def preset_func(widget): + try: + return self.LABELS[item] + except: + return default_func(widget) + if self.custom.has_key(item) and self.custom[item].has_key('label'): + return self.custom[item]['label'](self) + return preset_func(self) + + def get_markup(self, item): + def default_func(widget, value): + return gtklib.markup_escape_text(value) + def preset_func(widget, value): + if item in ('rev', 'revnum', 'revid'): + return gtklib.markup(value, face='monospace', size='9000') + elif item == 'branch': + return gtklib.markup(' %s ' % value, color='black', + background='#aaffaa') + elif item == 'tags': + opts = dict(color='black', background='#ffffaa') + tags = value.split(', ') + tags = [gtklib.markup(' %s ' % tag, **opts) for tag in tags] + return ' '.join(tags) + return default_func(widget, value) + value = self.get_data(item) + if value is None: + return None + if self.custom.has_key(item) and self.custom[item].has_key('markup'): + return self.custom[item]['markup'](self, value) + return preset_func(self, value) + +class ChangesetPanel(ChangesetWidget, gtk.VBox): + + def __init__(self, rev, style, repo, custom): + ChangesetWidget.__init__(self, rev, repo, custom) + gtk.VBox.__init__(self) + + self.csstyle = style + + if 'label' in style and style['label'] is not None: + label = style['label'] + assert isinstance(label, basestring) + frame = gtk.Frame(label) + self.add(frame) + vbox = gtk.VBox() + frame.add(vbox) + else: + vbox = self + + if 'margin' in style: + margin = style['margin'] + # 'border' range is 0-65535 + assert isinstance(margin, (int, long)) + assert 0 <= margin and margin <= 65535 + self.set_border_width(margin) + + table = gtklib.LayoutTable(ypad=1, headopts={'weight': 'bold'}) + vbox.add(table) + + if 'padding' in style: + padding = style['padding'] + # 'border' range is 0-65535 + assert isinstance(padding, (int, long)) + assert 0 <= padding and padding <= 65535 + table.set_border_width(padding) + + contents = style.get('contents', None) + assert contents + + # build info + for item in contents: + text = self.get_markup(item) + if text: + body = gtk.Label() + body.set_markup(text) + table.add_row(self.get_label(item), body) + +class ChangesetLabel(ChangesetWidget, gtk.Label): + + def __init__(self, rev, style, repo, custom): + ChangesetWidget.__init__(self, rev, repo, custom) + gtk.Label.__init__(self) + + self.set_alignment(0, 0.5) + self.csstyle = style + + self.update(rev) + + def update(self, rev=None, style=None, repo=None): + if rev is None: + rev = self.rev + else: + self.rev = str(rev) + if repo is None: + repo = self.repo + else: + self.repo = repo + if style is None: + style = self.csstyle + else: + self.csstyle = style + + if 'selectable' in style: + sel = style['selectable'] + assert isinstance(sel, bool) + self.set_selectable(sel) + + if 'width' in style or 'height' in style: + width = style.get('width', -1) + assert isinstance(width, (int, long)) + height = style.get('height', -1) + assert isinstance(height, (int, long)) + self.set_size_request(width, height) + self.set_line_wrap(True) + + contents = style.get('contents', None) + assert contents + + # build info + info = '' + pat = re.compile(r'(?:(?<=%%)|(?<!%)%\()(\w+)(?:\)s)') + for snip in contents: + # extract all placeholders + items = pat.findall(snip) + # fetch required data + data = {} + for item in items: + text = self.get_markup(item) + if text: + data[item] = text + if len(data) == 0: + continue + # insert data & append to label + info += snip % data + self.set_markup(info)