author | Martin Geisler <mg@lazybytes.net> |
Tue, 07 Jul 2009 23:54:42 +0200 | |
changeset 9077 | 22a33e207a7d |
parent 9057 | 07788bbb35e4 |
child 9206 | c1a1b49221e3 |
permissions | -rw-r--r-- |
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 |
|
9057
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
21 |
This extension modifies the status command to add color to its output to |
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
22 |
reflect file status, the qseries command to add color to reflect patch status |
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
23 |
(applied, unapplied, missing), and to diff-related commands to highlight |
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
24 |
additions, removals, diff headers, and trailing whitespace. |
7457
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
25 |
|
9057
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
26 |
Other effects in addition to color, like bold and underlined text, are also |
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
27 |
available. Effects are rendered with the ECMA-48 SGR control function (aka |
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
28 |
ANSI escape codes). This module also provides the render_text function, which |
07788bbb35e4
color: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents:
9017
diff
changeset
|
29 |
can be used to add effects to any text. |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
30 |
|
8685 | 31 |
Default effects may be overridden from the .hgrc file: |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
32 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
33 |
[color] |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
34 |
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
|
35 |
status.added = green bold |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
36 |
status.removed = red bold blue_background |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
37 |
status.deleted = cyan bold underline |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
38 |
status.unknown = magenta bold underline |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
39 |
status.ignored = black bold |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
40 |
|
6855
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
41 |
# 'none' turns off all effects |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
42 |
status.clean = none |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
43 |
status.copied = none |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
44 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
45 |
qseries.applied = blue bold underline |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
46 |
qseries.unapplied = black bold |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
47 |
qseries.missing = red bold |
7456 | 48 |
|
49 |
diff.diffline = bold |
|
50 |
diff.extended = cyan bold |
|
51 |
diff.file_a = red bold |
|
52 |
diff.file_b = green bold |
|
53 |
diff.hunk = magenta |
|
54 |
diff.deleted = red |
|
55 |
diff.inserted = green |
|
56 |
diff.changed = white |
|
7457
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
57 |
diff.trailingwhitespace = bold red_background |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
58 |
''' |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
59 |
|
8623
0c159aa7b329
color: replace re.split with ui.configlist
Martin Geisler <mg@lazybytes.net>
parents:
8622
diff
changeset
|
60 |
import os, sys |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
61 |
|
8963
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
62 |
from mercurial import cmdutil, commands, extensions, error |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
63 |
from mercurial.i18n import _ |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
64 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
65 |
# start and stop parameters for effects |
7459
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
66 |
_effect_params = {'none': 0, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
67 |
'black': 30, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
68 |
'red': 31, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
69 |
'green': 32, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
70 |
'yellow': 33, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
71 |
'blue': 34, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
72 |
'magenta': 35, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
73 |
'cyan': 36, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
74 |
'white': 37, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
75 |
'bold': 1, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
76 |
'italic': 3, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
77 |
'underline': 4, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
78 |
'inverse': 7, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
79 |
'black_background': 40, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
80 |
'red_background': 41, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
81 |
'green_background': 42, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
82 |
'yellow_background': 43, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
83 |
'blue_background': 44, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
84 |
'purple_background': 45, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
85 |
'cyan_background': 46, |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
86 |
'white_background': 47} |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
87 |
|
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
88 |
def render_effects(text, effects): |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
89 |
'Wrap text in commands to turn on each effect.' |
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
90 |
start = [str(_effect_params[e]) for e in ['none'] + effects] |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
91 |
start = '\033[' + ';'.join(start) + 'm' |
7459
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
92 |
stop = '\033[' + str(_effect_params['none']) + 'm' |
3fb5c142a9f0
color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents:
7457
diff
changeset
|
93 |
return ''.join([start, text, stop]) |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
94 |
|
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
95 |
def colorstatus(orig, ui, repo, *pats, **opts): |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
96 |
'''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
|
97 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
98 |
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
|
99 |
|
7419
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
100 |
nostatus = opts.get('no_status') |
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
101 |
opts['no_status'] = False |
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
102 |
# run status and capture its output |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
103 |
ui.pushbuffer() |
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
104 |
retval = orig(ui, repo, *pats, **opts) |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
105 |
# filter out empty strings |
7419
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
106 |
lines_with_status = [ line for line in ui.popbuffer().split(delimiter) if line ] |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
107 |
|
7419
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
108 |
if nostatus: |
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
109 |
lines = [l[2:] for l in lines_with_status] |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
110 |
else: |
7419
efe31fbe6cf0
color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents:
7418
diff
changeset
|
111 |
lines = lines_with_status |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
112 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
113 |
# apply color to output and display it |
8624
2b3dec0ef3ae
replace xrange(0, n) with xrange(n)
Martin Geisler <mg@lazybytes.net>
parents:
8623
diff
changeset
|
114 |
for i in xrange(len(lines)): |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
115 |
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
|
116 |
effects = _status_effects[status] |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
117 |
if effects: |
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
118 |
lines[i] = render_effects(lines[i], effects) |
7455 | 119 |
ui.write(lines[i] + delimiter) |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
120 |
return retval |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
121 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
122 |
_status_abbreviations = { 'M': 'modified', |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
123 |
'A': 'added', |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
124 |
'R': 'removed', |
5796
7705d308eb5e
Fix status char in color extension for deleted (missing) files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5792
diff
changeset
|
125 |
'!': 'deleted', |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
126 |
'?': 'unknown', |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
127 |
'I': 'ignored', |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
128 |
'C': 'clean', |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
129 |
' ': 'copied', } |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
130 |
|
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
131 |
_status_effects = { 'modified': ['blue', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
132 |
'added': ['green', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
133 |
'removed': ['red', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
134 |
'deleted': ['cyan', 'bold', 'underline'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
135 |
'unknown': ['magenta', 'bold', 'underline'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
136 |
'ignored': ['black', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
137 |
'clean': ['none'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
138 |
'copied': ['none'], } |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
139 |
|
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
140 |
def colorqseries(orig, ui, repo, *dummy, **opts): |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
141 |
'''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
|
142 |
ui.pushbuffer() |
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
143 |
retval = orig(ui, repo, **opts) |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
144 |
patches = ui.popbuffer().splitlines() |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
145 |
for patch in patches: |
6855
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
146 |
patchname = patch |
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
147 |
if opts['summary']: |
9017
c24c9ce0cdcf
color: don't highlight the summary part of the qseries command
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
8963
diff
changeset
|
148 |
patchname = patchname.split(': ', 1)[0] |
6855
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
149 |
if ui.verbose: |
9017
c24c9ce0cdcf
color: don't highlight the summary part of the qseries command
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
8963
diff
changeset
|
150 |
patchname = patchname.lstrip().split(' ', 2)[-1] |
6855
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
151 |
|
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
152 |
if opts['missing']: |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
153 |
effects = _patch_effects['missing'] |
6855
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
154 |
# Determine if patch is applied. |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
155 |
elif [ applied for applied in repo.mq.applied |
6855
09db2b8236ec
Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents:
6854
diff
changeset
|
156 |
if patchname == applied.name ]: |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
157 |
effects = _patch_effects['applied'] |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
158 |
else: |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
159 |
effects = _patch_effects['unapplied'] |
9017
c24c9ce0cdcf
color: don't highlight the summary part of the qseries command
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
8963
diff
changeset
|
160 |
|
c24c9ce0cdcf
color: don't highlight the summary part of the qseries command
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
8963
diff
changeset
|
161 |
patch = patch.replace(patchname, render_effects(patchname, effects), 1) |
c24c9ce0cdcf
color: don't highlight the summary part of the qseries command
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
8963
diff
changeset
|
162 |
ui.write(patch + '\n') |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
163 |
return retval |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
164 |
|
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
165 |
_patch_effects = { 'applied': ['blue', 'bold', 'underline'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
166 |
'missing': ['red', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
167 |
'unapplied': ['black', 'bold'], } |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
168 |
|
7456 | 169 |
def colorwrap(orig, s): |
170 |
'''wrap ui.write for colored diff output''' |
|
171 |
lines = s.split('\n') |
|
172 |
for i, line in enumerate(lines): |
|
7457
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
173 |
stripline = line |
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
174 |
if line and line[0] in '+-': |
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
175 |
# highlight trailing whitespace, but only in changed lines |
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
176 |
stripline = line.rstrip() |
7456 | 177 |
for prefix, style in _diff_prefixes: |
7457
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
178 |
if stripline.startswith(prefix): |
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
179 |
lines[i] = render_effects(stripline, _diff_effects[style]) |
7456 | 180 |
break |
7457
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
181 |
if line != stripline: |
a70fb83cbb9e
diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents:
7456
diff
changeset
|
182 |
lines[i] += render_effects( |
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
183 |
line[len(stripline):], _diff_effects['trailingwhitespace']) |
7456 | 184 |
orig('\n'.join(lines)) |
185 |
||
186 |
def colorshowpatch(orig, self, node): |
|
187 |
'''wrap cmdutil.changeset_printer.showpatch with colored output''' |
|
188 |
oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap) |
|
189 |
try: |
|
190 |
orig(self, node) |
|
191 |
finally: |
|
192 |
self.ui.write = oldwrite |
|
193 |
||
194 |
def colordiff(orig, ui, repo, *pats, **opts): |
|
195 |
'''run the diff command with colored output''' |
|
196 |
oldwrite = extensions.wrapfunction(ui, 'write', colorwrap) |
|
197 |
try: |
|
198 |
orig(ui, repo, *pats, **opts) |
|
199 |
finally: |
|
200 |
ui.write = oldwrite |
|
201 |
||
202 |
_diff_prefixes = [('diff', 'diffline'), |
|
203 |
('copy', 'extended'), |
|
204 |
('rename', 'extended'), |
|
7539
92c373f8135f
color: fix colorization of the 'old mode' git diff metadata
Gilles Moris <gilles.moris@free.fr>
parents:
7459
diff
changeset
|
205 |
('old', 'extended'), |
7456 | 206 |
('new', 'extended'), |
207 |
('deleted', 'extended'), |
|
208 |
('---', 'file_a'), |
|
209 |
('+++', 'file_b'), |
|
210 |
('@', 'hunk'), |
|
211 |
('-', 'deleted'), |
|
212 |
('+', 'inserted')] |
|
213 |
||
8630
8ff65f7e3273
color: cleanup extra commas
Martin Geisler <mg@lazybytes.net>
parents:
8624
diff
changeset
|
214 |
_diff_effects = {'diffline': ['bold'], |
8622
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
215 |
'extended': ['cyan', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
216 |
'file_a': ['red', 'bold'], |
0a4f6e1b78dc
color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents:
8278
diff
changeset
|
217 |
'file_b': ['green', 'bold'], |
8630
8ff65f7e3273
color: cleanup extra commas
Martin Geisler <mg@lazybytes.net>
parents:
8624
diff
changeset
|
218 |
'hunk': ['magenta'], |
8ff65f7e3273
color: cleanup extra commas
Martin Geisler <mg@lazybytes.net>
parents:
8624
diff
changeset
|
219 |
'deleted': ['red'], |
8ff65f7e3273
color: cleanup extra commas
Martin Geisler <mg@lazybytes.net>
parents:
8624
diff
changeset
|
220 |
'inserted': ['green'], |
8ff65f7e3273
color: cleanup extra commas
Martin Geisler <mg@lazybytes.net>
parents:
8624
diff
changeset
|
221 |
'changed': ['white'], |
8ff65f7e3273
color: cleanup extra commas
Martin Geisler <mg@lazybytes.net>
parents:
8624
diff
changeset
|
222 |
'trailingwhitespace': ['bold', 'red_background']} |
7456 | 223 |
|
8963
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
224 |
_ui = None |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
225 |
|
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
226 |
def uisetup(ui): |
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
227 |
'''Initialize the extension.''' |
8963
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
228 |
global _ui |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
229 |
_ui = ui |
7456 | 230 |
_setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects) |
231 |
_setupcmd(ui, 'incoming', commands.table, None, _diff_effects) |
|
232 |
_setupcmd(ui, 'log', commands.table, None, _diff_effects) |
|
233 |
_setupcmd(ui, 'outgoing', commands.table, None, _diff_effects) |
|
234 |
_setupcmd(ui, 'tip', commands.table, None, _diff_effects) |
|
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
235 |
_setupcmd(ui, 'status', commands.table, colorstatus, _status_effects) |
8963
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
236 |
|
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
237 |
def extsetup(): |
8278
1f9787de17d9
color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents:
7988
diff
changeset
|
238 |
try: |
1f9787de17d9
color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents:
7988
diff
changeset
|
239 |
mq = extensions.find('mq') |
8963
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
240 |
try: |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
241 |
# If we are loaded after mq, we must wrap commands.table |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
242 |
_setupcmd(_ui, 'qdiff', commands.table, colordiff, _diff_effects) |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
243 |
_setupcmd(_ui, 'qseries', commands.table, colorqseries, _patch_effects) |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
244 |
except error.UnknownCommand: |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
245 |
# Otherwise we wrap mq.cmdtable |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
246 |
_setupcmd(_ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects) |
a4ceae3aa7be
color: wrap qdiff/qseries after all extensions are loaded
Brodie Rao <me+hg@dackz.net>
parents:
8945
diff
changeset
|
247 |
_setupcmd(_ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects) |
8278
1f9787de17d9
color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents:
7988
diff
changeset
|
248 |
except KeyError: |
1f9787de17d9
color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents:
7988
diff
changeset
|
249 |
# The mq extension is not enabled |
1f9787de17d9
color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents:
7988
diff
changeset
|
250 |
pass |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
251 |
|
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
252 |
def _setupcmd(ui, cmd, table, func, effectsmap): |
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
253 |
'''patch in command to command table and load effect map''' |
7455 | 254 |
def nocolor(orig, *args, **opts): |
255 |
||
256 |
if (opts['no_color'] or opts['color'] == 'never' or |
|
257 |
(opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb' |
|
258 |
or not sys.__stdout__.isatty()))): |
|
259 |
return orig(*args, **opts) |
|
260 |
||
7456 | 261 |
oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer, |
262 |
'showpatch', colorshowpatch) |
|
263 |
try: |
|
264 |
if func is not None: |
|
265 |
return func(orig, *args, **opts) |
|
266 |
return orig(*args, **opts) |
|
267 |
finally: |
|
268 |
cmdutil.changeset_printer.showpatch = oldshowpatch |
|
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
269 |
|
7216
292fb2ad2846
extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
270 |
entry = extensions.wrapcommand(table, cmd, nocolor) |
7455 | 271 |
entry[1].extend([ |
272 |
('', 'color', 'auto', _("when to colorize (always, auto, or never)")), |
|
273 |
('', 'no-color', None, _("don't colorize output")), |
|
274 |
]) |
|
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
275 |
|
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
276 |
for status in effectsmap: |
8945
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
277 |
configkey = cmd + '.' + status |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
278 |
effects = ui.configlist('color', configkey) |
5787
b7b22a2ade2e
Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff
changeset
|
279 |
if 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
|
280 |
good = [] |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
281 |
for e in effects: |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
282 |
if e in _effect_params: |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
283 |
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
|
284 |
else: |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
285 |
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
|
286 |
"(configured in color.%s)\n") |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
287 |
% (e, configkey)) |
7b3d837ca60e
color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents:
8894
diff
changeset
|
288 |
effectsmap[status] = good |