Mercurial > hg-stable
annotate hgext/color.py @ 13920:332e400764e5
grep: don't print data from binary files for matches (issue2614)
author | Md. O. Shayan <mdoshayan@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 23:13:17 +0530 |
parents | 67f20625703f |
children | e0f07847f8de |
rev | line source |
---|---|
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
1 # color.py color output for the status and qseries commands |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
2 # |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
3 # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
4 # |
5792
956e01c31914
color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents:
5787
diff
changeset
|
5 # This program is free software; you can redistribute it and/or modify it |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
6 # under the terms of the GNU General Public License as published by the |
5792
956e01c31914
color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents:
5787
diff
changeset
|
7 # Free Software Foundation; either version 2 of the License, or (at your |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
8 # option) any later version. |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
9 # |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
10 # This program is distributed in the hope that it will be useful, but |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
11 # WITHOUT ANY WARRANTY; without even the implied warranty of |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
13 # Public License for more details. |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
14 # |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
15 # You should have received a copy of the GNU General Public License along |
5792
956e01c31914
color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents:
5787
diff
changeset
|
16 # with this program; if not, write to the Free Software Foundation, Inc., |
956e01c31914
color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents:
5787
diff
changeset
|
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
18 |
8894
868670dbc237
extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents:
8866
diff
changeset
|
19 '''colorize output from some commands |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
20 |
13638
72040d98ff0a
color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents:
13635
diff
changeset
|
21 This extension modifies the status and resolve commands to add color |
72040d98ff0a
color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents:
13635
diff
changeset
|
22 to their output to reflect file status, the qseries command to add |
72040d98ff0a
color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents:
13635
diff
changeset
|
23 color to reflect patch status (applied, unapplied, missing), and to |
72040d98ff0a
color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents:
13635
diff
changeset
|
24 diff-related commands to highlight additions, removals, diff headers, |
72040d98ff0a
color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents:
13635
diff
changeset
|
25 and trailing whitespace. |
7457
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
26 |
7988
0447939b4b97
color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents:
7983
diff
changeset
|
27 Other effects in addition to color, like bold and underlined text, are |
0447939b4b97
color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents:
7983
diff
changeset
|
28 also available. Effects are rendered with the ECMA-48 SGR control |
13635
c9ddc39c21b6
color: don't mention internal function in docstring
Martin Geisler <mg@aragost.com>
parents:
13361
diff
changeset
|
29 function (aka ANSI escape codes). |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
30 |
12083
ebfc46929f3e
help: refer to user configuration file more consistently
Brodie Rao <brodie@bitheap.org>
parents:
11732
diff
changeset
|
31 Default effects may be overridden from your configuration file:: |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
32 |
9206
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
33 [color] |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
34 status.modified = blue bold underline red_background |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
35 status.added = green bold |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
36 status.removed = red bold blue_background |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
37 status.deleted = cyan bold underline |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
38 status.unknown = magenta bold underline |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
39 status.ignored = black bold |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
40 |
9206
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
41 # 'none' turns off all effects |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
42 status.clean = none |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
43 status.copied = none |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
44 |
9206
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
45 qseries.applied = blue bold underline |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
46 qseries.unapplied = black bold |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
47 qseries.missing = red bold |
7456 | 48 |
9206
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
49 diff.diffline = bold |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
50 diff.extended = cyan bold |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
51 diff.file_a = red bold |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
52 diff.file_b = green bold |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
53 diff.hunk = magenta |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
54 diff.deleted = red |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
55 diff.inserted = green |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
56 diff.changed = white |
c1a1b49221e3
color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents:
9057
diff
changeset
|
57 diff.trailingwhitespace = bold red_background |
10046
0c23b0b3516b
color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents:
10045
diff
changeset
|
58 |
10223
51421ab573de
color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents:
10222
diff
changeset
|
59 resolve.unresolved = red bold |
51421ab573de
color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents:
10222
diff
changeset
|
60 resolve.resolved = green bold |
51421ab573de
color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents:
10222
diff
changeset
|
61 |
10046
0c23b0b3516b
color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents:
10045
diff
changeset
|
62 bookmarks.current = green |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
63 |
11969
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
64 branches.active = none |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
65 branches.closed = black bold |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
66 branches.current = green |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
67 branches.inactive = none |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
68 |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
69 The color extension will try to detect whether to use ANSI codes or |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
70 Win32 console APIs, unless it is made explicit:: |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
71 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
72 [color] |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
73 mode = ansi |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
74 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
75 Any value other than 'ansi', 'win32', or 'auto' will disable color. |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
76 |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
77 ''' |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
78 |
12062
c327bfa5e831
cleanup: remove unused imports
Brodie Rao <brodie@bitheap.org>
parents:
11969
diff
changeset
|
79 import os |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
80 |
12089
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
81 from mercurial import commands, dispatch, extensions, ui as uimod, util |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
82 from mercurial.i18n import _ |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
83 |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
84 # start and stop parameters for effects |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
85 _effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33, |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
86 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1, |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
87 'italic': 3, 'underline': 4, 'inverse': 7, |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
88 'black_background': 40, 'red_background': 41, |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
89 'green_background': 42, 'yellow_background': 43, |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
90 'blue_background': 44, 'purple_background': 45, |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
91 'cyan_background': 46, 'white_background': 47} |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
92 |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
93 _styles = {'grep.match': 'red bold', |
13361
5b4252364ff9
bookmarks: move color style to color
Matt Mackall <mpm@selenic.com>
parents:
12807
diff
changeset
|
94 'bookmarks.current': 'green', |
11969
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
95 'branches.active': 'none', |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
96 'branches.closed': 'black bold', |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
97 'branches.current': 'green', |
52ec5c813723
color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents:
11750
diff
changeset
|
98 'branches.inactive': 'none', |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
99 'diff.changed': 'white', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
100 'diff.deleted': 'red', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
101 'diff.diffline': 'bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
102 'diff.extended': 'cyan bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
103 'diff.file_a': 'red bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
104 'diff.file_b': 'green bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
105 'diff.hunk': 'magenta', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
106 'diff.inserted': 'green', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
107 'diff.trailingwhitespace': 'bold red_background', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
108 'diffstat.deleted': 'red', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
109 'diffstat.inserted': 'green', |
13774
1ce0e80799c0
ui: label prompts, default to yellow prompts
Martin Geisler <mg@lazybytes.net>
parents:
13648
diff
changeset
|
110 'ui.prompt': 'yellow', |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
111 'log.changeset': 'yellow', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
112 'resolve.resolved': 'green bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
113 'resolve.unresolved': 'red bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
114 'status.added': 'green bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
115 'status.clean': 'none', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
116 'status.copied': 'none', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
117 'status.deleted': 'cyan bold underline', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
118 'status.ignored': 'black bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
119 'status.modified': 'blue bold', |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
120 'status.removed': 'red bold', |
11310
ac873ecfc3c2
Backed out changeset: e1dde7363601
Steve Borho <steve@borho.org>
parents:
11302
diff
changeset
|
121 'status.unknown': 'magenta bold underline'} |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
122 |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
123 |
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
124 def render_effects(text, effects): |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
125 'Wrap text in commands to turn on each effect.' |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
126 if not text: |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
127 return text |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
128 start = [str(_effects[e]) for e in ['none'] + effects.split()] |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
129 start = '\033[' + ';'.join(start) + 'm' |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
130 stop = '\033[' + str(_effects['none']) + 'm' |
7459
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
131 return ''.join([start, text, stop]) |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
132 |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
133 def extstyles(): |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
134 for name, ext in extensions.extensions(): |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
135 _styles.update(getattr(ext, 'colortable', {})) |
7456 | 136 |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
137 def configstyles(ui): |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
138 for status, cfgeffects in ui.configitems('color'): |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
139 if '.' not in status: |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
140 continue |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
141 cfgeffects = ui.configlist('color', status) |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
142 if cfgeffects: |
8945
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
143 good = [] |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
144 for e in cfgeffects: |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
145 if e in _effects: |
8945
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
146 good.append(e) |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
147 else: |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
148 ui.warn(_("ignoring unknown color/effect %r " |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
149 "(configured in color.%s)\n") |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
150 % (e, status)) |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
151 _styles[status] = ' '.join(good) |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
152 |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
153 class colorui(uimod.ui): |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
154 def popbuffer(self, labeled=False): |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
155 if labeled: |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
156 return ''.join(self.label(a, label) for a, label |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
157 in self._buffers.pop()) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
158 return ''.join(a for a, label in self._buffers.pop()) |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
159 |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
160 _colormode = 'ansi' |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
161 def write(self, *args, **opts): |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
162 label = opts.get('label', '') |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
163 if self._buffers: |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
164 self._buffers[-1].extend([(str(a), label) for a in args]) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
165 elif self._colormode == 'win32': |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
166 for a in args: |
11727
c34a1ab80550
color: pass write/write_err to win32print correctly (issue2312)
Brodie Rao <brodie@bitheap.org>
parents:
11555
diff
changeset
|
167 win32print(a, super(colorui, self).write, **opts) |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
168 else: |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
169 return super(colorui, self).write( |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
170 *[self.label(str(a), label) for a in args], **opts) |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
171 |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
172 def write_err(self, *args, **opts): |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
173 label = opts.get('label', '') |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
174 if self._colormode == 'win32': |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
175 for a in args: |
11727
c34a1ab80550
color: pass write/write_err to win32print correctly (issue2312)
Brodie Rao <brodie@bitheap.org>
parents:
11555
diff
changeset
|
176 win32print(a, super(colorui, self).write_err, **opts) |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
177 else: |
11732
386e56ecfb78
color: call correct superclass method in write_err
Brodie Rao <brodie@bitheap.org>
parents:
11727
diff
changeset
|
178 return super(colorui, self).write_err( |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
179 *[self.label(str(a), label) for a in args], **opts) |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
180 |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
181 def label(self, msg, label): |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
182 effects = [] |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
183 for l in label.split(): |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
184 s = _styles.get(l, '') |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
185 if s: |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
186 effects.append(s) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
187 effects = ''.join(effects) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
188 if effects: |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
189 return '\n'.join([render_effects(s, effects) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
190 for s in msg.split('\n')]) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
191 return msg |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
192 |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
193 |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
194 def uisetup(ui): |
10871 | 195 if ui.plain(): |
196 return | |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
197 mode = ui.config('color', 'mode', 'auto') |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
198 if mode == 'auto': |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
199 if os.name == 'nt' and 'TERM' not in os.environ: |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
200 # looks line a cmd.exe console, use win32 API or nothing |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
201 mode = w32effects and 'win32' or 'none' |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
202 else: |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
203 mode = 'ansi' |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
204 if mode == 'win32': |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
205 if w32effects is None: |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
206 # only warn if color.mode is explicitly set to win32 |
13641
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
207 ui.warn(_('warning: failed to set color mode to %s\n') % mode) |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
208 return |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
209 _effects.update(w32effects) |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
210 elif mode != 'ansi': |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
211 return |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
212 def colorcmd(orig, ui_, opts, cmd, cmdfunc): |
12089
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
213 coloropt = opts['color'] |
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
214 auto = coloropt == 'auto' |
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
215 always = util.parsebool(coloropt) |
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
216 if (always or |
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
217 (always is None and |
70f4a0f4e9a3
color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents:
12084
diff
changeset
|
218 (auto and (os.environ.get('TERM') != 'dumb' and ui_.formatted())))): |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
219 colorui._colormode = mode |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
220 colorui.__bases__ = (ui_.__class__,) |
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
221 ui_.__class__ = colorui |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
222 extstyles() |
11555
d8d0fc3988ca
color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents:
11326
diff
changeset
|
223 configstyles(ui_) |
10826
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
224 return orig(ui_, opts, cmd, cmdfunc) |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
225 extensions.wrapfunction(dispatch, '_runcommand', colorcmd) |
717c35d55fb3
color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents:
10477
diff
changeset
|
226 |
12693
33f0682ba8b1
color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents:
12278
diff
changeset
|
227 def extsetup(ui): |
33f0682ba8b1
color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents:
12278
diff
changeset
|
228 commands.globalopts.append( |
33f0682ba8b1
color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents:
12278
diff
changeset
|
229 ('', 'color', 'auto', |
12807
6bbf0139a36d
color: give hint to translators about untranslated keywords
Martin Geisler <mg@lazybytes.net>
parents:
12693
diff
changeset
|
230 # i18n: 'always', 'auto', and 'never' are keywords and should |
6bbf0139a36d
color: give hint to translators about untranslated keywords
Martin Geisler <mg@lazybytes.net>
parents:
12693
diff
changeset
|
231 # not be translated |
12693
33f0682ba8b1
color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents:
12278
diff
changeset
|
232 _("when to colorize (boolean, always, auto, or never)"), |
33f0682ba8b1
color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents:
12278
diff
changeset
|
233 _('TYPE'))) |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
234 |
13641
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
235 if os.name != 'nt': |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
236 w32effects = None |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
237 else: |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
238 import re, ctypes |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
239 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
240 _kernel32 = ctypes.windll.kernel32 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
241 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
242 _WORD = ctypes.c_ushort |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
243 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
244 _INVALID_HANDLE_VALUE = -1 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
245 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
246 class _COORD(ctypes.Structure): |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
247 _fields_ = [('X', ctypes.c_short), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
248 ('Y', ctypes.c_short)] |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
249 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
250 class _SMALL_RECT(ctypes.Structure): |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
251 _fields_ = [('Left', ctypes.c_short), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
252 ('Top', ctypes.c_short), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
253 ('Right', ctypes.c_short), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
254 ('Bottom', ctypes.c_short)] |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
255 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
256 class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure): |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
257 _fields_ = [('dwSize', _COORD), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
258 ('dwCursorPosition', _COORD), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
259 ('wAttributes', _WORD), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
260 ('srWindow', _SMALL_RECT), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
261 ('dwMaximumWindowSize', _COORD)] |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
262 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
263 _STD_OUTPUT_HANDLE = 0xfffffff5L # (DWORD)-11 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
264 _STD_ERROR_HANDLE = 0xfffffff4L # (DWORD)-12 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
265 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
266 _FOREGROUND_BLUE = 0x0001 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
267 _FOREGROUND_GREEN = 0x0002 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
268 _FOREGROUND_RED = 0x0004 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
269 _FOREGROUND_INTENSITY = 0x0008 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
270 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
271 _BACKGROUND_BLUE = 0x0010 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
272 _BACKGROUND_GREEN = 0x0020 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
273 _BACKGROUND_RED = 0x0040 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
274 _BACKGROUND_INTENSITY = 0x0080 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
275 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
276 _COMMON_LVB_REVERSE_VIDEO = 0x4000 |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
277 _COMMON_LVB_UNDERSCORE = 0x8000 |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
278 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
279 # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
280 w32effects = { |
12277
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
281 'none': -1, |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
282 'black': 0, |
13641
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
283 'red': _FOREGROUND_RED, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
284 'green': _FOREGROUND_GREEN, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
285 'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
286 'blue': _FOREGROUND_BLUE, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
287 'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
288 'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
289 'white': _FOREGROUND_RED | _FOREGROUND_GREEN | _FOREGROUND_BLUE, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
290 'bold': _FOREGROUND_INTENSITY, |
12278 | 291 'black_background': 0x100, # unused value > 0x0f |
13641
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
292 'red_background': _BACKGROUND_RED, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
293 'green_background': _BACKGROUND_GREEN, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
294 'yellow_background': _BACKGROUND_RED | _BACKGROUND_GREEN, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
295 'blue_background': _BACKGROUND_BLUE, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
296 'purple_background': _BACKGROUND_BLUE | _BACKGROUND_RED, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
297 'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
298 'white_background': (_BACKGROUND_RED | _BACKGROUND_GREEN | |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
299 _BACKGROUND_BLUE), |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
300 'bold_background': _BACKGROUND_INTENSITY, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
301 'underline': _COMMON_LVB_UNDERSCORE, # double-byte charsets only |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
302 'inverse': _COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
303 } |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
304 |
13641
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
305 passthrough = set([_FOREGROUND_INTENSITY, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
306 _BACKGROUND_INTENSITY, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
307 _COMMON_LVB_UNDERSCORE, |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
308 _COMMON_LVB_REVERSE_VIDEO]) |
12277
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
309 |
13641
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
310 stdout = _kernel32.GetStdHandle( |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
311 _STD_OUTPUT_HANDLE) # don't close the handle returned |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
312 if stdout is None or stdout == _INVALID_HANDLE_VALUE: |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
313 w32effects = None |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
314 else: |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
315 csbi = _CONSOLE_SCREEN_BUFFER_INFO() |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
316 if not _kernel32.GetConsoleScreenBufferInfo( |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
317 stdout, ctypes.byref(csbi)): |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
318 # stdout may not support GetConsoleScreenBufferInfo() |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
319 # when called from subprocess or redirected |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
320 w32effects = None |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
321 else: |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
322 origattr = csbi.wAttributes |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
323 ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)', |
2420cb1ea1d6
color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents:
13635
diff
changeset
|
324 re.MULTILINE | re.DOTALL) |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
325 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
326 def win32print(text, orig, **opts): |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
327 label = opts.get('label', '') |
12277
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
328 attr = origattr |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
329 |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
330 def mapcolor(val, attr): |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
331 if val == -1: |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
332 return origattr |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
333 elif val in passthrough: |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
334 return attr | val |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
335 elif val > 0x0f: |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
336 return (val & 0x70) | (attr & 0x8f) |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
337 else: |
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
338 return (val & 0x07) | (attr & 0xf8) |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
339 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
340 # determine console attributes based on labels |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
341 for l in label.split(): |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
342 style = _styles.get(l, '') |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
343 for effect in style.split(): |
12277
a7d3147bd4b3
color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents:
12083
diff
changeset
|
344 attr = mapcolor(w32effects[effect], attr) |
10870
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
345 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
346 # hack to ensure regexp finds data |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
347 if not text.startswith('\033['): |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
348 text = '\033[m' + text |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
349 |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
350 # Look for ANSI-like codes embedded in text |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
351 m = re.match(ansire, text) |
a4944b430417
color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents:
10869
diff
changeset
|
352 |
13919
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
353 try: |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
354 while m: |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
355 for sattr in m.group(1).split(';'): |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
356 if sattr: |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
357 attr = mapcolor(int(sattr), attr) |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
358 _kernel32.SetConsoleTextAttribute(stdout, attr) |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
359 orig(m.group(2), **opts) |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
360 m = re.match(ansire, m.group(3)) |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
361 finally: |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
362 # Explicity reset original attributes |
67f20625703f
color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents:
13774
diff
changeset
|
363 _kernel32.SetConsoleTextAttribute(stdout, origattr) |