Show More
@@ -1,574 +1,574 b'' | |||
|
1 | 1 | # utility for color output for Mercurial commands |
|
2 | 2 | # |
|
3 | 3 | # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> and other |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import re |
|
11 | 11 | |
|
12 | 12 | from .i18n import _ |
|
13 | 13 | from .pycompat import getattr |
|
14 | 14 | |
|
15 | 15 | from . import ( |
|
16 | 16 | encoding, |
|
17 | 17 | pycompat, |
|
18 | 18 | ) |
|
19 | 19 | |
|
20 | 20 | from .utils import stringutil |
|
21 | 21 | |
|
22 | 22 | try: |
|
23 | 23 | import curses |
|
24 | 24 | |
|
25 | 25 | # Mapping from effect name to terminfo attribute name (or raw code) or |
|
26 | 26 | # color number. This will also force-load the curses module. |
|
27 | 27 | _baseterminfoparams = { |
|
28 | 28 | b'none': (True, b'sgr0', b''), |
|
29 | 29 | b'standout': (True, b'smso', b''), |
|
30 | 30 | b'underline': (True, b'smul', b''), |
|
31 | 31 | b'reverse': (True, b'rev', b''), |
|
32 | 32 | b'inverse': (True, b'rev', b''), |
|
33 | 33 | b'blink': (True, b'blink', b''), |
|
34 | 34 | b'dim': (True, b'dim', b''), |
|
35 | 35 | b'bold': (True, b'bold', b''), |
|
36 | 36 | b'invisible': (True, b'invis', b''), |
|
37 | 37 | b'italic': (True, b'sitm', b''), |
|
38 | 38 | b'black': (False, curses.COLOR_BLACK, b''), |
|
39 | 39 | b'red': (False, curses.COLOR_RED, b''), |
|
40 | 40 | b'green': (False, curses.COLOR_GREEN, b''), |
|
41 | 41 | b'yellow': (False, curses.COLOR_YELLOW, b''), |
|
42 | 42 | b'blue': (False, curses.COLOR_BLUE, b''), |
|
43 | 43 | b'magenta': (False, curses.COLOR_MAGENTA, b''), |
|
44 | 44 | b'cyan': (False, curses.COLOR_CYAN, b''), |
|
45 | 45 | b'white': (False, curses.COLOR_WHITE, b''), |
|
46 | 46 | } |
|
47 | 47 | except ImportError: |
|
48 | 48 | curses = None |
|
49 | 49 | _baseterminfoparams = {} |
|
50 | 50 | |
|
51 | 51 | # start and stop parameters for effects |
|
52 | 52 | _effects = { |
|
53 | 53 | b'none': 0, |
|
54 | 54 | b'black': 30, |
|
55 | 55 | b'red': 31, |
|
56 | 56 | b'green': 32, |
|
57 | 57 | b'yellow': 33, |
|
58 | 58 | b'blue': 34, |
|
59 | 59 | b'magenta': 35, |
|
60 | 60 | b'cyan': 36, |
|
61 | 61 | b'white': 37, |
|
62 | 62 | b'bold': 1, |
|
63 | 63 | b'italic': 3, |
|
64 | 64 | b'underline': 4, |
|
65 | 65 | b'inverse': 7, |
|
66 | 66 | b'dim': 2, |
|
67 | 67 | b'black_background': 40, |
|
68 | 68 | b'red_background': 41, |
|
69 | 69 | b'green_background': 42, |
|
70 | 70 | b'yellow_background': 43, |
|
71 | 71 | b'blue_background': 44, |
|
72 | 72 | b'purple_background': 45, |
|
73 | 73 | b'cyan_background': 46, |
|
74 | 74 | b'white_background': 47, |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | _defaultstyles = { |
|
78 | 78 | b'grep.match': b'red bold', |
|
79 | 79 | b'grep.linenumber': b'green', |
|
80 | 80 | b'grep.rev': b'blue', |
|
81 | 81 | b'grep.sep': b'cyan', |
|
82 | 82 | b'grep.filename': b'magenta', |
|
83 | 83 | b'grep.user': b'magenta', |
|
84 | 84 | b'grep.date': b'magenta', |
|
85 | 85 | b'grep.inserted': b'green bold', |
|
86 | 86 | b'grep.deleted': b'red bold', |
|
87 | 87 | b'bookmarks.active': b'green', |
|
88 | 88 | b'branches.active': b'none', |
|
89 | 89 | b'branches.closed': b'black bold', |
|
90 | 90 | b'branches.current': b'green', |
|
91 | 91 | b'branches.inactive': b'none', |
|
92 | 92 | b'diff.changed': b'white', |
|
93 | 93 | b'diff.deleted': b'red', |
|
94 | 94 | b'diff.deleted.changed': b'red bold underline', |
|
95 | 95 | b'diff.deleted.unchanged': b'red', |
|
96 | 96 | b'diff.diffline': b'bold', |
|
97 | 97 | b'diff.extended': b'cyan bold', |
|
98 | 98 | b'diff.file_a': b'red bold', |
|
99 | 99 | b'diff.file_b': b'green bold', |
|
100 | 100 | b'diff.hunk': b'magenta', |
|
101 | 101 | b'diff.inserted': b'green', |
|
102 | 102 | b'diff.inserted.changed': b'green bold underline', |
|
103 | 103 | b'diff.inserted.unchanged': b'green', |
|
104 | 104 | b'diff.tab': b'', |
|
105 | 105 | b'diff.trailingwhitespace': b'bold red_background', |
|
106 | 106 | b'changeset.public': b'', |
|
107 | 107 | b'changeset.draft': b'', |
|
108 | 108 | b'changeset.secret': b'', |
|
109 | 109 | b'diffstat.deleted': b'red', |
|
110 | 110 | b'diffstat.inserted': b'green', |
|
111 | 111 | b'formatvariant.name.mismatchconfig': b'red', |
|
112 | 112 | b'formatvariant.name.mismatchdefault': b'yellow', |
|
113 | 113 | b'formatvariant.name.uptodate': b'green', |
|
114 | 114 | b'formatvariant.repo.mismatchconfig': b'red', |
|
115 | 115 | b'formatvariant.repo.mismatchdefault': b'yellow', |
|
116 | 116 | b'formatvariant.repo.uptodate': b'green', |
|
117 | 117 | b'formatvariant.config.special': b'yellow', |
|
118 | 118 | b'formatvariant.config.default': b'green', |
|
119 | 119 | b'formatvariant.default': b'', |
|
120 | 120 | b'histedit.remaining': b'red bold', |
|
121 | 121 | b'ui.addremove.added': b'green', |
|
122 | 122 | b'ui.addremove.removed': b'red', |
|
123 | 123 | b'ui.error': b'red', |
|
124 | 124 | b'ui.prompt': b'yellow', |
|
125 | 125 | b'log.changeset': b'yellow', |
|
126 | 126 | b'patchbomb.finalsummary': b'', |
|
127 | 127 | b'patchbomb.from': b'magenta', |
|
128 | 128 | b'patchbomb.to': b'cyan', |
|
129 | 129 | b'patchbomb.subject': b'green', |
|
130 | 130 | b'patchbomb.diffstats': b'', |
|
131 | 131 | b'rebase.rebased': b'blue', |
|
132 | 132 | b'rebase.remaining': b'red bold', |
|
133 | 133 | b'resolve.resolved': b'green bold', |
|
134 | 134 | b'resolve.unresolved': b'red bold', |
|
135 | 135 | b'shelve.age': b'cyan', |
|
136 | 136 | b'shelve.newest': b'green bold', |
|
137 | 137 | b'shelve.name': b'blue bold', |
|
138 | 138 | b'status.added': b'green bold', |
|
139 | 139 | b'status.clean': b'none', |
|
140 | 140 | b'status.copied': b'none', |
|
141 | 141 | b'status.deleted': b'cyan bold underline', |
|
142 | 142 | b'status.ignored': b'black bold', |
|
143 | 143 | b'status.modified': b'blue bold', |
|
144 | 144 | b'status.removed': b'red bold', |
|
145 | 145 | b'status.unknown': b'magenta bold underline', |
|
146 | 146 | b'tags.normal': b'green', |
|
147 | 147 | b'tags.local': b'black bold', |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | |
|
151 | 151 | def loadcolortable(ui, extname, colortable): |
|
152 | 152 | _defaultstyles.update(colortable) |
|
153 | 153 | |
|
154 | 154 | |
|
155 | 155 | def _terminfosetup(ui, mode, formatted): |
|
156 | 156 | '''Initialize terminfo data and the terminal if we're in terminfo mode.''' |
|
157 | 157 | |
|
158 | 158 | # If we failed to load curses, we go ahead and return. |
|
159 | 159 | if curses is None: |
|
160 | 160 | return |
|
161 | 161 | # Otherwise, see what the config file says. |
|
162 | 162 | if mode not in (b'auto', b'terminfo'): |
|
163 | 163 | return |
|
164 | 164 | ui._terminfoparams.update(_baseterminfoparams) |
|
165 | 165 | |
|
166 | 166 | for key, val in ui.configitems(b'color'): |
|
167 | 167 | if key.startswith(b'color.'): |
|
168 | 168 | newval = (False, int(val), b'') |
|
169 | 169 | ui._terminfoparams[key[6:]] = newval |
|
170 | 170 | elif key.startswith(b'terminfo.'): |
|
171 | 171 | newval = (True, b'', val.replace(b'\\E', b'\x1b')) |
|
172 | 172 | ui._terminfoparams[key[9:]] = newval |
|
173 | 173 | try: |
|
174 | 174 | curses.setupterm() |
|
175 | 175 | except curses.error: |
|
176 | 176 | ui._terminfoparams.clear() |
|
177 | 177 | return |
|
178 | 178 | |
|
179 | 179 | for key, (b, e, c) in ui._terminfoparams.copy().items(): |
|
180 | 180 | if not b: |
|
181 | 181 | continue |
|
182 | 182 | if not c and not curses.tigetstr(pycompat.sysstr(e)): |
|
183 | 183 | # Most terminals don't support dim, invis, etc, so don't be |
|
184 | 184 | # noisy and use ui.debug(). |
|
185 | 185 | ui.debug(b"no terminfo entry for %s\n" % e) |
|
186 | 186 | del ui._terminfoparams[key] |
|
187 | 187 | if not curses.tigetstr(r'setaf') or not curses.tigetstr(r'setab'): |
|
188 | 188 | # Only warn about missing terminfo entries if we explicitly asked for |
|
189 | 189 | # terminfo mode and we're in a formatted terminal. |
|
190 | 190 | if mode == b"terminfo" and formatted: |
|
191 | 191 | ui.warn( |
|
192 | 192 | _( |
|
193 | 193 | b"no terminfo entry for setab/setaf: reverting to " |
|
194 | 194 | b"ECMA-48 color\n" |
|
195 | 195 | ) |
|
196 | 196 | ) |
|
197 | 197 | ui._terminfoparams.clear() |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | def setup(ui): |
|
201 | 201 | """configure color on a ui |
|
202 | 202 | |
|
203 | 203 | That function both set the colormode for the ui object and read |
|
204 | 204 | the configuration looking for custom colors and effect definitions.""" |
|
205 | 205 | mode = _modesetup(ui) |
|
206 | 206 | ui._colormode = mode |
|
207 | 207 | if mode and mode != b'debug': |
|
208 | 208 | configstyles(ui) |
|
209 | 209 | |
|
210 | 210 | |
|
211 | 211 | def _modesetup(ui): |
|
212 | 212 | if ui.plain(b'color'): |
|
213 | 213 | return None |
|
214 | 214 | config = ui.config(b'ui', b'color') |
|
215 | 215 | if config == b'debug': |
|
216 | 216 | return b'debug' |
|
217 | 217 | |
|
218 | 218 | auto = config == b'auto' |
|
219 | 219 | always = False |
|
220 | 220 | if not auto and stringutil.parsebool(config): |
|
221 | 221 | # We want the config to behave like a boolean, "on" is actually auto, |
|
222 | 222 | # but "always" value is treated as a special case to reduce confusion. |
|
223 | 223 | if ( |
|
224 | 224 | ui.configsource(b'ui', b'color') == b'--color' |
|
225 | 225 | or config == b'always' |
|
226 | 226 | ): |
|
227 | 227 | always = True |
|
228 | 228 | else: |
|
229 | 229 | auto = True |
|
230 | 230 | |
|
231 | 231 | if not always and not auto: |
|
232 | 232 | return None |
|
233 | 233 | |
|
234 | 234 | formatted = always or ( |
|
235 | 235 | encoding.environ.get(b'TERM') != b'dumb' and ui.formatted() |
|
236 | 236 | ) |
|
237 | 237 | |
|
238 | 238 | mode = ui.config(b'color', b'mode') |
|
239 | 239 | |
|
240 | 240 | # If pager is active, color.pagermode overrides color.mode. |
|
241 | 241 | if getattr(ui, 'pageractive', False): |
|
242 | 242 | mode = ui.config(b'color', b'pagermode', mode) |
|
243 | 243 | |
|
244 | 244 | realmode = mode |
|
245 | 245 | if pycompat.iswindows: |
|
246 | 246 | from . import win32 |
|
247 | 247 | |
|
248 | 248 | term = encoding.environ.get(b'TERM') |
|
249 | 249 | # TERM won't be defined in a vanilla cmd.exe environment. |
|
250 | 250 | |
|
251 | 251 | # UNIX-like environments on Windows such as Cygwin and MSYS will |
|
252 | 252 | # set TERM. They appear to make a best effort attempt at setting it |
|
253 | 253 | # to something appropriate. However, not all environments with TERM |
|
254 | 254 | # defined support ANSI. |
|
255 | 255 | ansienviron = term and b'xterm' in term |
|
256 | 256 | |
|
257 | 257 | if mode == b'auto': |
|
258 | 258 | # Since "ansi" could result in terminal gibberish, we error on the |
|
259 | 259 | # side of selecting "win32". However, if w32effects is not defined, |
|
260 | 260 | # we almost certainly don't support "win32", so don't even try. |
|
261 | 261 | # w32effects is not populated when stdout is redirected, so checking |
|
262 | 262 | # it first avoids win32 calls in a state known to error out. |
|
263 | 263 | if ansienviron or not w32effects or win32.enablevtmode(): |
|
264 | 264 | realmode = b'ansi' |
|
265 | 265 | else: |
|
266 | 266 | realmode = b'win32' |
|
267 | 267 | # An empty w32effects is a clue that stdout is redirected, and thus |
|
268 | 268 | # cannot enable VT mode. |
|
269 | 269 | elif mode == b'ansi' and w32effects and not ansienviron: |
|
270 | 270 | win32.enablevtmode() |
|
271 | 271 | elif mode == b'auto': |
|
272 | 272 | realmode = b'ansi' |
|
273 | 273 | |
|
274 | 274 | def modewarn(): |
|
275 | 275 | # only warn if color.mode was explicitly set and we're in |
|
276 | 276 | # a formatted terminal |
|
277 | 277 | if mode == realmode and formatted: |
|
278 | 278 | ui.warn(_(b'warning: failed to set color mode to %s\n') % mode) |
|
279 | 279 | |
|
280 | 280 | if realmode == b'win32': |
|
281 | 281 | ui._terminfoparams.clear() |
|
282 | 282 | if not w32effects: |
|
283 | 283 | modewarn() |
|
284 | 284 | return None |
|
285 | 285 | elif realmode == b'ansi': |
|
286 | 286 | ui._terminfoparams.clear() |
|
287 | 287 | elif realmode == b'terminfo': |
|
288 | 288 | _terminfosetup(ui, mode, formatted) |
|
289 | 289 | if not ui._terminfoparams: |
|
290 | 290 | ## FIXME Shouldn't we return None in this case too? |
|
291 | 291 | modewarn() |
|
292 | 292 | realmode = b'ansi' |
|
293 | 293 | else: |
|
294 | 294 | return None |
|
295 | 295 | |
|
296 | 296 | if always or (auto and formatted): |
|
297 | 297 | return realmode |
|
298 | 298 | return None |
|
299 | 299 | |
|
300 | 300 | |
|
301 | 301 | def configstyles(ui): |
|
302 | 302 | ui._styles.update(_defaultstyles) |
|
303 | 303 | for status, cfgeffects in ui.configitems(b'color'): |
|
304 | 304 | if b'.' not in status or status.startswith((b'color.', b'terminfo.')): |
|
305 | 305 | continue |
|
306 | 306 | cfgeffects = ui.configlist(b'color', status) |
|
307 | 307 | if cfgeffects: |
|
308 | 308 | good = [] |
|
309 | 309 | for e in cfgeffects: |
|
310 | 310 | if valideffect(ui, e): |
|
311 | 311 | good.append(e) |
|
312 | 312 | else: |
|
313 | 313 | ui.warn( |
|
314 | 314 | _( |
|
315 | 315 | b"ignoring unknown color/effect %s " |
|
316 | 316 | b"(configured in color.%s)\n" |
|
317 | 317 | ) |
|
318 | 318 | % (stringutil.pprint(e), status) |
|
319 | 319 | ) |
|
320 | 320 | ui._styles[status] = b' '.join(good) |
|
321 | 321 | |
|
322 | 322 | |
|
323 | 323 | def _activeeffects(ui): |
|
324 | 324 | '''Return the effects map for the color mode set on the ui.''' |
|
325 | 325 | if ui._colormode == b'win32': |
|
326 | 326 | return w32effects |
|
327 | 327 | elif ui._colormode is not None: |
|
328 | 328 | return _effects |
|
329 | 329 | return {} |
|
330 | 330 | |
|
331 | 331 | |
|
332 | 332 | def valideffect(ui, effect): |
|
333 | 333 | b'Determine if the effect is valid or not.' |
|
334 | 334 | return (not ui._terminfoparams and effect in _activeeffects(ui)) or ( |
|
335 | 335 | effect in ui._terminfoparams or effect[:-11] in ui._terminfoparams |
|
336 | 336 | ) |
|
337 | 337 | |
|
338 | 338 | |
|
339 | 339 | def _effect_str(ui, effect): |
|
340 | 340 | '''Helper function for render_effects().''' |
|
341 | 341 | |
|
342 | 342 | bg = False |
|
343 | 343 | if effect.endswith(b'_background'): |
|
344 | 344 | bg = True |
|
345 | 345 | effect = effect[:-11] |
|
346 | 346 | try: |
|
347 | 347 | attr, val, termcode = ui._terminfoparams[effect] |
|
348 | 348 | except KeyError: |
|
349 | 349 | return b'' |
|
350 | 350 | if attr: |
|
351 | 351 | if termcode: |
|
352 | 352 | return termcode |
|
353 | 353 | else: |
|
354 | 354 | return curses.tigetstr(pycompat.sysstr(val)) |
|
355 | 355 | elif bg: |
|
356 | 356 | return curses.tparm(curses.tigetstr(r'setab'), val) |
|
357 | 357 | else: |
|
358 | 358 | return curses.tparm(curses.tigetstr(r'setaf'), val) |
|
359 | 359 | |
|
360 | 360 | |
|
361 | 361 | def _mergeeffects(text, start, stop): |
|
362 | 362 | """Insert start sequence at every occurrence of stop sequence |
|
363 | 363 | |
|
364 | 364 | >>> s = _mergeeffects(b'cyan', b'[C]', b'|') |
|
365 | 365 | >>> s = _mergeeffects(s + b'yellow', b'[Y]', b'|') |
|
366 | 366 | >>> s = _mergeeffects(b'ma' + s + b'genta', b'[M]', b'|') |
|
367 | 367 | >>> s = _mergeeffects(b'red' + s, b'[R]', b'|') |
|
368 | 368 | >>> s |
|
369 | 369 | '[R]red[M]ma[Y][C]cyan|[R][M][Y]yellow|[R][M]genta|' |
|
370 | 370 | """ |
|
371 | 371 | parts = [] |
|
372 | 372 | for t in text.split(stop): |
|
373 | 373 | if not t: |
|
374 | 374 | continue |
|
375 | 375 | parts.extend([start, t, stop]) |
|
376 | 376 | return b''.join(parts) |
|
377 | 377 | |
|
378 | 378 | |
|
379 | 379 | def _render_effects(ui, text, effects): |
|
380 | 380 | b'Wrap text in commands to turn on each effect.' |
|
381 | 381 | if not text: |
|
382 | 382 | return text |
|
383 | 383 | if ui._terminfoparams: |
|
384 | 384 | start = b''.join( |
|
385 | 385 | _effect_str(ui, effect) for effect in [b'none'] + effects.split() |
|
386 | 386 | ) |
|
387 | 387 | stop = _effect_str(ui, b'none') |
|
388 | 388 | else: |
|
389 | 389 | activeeffects = _activeeffects(ui) |
|
390 | 390 | start = [ |
|
391 | 391 | pycompat.bytestr(activeeffects[e]) |
|
392 | 392 | for e in [b'none'] + effects.split() |
|
393 | 393 | ] |
|
394 | 394 | start = b'\033[' + b';'.join(start) + b'm' |
|
395 | 395 | stop = b'\033[' + pycompat.bytestr(activeeffects[b'none']) + b'm' |
|
396 | 396 | return _mergeeffects(text, start, stop) |
|
397 | 397 | |
|
398 | 398 | |
|
399 | 399 | _ansieffectre = re.compile(br'\x1b\[[0-9;]*m') |
|
400 | 400 | |
|
401 | 401 | |
|
402 | 402 | def stripeffects(text): |
|
403 | 403 | """Strip ANSI control codes which could be inserted by colorlabel()""" |
|
404 | 404 | return _ansieffectre.sub(b'', text) |
|
405 | 405 | |
|
406 | 406 | |
|
407 | 407 | def colorlabel(ui, msg, label): |
|
408 | 408 | """add color control code according to the mode""" |
|
409 | 409 | if ui._colormode == b'debug': |
|
410 | 410 | if label and msg: |
|
411 | 411 | if msg.endswith(b'\n'): |
|
412 | 412 | msg = b"[%s|%s]\n" % (label, msg[:-1]) |
|
413 | 413 | else: |
|
414 | 414 | msg = b"[%s|%s]" % (label, msg) |
|
415 | 415 | elif ui._colormode is not None: |
|
416 | 416 | effects = [] |
|
417 | 417 | for l in label.split(): |
|
418 | 418 | s = ui._styles.get(l, b'') |
|
419 | 419 | if s: |
|
420 | 420 | effects.append(s) |
|
421 | 421 | elif valideffect(ui, l): |
|
422 | 422 | effects.append(l) |
|
423 | 423 | effects = b' '.join(effects) |
|
424 | 424 | if effects: |
|
425 | 425 | msg = b'\n'.join( |
|
426 | 426 | [ |
|
427 | 427 | _render_effects(ui, line, effects) |
|
428 | 428 | for line in msg.split(b'\n') |
|
429 | 429 | ] |
|
430 | 430 | ) |
|
431 | 431 | return msg |
|
432 | 432 | |
|
433 | 433 | |
|
434 | 434 | w32effects = None |
|
435 | 435 | if pycompat.iswindows: |
|
436 | 436 | import ctypes |
|
437 | 437 | |
|
438 | _kernel32 = ctypes.windll.kernel32 | |
|
438 | _kernel32 = ctypes.windll.kernel32 # pytype: disable=module-attr | |
|
439 | 439 | |
|
440 | 440 | _WORD = ctypes.c_ushort |
|
441 | 441 | |
|
442 | 442 | _INVALID_HANDLE_VALUE = -1 |
|
443 | 443 | |
|
444 | 444 | class _COORD(ctypes.Structure): |
|
445 | 445 | _fields_ = [(r'X', ctypes.c_short), (r'Y', ctypes.c_short)] |
|
446 | 446 | |
|
447 | 447 | class _SMALL_RECT(ctypes.Structure): |
|
448 | 448 | _fields_ = [ |
|
449 | 449 | (r'Left', ctypes.c_short), |
|
450 | 450 | (r'Top', ctypes.c_short), |
|
451 | 451 | (r'Right', ctypes.c_short), |
|
452 | 452 | (r'Bottom', ctypes.c_short), |
|
453 | 453 | ] |
|
454 | 454 | |
|
455 | 455 | class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure): |
|
456 | 456 | _fields_ = [ |
|
457 | 457 | (r'dwSize', _COORD), |
|
458 | 458 | (r'dwCursorPosition', _COORD), |
|
459 | 459 | (r'wAttributes', _WORD), |
|
460 | 460 | (r'srWindow', _SMALL_RECT), |
|
461 | 461 | (r'dwMaximumWindowSize', _COORD), |
|
462 | 462 | ] |
|
463 | 463 | |
|
464 | 464 | _STD_OUTPUT_HANDLE = 0xFFFFFFF5 # (DWORD)-11 |
|
465 | 465 | _STD_ERROR_HANDLE = 0xFFFFFFF4 # (DWORD)-12 |
|
466 | 466 | |
|
467 | 467 | _FOREGROUND_BLUE = 0x0001 |
|
468 | 468 | _FOREGROUND_GREEN = 0x0002 |
|
469 | 469 | _FOREGROUND_RED = 0x0004 |
|
470 | 470 | _FOREGROUND_INTENSITY = 0x0008 |
|
471 | 471 | |
|
472 | 472 | _BACKGROUND_BLUE = 0x0010 |
|
473 | 473 | _BACKGROUND_GREEN = 0x0020 |
|
474 | 474 | _BACKGROUND_RED = 0x0040 |
|
475 | 475 | _BACKGROUND_INTENSITY = 0x0080 |
|
476 | 476 | |
|
477 | 477 | _COMMON_LVB_REVERSE_VIDEO = 0x4000 |
|
478 | 478 | _COMMON_LVB_UNDERSCORE = 0x8000 |
|
479 | 479 | |
|
480 | 480 | # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx |
|
481 | 481 | w32effects = { |
|
482 | 482 | b'none': -1, |
|
483 | 483 | b'black': 0, |
|
484 | 484 | b'red': _FOREGROUND_RED, |
|
485 | 485 | b'green': _FOREGROUND_GREEN, |
|
486 | 486 | b'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN, |
|
487 | 487 | b'blue': _FOREGROUND_BLUE, |
|
488 | 488 | b'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED, |
|
489 | 489 | b'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN, |
|
490 | 490 | b'white': _FOREGROUND_RED | _FOREGROUND_GREEN | _FOREGROUND_BLUE, |
|
491 | 491 | b'bold': _FOREGROUND_INTENSITY, |
|
492 | 492 | b'black_background': 0x100, # unused value > 0x0f |
|
493 | 493 | b'red_background': _BACKGROUND_RED, |
|
494 | 494 | b'green_background': _BACKGROUND_GREEN, |
|
495 | 495 | b'yellow_background': _BACKGROUND_RED | _BACKGROUND_GREEN, |
|
496 | 496 | b'blue_background': _BACKGROUND_BLUE, |
|
497 | 497 | b'purple_background': _BACKGROUND_BLUE | _BACKGROUND_RED, |
|
498 | 498 | b'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN, |
|
499 | 499 | b'white_background': ( |
|
500 | 500 | _BACKGROUND_RED | _BACKGROUND_GREEN | _BACKGROUND_BLUE |
|
501 | 501 | ), |
|
502 | 502 | b'bold_background': _BACKGROUND_INTENSITY, |
|
503 | 503 | b'underline': _COMMON_LVB_UNDERSCORE, # double-byte charsets only |
|
504 | 504 | b'inverse': _COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only |
|
505 | 505 | } |
|
506 | 506 | |
|
507 | 507 | passthrough = { |
|
508 | 508 | _FOREGROUND_INTENSITY, |
|
509 | 509 | _BACKGROUND_INTENSITY, |
|
510 | 510 | _COMMON_LVB_UNDERSCORE, |
|
511 | 511 | _COMMON_LVB_REVERSE_VIDEO, |
|
512 | 512 | } |
|
513 | 513 | |
|
514 | 514 | stdout = _kernel32.GetStdHandle( |
|
515 | 515 | _STD_OUTPUT_HANDLE |
|
516 | 516 | ) # don't close the handle returned |
|
517 | 517 | if stdout is None or stdout == _INVALID_HANDLE_VALUE: |
|
518 | 518 | w32effects = None |
|
519 | 519 | else: |
|
520 | 520 | csbi = _CONSOLE_SCREEN_BUFFER_INFO() |
|
521 | 521 | if not _kernel32.GetConsoleScreenBufferInfo(stdout, ctypes.byref(csbi)): |
|
522 | 522 | # stdout may not support GetConsoleScreenBufferInfo() |
|
523 | 523 | # when called from subprocess or redirected |
|
524 | 524 | w32effects = None |
|
525 | 525 | else: |
|
526 | 526 | origattr = csbi.wAttributes |
|
527 | 527 | ansire = re.compile( |
|
528 | 528 | br'\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL |
|
529 | 529 | ) |
|
530 | 530 | |
|
531 | 531 | def win32print(ui, writefunc, text, **opts): |
|
532 | 532 | label = opts.get(r'label', b'') |
|
533 | 533 | attr = origattr |
|
534 | 534 | |
|
535 | 535 | def mapcolor(val, attr): |
|
536 | 536 | if val == -1: |
|
537 | 537 | return origattr |
|
538 | 538 | elif val in passthrough: |
|
539 | 539 | return attr | val |
|
540 | 540 | elif val > 0x0F: |
|
541 | 541 | return (val & 0x70) | (attr & 0x8F) |
|
542 | 542 | else: |
|
543 | 543 | return (val & 0x07) | (attr & 0xF8) |
|
544 | 544 | |
|
545 | 545 | # determine console attributes based on labels |
|
546 | 546 | for l in label.split(): |
|
547 | 547 | style = ui._styles.get(l, b'') |
|
548 | 548 | for effect in style.split(): |
|
549 | 549 | try: |
|
550 | 550 | attr = mapcolor(w32effects[effect], attr) |
|
551 | 551 | except KeyError: |
|
552 | 552 | # w32effects could not have certain attributes so we skip |
|
553 | 553 | # them if not found |
|
554 | 554 | pass |
|
555 | 555 | # hack to ensure regexp finds data |
|
556 | 556 | if not text.startswith(b'\033['): |
|
557 | 557 | text = b'\033[m' + text |
|
558 | 558 | |
|
559 | 559 | # Look for ANSI-like codes embedded in text |
|
560 | 560 | m = re.match(ansire, text) |
|
561 | 561 | |
|
562 | 562 | try: |
|
563 | 563 | while m: |
|
564 | 564 | for sattr in m.group(1).split(b';'): |
|
565 | 565 | if sattr: |
|
566 | 566 | attr = mapcolor(int(sattr), attr) |
|
567 | 567 | ui.flush() |
|
568 | 568 | _kernel32.SetConsoleTextAttribute(stdout, attr) |
|
569 | 569 | writefunc(m.group(2)) |
|
570 | 570 | m = re.match(ansire, m.group(3)) |
|
571 | 571 | finally: |
|
572 | 572 | # Explicitly reset original attributes |
|
573 | 573 | ui.flush() |
|
574 | 574 | _kernel32.SetConsoleTextAttribute(stdout, origattr) |
General Comments 0
You need to be logged in to leave comments.
Login now