Show More
@@ -1,220 +1,224 b'' | |||||
1 | # color.py color output for the status and qseries commands |
|
1 | # color.py color output for the status and qseries commands | |
2 | # |
|
2 | # | |
3 | # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> |
|
3 | # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> | |
4 | # |
|
4 | # | |
5 | # This program is free software; you can redistribute it and/or modify it |
|
5 | # This program is free software; you can redistribute it and/or modify it | |
6 | # under the terms of the GNU General Public License as published by the |
|
6 | # under the terms of the GNU General Public License as published by the | |
7 | # Free Software Foundation; either version 2 of the License, or (at your |
|
7 | # Free Software Foundation; either version 2 of the License, or (at your | |
8 | # option) any later version. |
|
8 | # option) any later version. | |
9 | # |
|
9 | # | |
10 | # This program is distributed in the hope that it will be useful, but |
|
10 | # This program is distributed in the hope that it will be useful, but | |
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | |
13 | # Public License for more details. |
|
13 | # Public License for more details. | |
14 | # |
|
14 | # | |
15 | # You should have received a copy of the GNU General Public License along |
|
15 | # You should have received a copy of the GNU General Public License along | |
16 | # with this program; if not, write to the Free Software Foundation, Inc., |
|
16 | # with this program; if not, write to the Free Software Foundation, Inc., | |
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 |
|
18 | |||
19 | '''add color output to the status and qseries commands |
|
19 | '''add color output to the status and qseries commands | |
20 |
|
20 | |||
21 | This extension modifies the status command to add color to its output to |
|
21 | This extension modifies the status command to add color to its output to | |
22 | reflect file status, and the qseries command to add color to reflect patch |
|
22 | reflect file status, and the qseries command to add color to reflect patch | |
23 | status (applied, unapplied, missing). Other effects in addition to color, |
|
23 | status (applied, unapplied, missing). Other effects in addition to color, | |
24 | like bold and underlined text, are also available. Effects are rendered |
|
24 | like bold and underlined text, are also available. Effects are rendered | |
25 | with the ECMA-48 SGR control function (aka ANSI escape codes). This module |
|
25 | with the ECMA-48 SGR control function (aka ANSI escape codes). This module | |
26 | also provides the render_text function, which can be used to add effects to |
|
26 | also provides the render_text function, which can be used to add effects to | |
27 | any text. |
|
27 | any text. | |
28 |
|
28 | |||
29 | To enable this extension, add this to your .hgrc file: |
|
29 | To enable this extension, add this to your .hgrc file: | |
30 | [extensions] |
|
30 | [extensions] | |
31 | color = |
|
31 | color = | |
32 |
|
32 | |||
33 | Default effects my be overriden from the .hgrc file: |
|
33 | Default effects my be overriden from the .hgrc file: | |
34 |
|
34 | |||
35 | [color] |
|
35 | [color] | |
36 | status.modified = blue bold underline red_background |
|
36 | status.modified = blue bold underline red_background | |
37 | status.added = green bold |
|
37 | status.added = green bold | |
38 | status.removed = red bold blue_background |
|
38 | status.removed = red bold blue_background | |
39 | status.deleted = cyan bold underline |
|
39 | status.deleted = cyan bold underline | |
40 | status.unknown = magenta bold underline |
|
40 | status.unknown = magenta bold underline | |
41 | status.ignored = black bold |
|
41 | status.ignored = black bold | |
42 |
|
42 | |||
43 | 'none' turns off all effects |
|
43 | # 'none' turns off all effects | |
44 | status.clean = none |
|
44 | status.clean = none | |
45 | status.copied = none |
|
45 | status.copied = none | |
46 |
|
46 | |||
47 | qseries.applied = blue bold underline |
|
47 | qseries.applied = blue bold underline | |
48 | qseries.unapplied = black bold |
|
48 | qseries.unapplied = black bold | |
49 | qseries.missing = red bold |
|
49 | qseries.missing = red bold | |
50 | ''' |
|
50 | ''' | |
51 |
|
51 | |||
52 | import re, sys |
|
52 | import re, sys | |
53 |
|
53 | |||
54 | from mercurial import commands, cmdutil |
|
54 | from mercurial import commands, cmdutil | |
55 | from mercurial.i18n import _ |
|
55 | from mercurial.i18n import _ | |
56 |
|
56 | |||
57 | # start and stop parameters for effects |
|
57 | # start and stop parameters for effects | |
58 | _effect_params = { 'none': (0, 0), |
|
58 | _effect_params = { 'none': (0, 0), | |
59 | 'black': (30, 39), |
|
59 | 'black': (30, 39), | |
60 | 'red': (31, 39), |
|
60 | 'red': (31, 39), | |
61 | 'green': (32, 39), |
|
61 | 'green': (32, 39), | |
62 | 'yellow': (33, 39), |
|
62 | 'yellow': (33, 39), | |
63 | 'blue': (34, 39), |
|
63 | 'blue': (34, 39), | |
64 | 'magenta': (35, 39), |
|
64 | 'magenta': (35, 39), | |
65 | 'cyan': (36, 39), |
|
65 | 'cyan': (36, 39), | |
66 | 'white': (37, 39), |
|
66 | 'white': (37, 39), | |
67 | 'bold': (1, 22), |
|
67 | 'bold': (1, 22), | |
68 | 'italic': (3, 23), |
|
68 | 'italic': (3, 23), | |
69 | 'underline': (4, 24), |
|
69 | 'underline': (4, 24), | |
70 | 'inverse': (7, 27), |
|
70 | 'inverse': (7, 27), | |
71 | 'black_background': (40, 49), |
|
71 | 'black_background': (40, 49), | |
72 | 'red_background': (41, 49), |
|
72 | 'red_background': (41, 49), | |
73 | 'green_background': (42, 49), |
|
73 | 'green_background': (42, 49), | |
74 | 'yellow_background': (43, 49), |
|
74 | 'yellow_background': (43, 49), | |
75 | 'blue_background': (44, 49), |
|
75 | 'blue_background': (44, 49), | |
76 | 'purple_background': (45, 49), |
|
76 | 'purple_background': (45, 49), | |
77 | 'cyan_background': (46, 49), |
|
77 | 'cyan_background': (46, 49), | |
78 | 'white_background': (47, 49), } |
|
78 | 'white_background': (47, 49), } | |
79 |
|
79 | |||
80 | def render_effects(text, *effects): |
|
80 | def render_effects(text, *effects): | |
81 | 'Wrap text in commands to turn on each effect.' |
|
81 | 'Wrap text in commands to turn on each effect.' | |
82 | start = [] |
|
82 | start = [] | |
83 | stop = [] |
|
83 | stop = [] | |
84 | for effect in effects: |
|
84 | for effect in effects: | |
85 | start.append(str(_effect_params[effect][0])) |
|
85 | start.append(str(_effect_params[effect][0])) | |
86 | stop.append(str(_effect_params[effect][1])) |
|
86 | stop.append(str(_effect_params[effect][1])) | |
87 | start = '\033[' + ';'.join(start) + 'm' |
|
87 | start = '\033[' + ';'.join(start) + 'm' | |
88 | stop = '\033[' + ';'.join(stop) + 'm' |
|
88 | stop = '\033[' + ';'.join(stop) + 'm' | |
89 | return start + text + stop |
|
89 | return start + text + stop | |
90 |
|
90 | |||
91 | def colorstatus(statusfunc, ui, repo, *pats, **opts): |
|
91 | def colorstatus(statusfunc, ui, repo, *pats, **opts): | |
92 | '''run the status command with colored output''' |
|
92 | '''run the status command with colored output''' | |
93 |
|
93 | |||
94 | delimiter = opts['print0'] and '\0' or '\n' |
|
94 | delimiter = opts['print0'] and '\0' or '\n' | |
95 |
|
95 | |||
96 | # run status and capture it's output |
|
96 | # run status and capture it's output | |
97 | ui.pushbuffer() |
|
97 | ui.pushbuffer() | |
98 | retval = statusfunc(ui, repo, *pats, **opts) |
|
98 | retval = statusfunc(ui, repo, *pats, **opts) | |
99 | # filter out empty strings |
|
99 | # filter out empty strings | |
100 | lines = [ line for line in ui.popbuffer().split(delimiter) if line ] |
|
100 | lines = [ line for line in ui.popbuffer().split(delimiter) if line ] | |
101 |
|
101 | |||
102 | if opts['no_status']: |
|
102 | if opts['no_status']: | |
103 | # if --no-status, run the command again without that option to get |
|
103 | # if --no-status, run the command again without that option to get | |
104 | # output with status abbreviations |
|
104 | # output with status abbreviations | |
105 | opts['no_status'] = False |
|
105 | opts['no_status'] = False | |
106 | ui.pushbuffer() |
|
106 | ui.pushbuffer() | |
107 | statusfunc(ui, repo, *pats, **opts) |
|
107 | statusfunc(ui, repo, *pats, **opts) | |
108 | # filter out empty strings |
|
108 | # filter out empty strings | |
109 | lines_with_status = [ line for |
|
109 | lines_with_status = [ line for | |
110 | line in ui.popbuffer().split(delimiter) if line ] |
|
110 | line in ui.popbuffer().split(delimiter) if line ] | |
111 | else: |
|
111 | else: | |
112 | lines_with_status = lines |
|
112 | lines_with_status = lines | |
113 |
|
113 | |||
114 | # apply color to output and display it |
|
114 | # apply color to output and display it | |
115 | for i in xrange(0, len(lines)): |
|
115 | for i in xrange(0, len(lines)): | |
116 | status = _status_abbreviations[lines_with_status[i][0]] |
|
116 | status = _status_abbreviations[lines_with_status[i][0]] | |
117 | effects = _status_effects[status] |
|
117 | effects = _status_effects[status] | |
118 | if effects: |
|
118 | if effects: | |
119 | lines[i] = render_effects(lines[i], *effects) |
|
119 | lines[i] = render_effects(lines[i], *effects) | |
120 | sys.stdout.write(lines[i] + delimiter) |
|
120 | sys.stdout.write(lines[i] + delimiter) | |
121 | return retval |
|
121 | return retval | |
122 |
|
122 | |||
123 | _status_abbreviations = { 'M': 'modified', |
|
123 | _status_abbreviations = { 'M': 'modified', | |
124 | 'A': 'added', |
|
124 | 'A': 'added', | |
125 | 'R': 'removed', |
|
125 | 'R': 'removed', | |
126 | '!': 'deleted', |
|
126 | '!': 'deleted', | |
127 | '?': 'unknown', |
|
127 | '?': 'unknown', | |
128 | 'I': 'ignored', |
|
128 | 'I': 'ignored', | |
129 | 'C': 'clean', |
|
129 | 'C': 'clean', | |
130 | ' ': 'copied', } |
|
130 | ' ': 'copied', } | |
131 |
|
131 | |||
132 | _status_effects = { 'modified': ('blue', 'bold'), |
|
132 | _status_effects = { 'modified': ('blue', 'bold'), | |
133 | 'added': ('green', 'bold'), |
|
133 | 'added': ('green', 'bold'), | |
134 | 'removed': ('red', 'bold'), |
|
134 | 'removed': ('red', 'bold'), | |
135 | 'deleted': ('cyan', 'bold', 'underline'), |
|
135 | 'deleted': ('cyan', 'bold', 'underline'), | |
136 | 'unknown': ('magenta', 'bold', 'underline'), |
|
136 | 'unknown': ('magenta', 'bold', 'underline'), | |
137 | 'ignored': ('black', 'bold'), |
|
137 | 'ignored': ('black', 'bold'), | |
138 | 'clean': ('none', ), |
|
138 | 'clean': ('none', ), | |
139 | 'copied': ('none', ), } |
|
139 | 'copied': ('none', ), } | |
140 |
|
140 | |||
141 | def colorqseries(qseriesfunc, ui, repo, *dummy, **opts): |
|
141 | def colorqseries(qseriesfunc, ui, repo, *dummy, **opts): | |
142 | '''run the qseries command with colored output''' |
|
142 | '''run the qseries command with colored output''' | |
143 | ui.pushbuffer() |
|
143 | ui.pushbuffer() | |
144 | retval = qseriesfunc(ui, repo, **opts) |
|
144 | retval = qseriesfunc(ui, repo, **opts) | |
145 | patches = ui.popbuffer().splitlines() |
|
145 | patches = ui.popbuffer().splitlines() | |
146 | for patch in patches: |
|
146 | for patch in patches: | |
|
147 | patchname = patch | |||
|
148 | if opts['summary']: | |||
|
149 | patchname = patchname.split(': ')[0] | |||
|
150 | if ui.verbose: | |||
|
151 | patchname = patchname.split(' ', 2)[-1] | |||
|
152 | ||||
147 | if opts['missing']: |
|
153 | if opts['missing']: | |
148 | effects = _patch_effects['missing'] |
|
154 | effects = _patch_effects['missing'] | |
149 |
# Determine if patch is applied. |
|
155 | # Determine if patch is applied. | |
150 | # line in the applied patch list, in case --summary has been used |
|
|||
151 | # and output line isn't just the patch name. |
|
|||
152 | elif [ applied for applied in repo.mq.applied |
|
156 | elif [ applied for applied in repo.mq.applied | |
153 |
if patch |
|
157 | if patchname == applied.name ]: | |
154 | effects = _patch_effects['applied'] |
|
158 | effects = _patch_effects['applied'] | |
155 | else: |
|
159 | else: | |
156 | effects = _patch_effects['unapplied'] |
|
160 | effects = _patch_effects['unapplied'] | |
157 | sys.stdout.write(render_effects(patch, *effects) + '\n') |
|
161 | sys.stdout.write(render_effects(patch, *effects) + '\n') | |
158 | return retval |
|
162 | return retval | |
159 |
|
163 | |||
160 | _patch_effects = { 'applied': ('blue', 'bold', 'underline'), |
|
164 | _patch_effects = { 'applied': ('blue', 'bold', 'underline'), | |
161 | 'missing': ('red', 'bold'), |
|
165 | 'missing': ('red', 'bold'), | |
162 | 'unapplied': ('black', 'bold'), } |
|
166 | 'unapplied': ('black', 'bold'), } | |
163 |
|
167 | |||
164 | def uisetup(ui): |
|
168 | def uisetup(ui): | |
165 | '''Initialize the extension.''' |
|
169 | '''Initialize the extension.''' | |
166 | nocoloropt = ('', 'no-color', None, _("don't colorize output")) |
|
170 | nocoloropt = ('', 'no-color', None, _("don't colorize output")) | |
167 | _decoratecmd(ui, 'status', commands.table, colorstatus, nocoloropt) |
|
171 | _decoratecmd(ui, 'status', commands.table, colorstatus, nocoloropt) | |
168 | _configcmdeffects(ui, 'status', _status_effects); |
|
172 | _configcmdeffects(ui, 'status', _status_effects); | |
169 | if ui.config('extensions', 'hgext.mq') is not None or \ |
|
173 | if ui.config('extensions', 'hgext.mq') is not None or \ | |
170 | ui.config('extensions', 'mq') is not None: |
|
174 | ui.config('extensions', 'mq') is not None: | |
171 | from hgext import mq |
|
175 | from hgext import mq | |
172 | _decoratecmd(ui, 'qseries', mq.cmdtable, colorqseries, nocoloropt) |
|
176 | _decoratecmd(ui, 'qseries', mq.cmdtable, colorqseries, nocoloropt) | |
173 | _configcmdeffects(ui, 'qseries', _patch_effects); |
|
177 | _configcmdeffects(ui, 'qseries', _patch_effects); | |
174 |
|
178 | |||
175 | def _decoratecmd(ui, cmd, table, delegate, *delegateoptions): |
|
179 | def _decoratecmd(ui, cmd, table, delegate, *delegateoptions): | |
176 | '''Replace the function that implements cmd in table with a decorator. |
|
180 | '''Replace the function that implements cmd in table with a decorator. | |
177 |
|
181 | |||
178 | The decorator that becomes the new implementation of cmd calls |
|
182 | The decorator that becomes the new implementation of cmd calls | |
179 | delegate. The delegate's first argument is the replaced function, |
|
183 | delegate. The delegate's first argument is the replaced function, | |
180 | followed by the normal Mercurial command arguments (ui, repo, ...). If |
|
184 | followed by the normal Mercurial command arguments (ui, repo, ...). If | |
181 | the delegate adds command options, supply them as delegateoptions. |
|
185 | the delegate adds command options, supply them as delegateoptions. | |
182 | ''' |
|
186 | ''' | |
183 | cmdkey, cmdentry = _cmdtableitem(ui, cmd, table) |
|
187 | cmdkey, cmdentry = _cmdtableitem(ui, cmd, table) | |
184 | decorator = lambda ui, repo, *args, **opts: \ |
|
188 | decorator = lambda ui, repo, *args, **opts: \ | |
185 | _colordecorator(delegate, cmdentry[0], |
|
189 | _colordecorator(delegate, cmdentry[0], | |
186 | ui, repo, *args, **opts) |
|
190 | ui, repo, *args, **opts) | |
187 | # make sure 'hg help cmd' still works |
|
191 | # make sure 'hg help cmd' still works | |
188 | decorator.__doc__ = cmdentry[0].__doc__ |
|
192 | decorator.__doc__ = cmdentry[0].__doc__ | |
189 | decoratorentry = (decorator,) + cmdentry[1:] |
|
193 | decoratorentry = (decorator,) + cmdentry[1:] | |
190 | for option in delegateoptions: |
|
194 | for option in delegateoptions: | |
191 | decoratorentry[1].append(option) |
|
195 | decoratorentry[1].append(option) | |
192 | table[cmdkey] = decoratorentry |
|
196 | table[cmdkey] = decoratorentry | |
193 |
|
197 | |||
194 | def _cmdtableitem(ui, cmd, table): |
|
198 | def _cmdtableitem(ui, cmd, table): | |
195 | '''Return key, value from table for cmd, or None if not found.''' |
|
199 | '''Return key, value from table for cmd, or None if not found.''' | |
196 | aliases, entry = cmdutil.findcmd(ui, cmd, table) |
|
200 | aliases, entry = cmdutil.findcmd(ui, cmd, table) | |
197 | for candidatekey, candidateentry in table.iteritems(): |
|
201 | for candidatekey, candidateentry in table.iteritems(): | |
198 | if candidateentry is entry: |
|
202 | if candidateentry is entry: | |
199 | return candidatekey, entry |
|
203 | return candidatekey, entry | |
200 |
|
204 | |||
201 | def _colordecorator(colorfunc, nocolorfunc, ui, repo, *args, **opts): |
|
205 | def _colordecorator(colorfunc, nocolorfunc, ui, repo, *args, **opts): | |
202 | '''Delegate to colorfunc or nocolorfunc, depending on conditions. |
|
206 | '''Delegate to colorfunc or nocolorfunc, depending on conditions. | |
203 |
|
207 | |||
204 | Delegate to colorfunc unless --no-color option is set or output is not |
|
208 | Delegate to colorfunc unless --no-color option is set or output is not | |
205 | to a tty. |
|
209 | to a tty. | |
206 | ''' |
|
210 | ''' | |
207 | if opts['no_color'] or not sys.stdout.isatty(): |
|
211 | if opts['no_color'] or not sys.stdout.isatty(): | |
208 | return nocolorfunc(ui, repo, *args, **opts) |
|
212 | return nocolorfunc(ui, repo, *args, **opts) | |
209 | return colorfunc(nocolorfunc, ui, repo, *args, **opts) |
|
213 | return colorfunc(nocolorfunc, ui, repo, *args, **opts) | |
210 |
|
214 | |||
211 | def _configcmdeffects(ui, cmdname, effectsmap): |
|
215 | def _configcmdeffects(ui, cmdname, effectsmap): | |
212 | '''Override default effects for cmdname with those from .hgrc file. |
|
216 | '''Override default effects for cmdname with those from .hgrc file. | |
213 |
|
217 | |||
214 | Entries in the .hgrc file are in the [color] section, and look like |
|
218 | Entries in the .hgrc file are in the [color] section, and look like | |
215 | 'cmdname'.'status' (for instance, 'status.modified = blue bold inverse'). |
|
219 | 'cmdname'.'status' (for instance, 'status.modified = blue bold inverse'). | |
216 | ''' |
|
220 | ''' | |
217 | for status in effectsmap: |
|
221 | for status in effectsmap: | |
218 | effects = ui.config('color', cmdname + '.' + status) |
|
222 | effects = ui.config('color', cmdname + '.' + status) | |
219 | if effects: |
|
223 | if effects: | |
220 | effectsmap[status] = re.split('\W+', effects) |
|
224 | effectsmap[status] = re.split('\W+', effects) |
General Comments 0
You need to be logged in to leave comments.
Login now