Show More
@@ -1,183 +1,182 b'' | |||
|
1 | 1 | # color.py color output for the status and qseries commands |
|
2 | 2 | # |
|
3 | 3 | # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # This program is free software; you can redistribute it and/or modify it |
|
6 | 6 | # under the terms of the GNU General Public License as published by the |
|
7 | 7 | # Free Software Foundation; either version 2 of the License, or (at your |
|
8 | 8 | # option) any later version. |
|
9 | 9 | # |
|
10 | 10 | # This program is distributed in the hope that it will be useful, but |
|
11 | 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
13 | 13 | # Public License for more details. |
|
14 | 14 | # |
|
15 | 15 | # You should have received a copy of the GNU General Public License along |
|
16 | 16 | # with this program; if not, write to the Free Software Foundation, Inc., |
|
17 | 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 | 18 | |
|
19 | 19 | '''colorize output from some commands |
|
20 | 20 | |
|
21 | 21 | This extension modifies the status and resolve commands to add color to their |
|
22 | 22 | output to reflect file status, the qseries command to add color to reflect |
|
23 | 23 | patch status (applied, unapplied, missing), and to diff-related |
|
24 | 24 | commands to highlight additions, removals, diff headers, and trailing |
|
25 | 25 | whitespace. |
|
26 | 26 | |
|
27 | 27 | Other effects in addition to color, like bold and underlined text, are |
|
28 | 28 | also available. Effects are rendered with the ECMA-48 SGR control |
|
29 | 29 | function (aka ANSI escape codes). This module also provides the |
|
30 | 30 | render_text function, which can be used to add effects to any text. |
|
31 | 31 | |
|
32 | 32 | Default effects may be overridden from the .hgrc file:: |
|
33 | 33 | |
|
34 | 34 | [color] |
|
35 | 35 | status.modified = blue bold underline red_background |
|
36 | 36 | status.added = green bold |
|
37 | 37 | status.removed = red bold blue_background |
|
38 | 38 | status.deleted = cyan bold underline |
|
39 | 39 | status.unknown = magenta bold underline |
|
40 | 40 | status.ignored = black bold |
|
41 | 41 | |
|
42 | 42 | # 'none' turns off all effects |
|
43 | 43 | status.clean = none |
|
44 | 44 | status.copied = none |
|
45 | 45 | |
|
46 | 46 | qseries.applied = blue bold underline |
|
47 | 47 | qseries.unapplied = black bold |
|
48 | 48 | qseries.missing = red bold |
|
49 | 49 | |
|
50 | 50 | diff.diffline = bold |
|
51 | 51 | diff.extended = cyan bold |
|
52 | 52 | diff.file_a = red bold |
|
53 | 53 | diff.file_b = green bold |
|
54 | 54 | diff.hunk = magenta |
|
55 | 55 | diff.deleted = red |
|
56 | 56 | diff.inserted = green |
|
57 | 57 | diff.changed = white |
|
58 | 58 | diff.trailingwhitespace = bold red_background |
|
59 | 59 | |
|
60 | 60 | resolve.unresolved = red bold |
|
61 | 61 | resolve.resolved = green bold |
|
62 | 62 | |
|
63 | 63 | bookmarks.current = green |
|
64 | 64 | ''' |
|
65 | 65 | |
|
66 | 66 | import os, sys |
|
67 | 67 | |
|
68 | 68 | from mercurial import commands, dispatch, extensions |
|
69 | 69 | from mercurial.i18n import _ |
|
70 | from mercurial.ui import ui as uicls | |
|
71 | 70 | |
|
72 | 71 | # start and stop parameters for effects |
|
73 | 72 | _effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33, |
|
74 | 73 | 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1, |
|
75 | 74 | 'italic': 3, 'underline': 4, 'inverse': 7, |
|
76 | 75 | 'black_background': 40, 'red_background': 41, |
|
77 | 76 | 'green_background': 42, 'yellow_background': 43, |
|
78 | 77 | 'blue_background': 44, 'purple_background': 45, |
|
79 | 78 | 'cyan_background': 46, 'white_background': 47} |
|
80 | 79 | |
|
81 | 80 | _styles = {'grep.match': 'red bold', |
|
82 | 81 | 'diff.changed': 'white', |
|
83 | 82 | 'diff.deleted': 'red', |
|
84 | 83 | 'diff.diffline': 'bold', |
|
85 | 84 | 'diff.extended': 'cyan bold', |
|
86 | 85 | 'diff.file_a': 'red bold', |
|
87 | 86 | 'diff.file_b': 'green bold', |
|
88 | 87 | 'diff.hunk': 'magenta', |
|
89 | 88 | 'diff.inserted': 'green', |
|
90 | 89 | 'diff.trailingwhitespace': 'bold red_background', |
|
91 | 90 | 'diffstat.deleted': 'red', |
|
92 | 91 | 'diffstat.inserted': 'green', |
|
93 | 92 | 'log.changeset': 'yellow', |
|
94 | 93 | 'resolve.resolved': 'green bold', |
|
95 | 94 | 'resolve.unresolved': 'red bold', |
|
96 | 95 | 'status.added': 'green bold', |
|
97 | 96 | 'status.clean': 'none', |
|
98 | 97 | 'status.copied': 'none', |
|
99 | 98 | 'status.deleted': 'cyan bold underline', |
|
100 | 99 | 'status.ignored': 'black bold', |
|
101 | 100 | 'status.modified': 'blue bold', |
|
102 | 101 | 'status.removed': 'red bold', |
|
103 | 102 | 'status.unknown': 'magenta bold underline'} |
|
104 | 103 | |
|
105 | 104 | |
|
106 | 105 | def render_effects(text, effects): |
|
107 | 106 | 'Wrap text in commands to turn on each effect.' |
|
108 | 107 | if not text: |
|
109 | 108 | return text |
|
110 | 109 | start = [str(_effects[e]) for e in ['none'] + effects.split()] |
|
111 | 110 | start = '\033[' + ';'.join(start) + 'm' |
|
112 | 111 | stop = '\033[' + str(_effects['none']) + 'm' |
|
113 | 112 | return ''.join([start, text, stop]) |
|
114 | 113 | |
|
115 | 114 | def extstyles(): |
|
116 | 115 | for name, ext in extensions.extensions(): |
|
117 | 116 | _styles.update(getattr(ext, 'colortable', {})) |
|
118 | 117 | |
|
119 | 118 | def configstyles(ui): |
|
120 | 119 | for status, cfgeffects in ui.configitems('color'): |
|
121 | 120 | if '.' not in status: |
|
122 | 121 | continue |
|
123 | 122 | cfgeffects = ui.configlist('color', status) |
|
124 | 123 | if cfgeffects: |
|
125 | 124 | good = [] |
|
126 | 125 | for e in cfgeffects: |
|
127 | 126 | if e in _effects: |
|
128 | 127 | good.append(e) |
|
129 | 128 | else: |
|
130 | 129 | ui.warn(_("ignoring unknown color/effect %r " |
|
131 | 130 | "(configured in color.%s)\n") |
|
132 | 131 | % (e, status)) |
|
133 | 132 | _styles[status] = ' '.join(good) |
|
134 | 133 | |
|
135 | 134 | _buffers = None |
|
136 | 135 | def style(msg, label): |
|
137 | 136 | effects = [] |
|
138 | 137 | for l in label.split(): |
|
139 | 138 | s = _styles.get(l, '') |
|
140 | 139 | if s: |
|
141 | 140 | effects.append(s) |
|
142 | 141 | effects = ''.join(effects) |
|
143 | 142 | if effects: |
|
144 | 143 | return '\n'.join([render_effects(s, effects) |
|
145 | 144 | for s in msg.split('\n')]) |
|
146 | 145 | return msg |
|
147 | 146 | |
|
148 | 147 | def popbuffer(orig, labeled=False): |
|
149 | 148 | global _buffers |
|
150 | 149 | if labeled: |
|
151 | 150 | return ''.join(style(a, label) for a, label in _buffers.pop()) |
|
152 | 151 | return ''.join(a for a, label in _buffers.pop()) |
|
153 | 152 | |
|
154 | 153 | def write(orig, *args, **opts): |
|
155 | 154 | label = opts.get('label', '') |
|
156 | 155 | global _buffers |
|
157 | 156 | if _buffers: |
|
158 | 157 | _buffers[-1].extend([(str(a), label) for a in args]) |
|
159 | 158 | else: |
|
160 | 159 | return orig(*[style(str(a), label) for a in args], **opts) |
|
161 | 160 | |
|
162 | 161 | def write_err(orig, *args, **opts): |
|
163 | 162 | label = opts.get('label', '') |
|
164 | 163 | return orig(*[style(str(a), label) for a in args], **opts) |
|
165 | 164 | |
|
166 | 165 | def uisetup(ui): |
|
167 | 166 | def colorcmd(orig, ui_, opts, cmd, cmdfunc): |
|
168 | 167 | if (opts['color'] == 'always' or |
|
169 | 168 | (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb' |
|
170 | 169 | and sys.__stdout__.isatty()))): |
|
171 | 170 | global _buffers |
|
172 | 171 | _buffers = ui_._buffers |
|
173 | 172 | extensions.wrapfunction(ui_, 'popbuffer', popbuffer) |
|
174 | 173 | extensions.wrapfunction(ui_, 'write', write) |
|
175 | 174 | extensions.wrapfunction(ui_, 'write_err', write_err) |
|
176 | 175 | ui_.label = style |
|
177 | 176 | extstyles() |
|
178 | 177 | configstyles(ui) |
|
179 | 178 | return orig(ui_, opts, cmd, cmdfunc) |
|
180 | 179 | extensions.wrapfunction(dispatch, '_runcommand', colorcmd) |
|
181 | 180 | |
|
182 | 181 | commands.globalopts.append(('', 'color', 'auto', |
|
183 | 182 | _("when to colorize (always, auto, or never)"))) |
General Comments 0
You need to be logged in to leave comments.
Login now