##// END OF EJS Templates
color: remove unused import
Steve Borho -
r10869:2d57be56 default
parent child Browse files
Show More
@@ -1,183 +1,182 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 '''colorize output from some commands
19 '''colorize output from some commands
20
20
21 This extension modifies the status and resolve commands to add color to their
21 This extension modifies the status and resolve commands to add color to their
22 output to reflect file status, the qseries command to add color to reflect
22 output to reflect file status, the qseries command to add color to reflect
23 patch status (applied, unapplied, missing), and to diff-related
23 patch status (applied, unapplied, missing), and to diff-related
24 commands to highlight additions, removals, diff headers, and trailing
24 commands to highlight additions, removals, diff headers, and trailing
25 whitespace.
25 whitespace.
26
26
27 Other effects in addition to color, like bold and underlined text, are
27 Other effects in addition to color, like bold and underlined text, are
28 also available. Effects are rendered with the ECMA-48 SGR control
28 also available. Effects are rendered with the ECMA-48 SGR control
29 function (aka ANSI escape codes). This module also provides the
29 function (aka ANSI escape codes). This module also provides the
30 render_text function, which can be used to add effects to any text.
30 render_text function, which can be used to add effects to any text.
31
31
32 Default effects may be overridden from the .hgrc file::
32 Default effects may be overridden from the .hgrc file::
33
33
34 [color]
34 [color]
35 status.modified = blue bold underline red_background
35 status.modified = blue bold underline red_background
36 status.added = green bold
36 status.added = green bold
37 status.removed = red bold blue_background
37 status.removed = red bold blue_background
38 status.deleted = cyan bold underline
38 status.deleted = cyan bold underline
39 status.unknown = magenta bold underline
39 status.unknown = magenta bold underline
40 status.ignored = black bold
40 status.ignored = black bold
41
41
42 # 'none' turns off all effects
42 # 'none' turns off all effects
43 status.clean = none
43 status.clean = none
44 status.copied = none
44 status.copied = none
45
45
46 qseries.applied = blue bold underline
46 qseries.applied = blue bold underline
47 qseries.unapplied = black bold
47 qseries.unapplied = black bold
48 qseries.missing = red bold
48 qseries.missing = red bold
49
49
50 diff.diffline = bold
50 diff.diffline = bold
51 diff.extended = cyan bold
51 diff.extended = cyan bold
52 diff.file_a = red bold
52 diff.file_a = red bold
53 diff.file_b = green bold
53 diff.file_b = green bold
54 diff.hunk = magenta
54 diff.hunk = magenta
55 diff.deleted = red
55 diff.deleted = red
56 diff.inserted = green
56 diff.inserted = green
57 diff.changed = white
57 diff.changed = white
58 diff.trailingwhitespace = bold red_background
58 diff.trailingwhitespace = bold red_background
59
59
60 resolve.unresolved = red bold
60 resolve.unresolved = red bold
61 resolve.resolved = green bold
61 resolve.resolved = green bold
62
62
63 bookmarks.current = green
63 bookmarks.current = green
64 '''
64 '''
65
65
66 import os, sys
66 import os, sys
67
67
68 from mercurial import commands, dispatch, extensions
68 from mercurial import commands, dispatch, extensions
69 from mercurial.i18n import _
69 from mercurial.i18n import _
70 from mercurial.ui import ui as uicls
71
70
72 # start and stop parameters for effects
71 # start and stop parameters for effects
73 _effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
72 _effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
74 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1,
73 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1,
75 'italic': 3, 'underline': 4, 'inverse': 7,
74 'italic': 3, 'underline': 4, 'inverse': 7,
76 'black_background': 40, 'red_background': 41,
75 'black_background': 40, 'red_background': 41,
77 'green_background': 42, 'yellow_background': 43,
76 'green_background': 42, 'yellow_background': 43,
78 'blue_background': 44, 'purple_background': 45,
77 'blue_background': 44, 'purple_background': 45,
79 'cyan_background': 46, 'white_background': 47}
78 'cyan_background': 46, 'white_background': 47}
80
79
81 _styles = {'grep.match': 'red bold',
80 _styles = {'grep.match': 'red bold',
82 'diff.changed': 'white',
81 'diff.changed': 'white',
83 'diff.deleted': 'red',
82 'diff.deleted': 'red',
84 'diff.diffline': 'bold',
83 'diff.diffline': 'bold',
85 'diff.extended': 'cyan bold',
84 'diff.extended': 'cyan bold',
86 'diff.file_a': 'red bold',
85 'diff.file_a': 'red bold',
87 'diff.file_b': 'green bold',
86 'diff.file_b': 'green bold',
88 'diff.hunk': 'magenta',
87 'diff.hunk': 'magenta',
89 'diff.inserted': 'green',
88 'diff.inserted': 'green',
90 'diff.trailingwhitespace': 'bold red_background',
89 'diff.trailingwhitespace': 'bold red_background',
91 'diffstat.deleted': 'red',
90 'diffstat.deleted': 'red',
92 'diffstat.inserted': 'green',
91 'diffstat.inserted': 'green',
93 'log.changeset': 'yellow',
92 'log.changeset': 'yellow',
94 'resolve.resolved': 'green bold',
93 'resolve.resolved': 'green bold',
95 'resolve.unresolved': 'red bold',
94 'resolve.unresolved': 'red bold',
96 'status.added': 'green bold',
95 'status.added': 'green bold',
97 'status.clean': 'none',
96 'status.clean': 'none',
98 'status.copied': 'none',
97 'status.copied': 'none',
99 'status.deleted': 'cyan bold underline',
98 'status.deleted': 'cyan bold underline',
100 'status.ignored': 'black bold',
99 'status.ignored': 'black bold',
101 'status.modified': 'blue bold',
100 'status.modified': 'blue bold',
102 'status.removed': 'red bold',
101 'status.removed': 'red bold',
103 'status.unknown': 'magenta bold underline'}
102 'status.unknown': 'magenta bold underline'}
104
103
105
104
106 def render_effects(text, effects):
105 def render_effects(text, effects):
107 'Wrap text in commands to turn on each effect.'
106 'Wrap text in commands to turn on each effect.'
108 if not text:
107 if not text:
109 return text
108 return text
110 start = [str(_effects[e]) for e in ['none'] + effects.split()]
109 start = [str(_effects[e]) for e in ['none'] + effects.split()]
111 start = '\033[' + ';'.join(start) + 'm'
110 start = '\033[' + ';'.join(start) + 'm'
112 stop = '\033[' + str(_effects['none']) + 'm'
111 stop = '\033[' + str(_effects['none']) + 'm'
113 return ''.join([start, text, stop])
112 return ''.join([start, text, stop])
114
113
115 def extstyles():
114 def extstyles():
116 for name, ext in extensions.extensions():
115 for name, ext in extensions.extensions():
117 _styles.update(getattr(ext, 'colortable', {}))
116 _styles.update(getattr(ext, 'colortable', {}))
118
117
119 def configstyles(ui):
118 def configstyles(ui):
120 for status, cfgeffects in ui.configitems('color'):
119 for status, cfgeffects in ui.configitems('color'):
121 if '.' not in status:
120 if '.' not in status:
122 continue
121 continue
123 cfgeffects = ui.configlist('color', status)
122 cfgeffects = ui.configlist('color', status)
124 if cfgeffects:
123 if cfgeffects:
125 good = []
124 good = []
126 for e in cfgeffects:
125 for e in cfgeffects:
127 if e in _effects:
126 if e in _effects:
128 good.append(e)
127 good.append(e)
129 else:
128 else:
130 ui.warn(_("ignoring unknown color/effect %r "
129 ui.warn(_("ignoring unknown color/effect %r "
131 "(configured in color.%s)\n")
130 "(configured in color.%s)\n")
132 % (e, status))
131 % (e, status))
133 _styles[status] = ' '.join(good)
132 _styles[status] = ' '.join(good)
134
133
135 _buffers = None
134 _buffers = None
136 def style(msg, label):
135 def style(msg, label):
137 effects = []
136 effects = []
138 for l in label.split():
137 for l in label.split():
139 s = _styles.get(l, '')
138 s = _styles.get(l, '')
140 if s:
139 if s:
141 effects.append(s)
140 effects.append(s)
142 effects = ''.join(effects)
141 effects = ''.join(effects)
143 if effects:
142 if effects:
144 return '\n'.join([render_effects(s, effects)
143 return '\n'.join([render_effects(s, effects)
145 for s in msg.split('\n')])
144 for s in msg.split('\n')])
146 return msg
145 return msg
147
146
148 def popbuffer(orig, labeled=False):
147 def popbuffer(orig, labeled=False):
149 global _buffers
148 global _buffers
150 if labeled:
149 if labeled:
151 return ''.join(style(a, label) for a, label in _buffers.pop())
150 return ''.join(style(a, label) for a, label in _buffers.pop())
152 return ''.join(a for a, label in _buffers.pop())
151 return ''.join(a for a, label in _buffers.pop())
153
152
154 def write(orig, *args, **opts):
153 def write(orig, *args, **opts):
155 label = opts.get('label', '')
154 label = opts.get('label', '')
156 global _buffers
155 global _buffers
157 if _buffers:
156 if _buffers:
158 _buffers[-1].extend([(str(a), label) for a in args])
157 _buffers[-1].extend([(str(a), label) for a in args])
159 else:
158 else:
160 return orig(*[style(str(a), label) for a in args], **opts)
159 return orig(*[style(str(a), label) for a in args], **opts)
161
160
162 def write_err(orig, *args, **opts):
161 def write_err(orig, *args, **opts):
163 label = opts.get('label', '')
162 label = opts.get('label', '')
164 return orig(*[style(str(a), label) for a in args], **opts)
163 return orig(*[style(str(a), label) for a in args], **opts)
165
164
166 def uisetup(ui):
165 def uisetup(ui):
167 def colorcmd(orig, ui_, opts, cmd, cmdfunc):
166 def colorcmd(orig, ui_, opts, cmd, cmdfunc):
168 if (opts['color'] == 'always' or
167 if (opts['color'] == 'always' or
169 (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb'
168 (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb'
170 and sys.__stdout__.isatty()))):
169 and sys.__stdout__.isatty()))):
171 global _buffers
170 global _buffers
172 _buffers = ui_._buffers
171 _buffers = ui_._buffers
173 extensions.wrapfunction(ui_, 'popbuffer', popbuffer)
172 extensions.wrapfunction(ui_, 'popbuffer', popbuffer)
174 extensions.wrapfunction(ui_, 'write', write)
173 extensions.wrapfunction(ui_, 'write', write)
175 extensions.wrapfunction(ui_, 'write_err', write_err)
174 extensions.wrapfunction(ui_, 'write_err', write_err)
176 ui_.label = style
175 ui_.label = style
177 extstyles()
176 extstyles()
178 configstyles(ui)
177 configstyles(ui)
179 return orig(ui_, opts, cmd, cmdfunc)
178 return orig(ui_, opts, cmd, cmdfunc)
180 extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
179 extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
181
180
182 commands.globalopts.append(('', 'color', 'auto',
181 commands.globalopts.append(('', 'color', 'auto',
183 _("when to colorize (always, auto, or never)")))
182 _("when to colorize (always, auto, or never)")))
General Comments 0
You need to be logged in to leave comments. Login now