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