##// END OF EJS Templates
color: diff colorization...
Brodie Rao -
r7456:79eb16db default
parent child Browse files
Show More
@@ -16,15 +16,16 b''
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 status, qseries, and diff-related 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, the qseries command to add color to reflect patch status
23 status (applied, unapplied, missing). Other effects in addition to color,
23 (applied, unapplied, missing), and to diff-related commands to highlight
24 like bold and underlined text, are also available. Effects are rendered
24 additions, removals, diff headers, and trailing whitespace. Other effects in
25 with the ECMA-48 SGR control function (aka ANSI escape codes). This module
25 addition to color, like bold and underlined text, are also available.
26 also provides the render_text function, which can be used to add effects to
26 Effects are rendered with the ECMA-48 SGR control function (aka ANSI escape
27 any text.
27 codes). This module also provides the render_text function, which can be
28 used to add effects to any text.
28
29
29 To enable this extension, add this to your .hgrc file:
30 To enable this extension, add this to your .hgrc file:
30 [extensions]
31 [extensions]
@@ -47,11 +48,21 b' status.copied = none'
47 qseries.applied = blue bold underline
48 qseries.applied = blue bold underline
48 qseries.unapplied = black bold
49 qseries.unapplied = black bold
49 qseries.missing = red bold
50 qseries.missing = red bold
51
52 diff.diffline = bold
53 diff.extended = cyan bold
54 diff.file_a = red bold
55 diff.file_b = green bold
56 diff.hunk = magenta
57 diff.deleted = red
58 diff.inserted = green
59 diff.changed = white
60 diff.whitespace = bold red_background
50 '''
61 '''
51
62
52 import os, re, sys
63 import os, re, sys
53
64
54 from mercurial import commands, extensions
65 from mercurial import cmdutil, commands, extensions
55 from mercurial.i18n import _
66 from mercurial.i18n import _
56
67
57 # start and stop parameters for effects
68 # start and stop parameters for effects
@@ -161,12 +172,65 b' def colorqseries(orig, ui, repo, *dummy,'
161 'missing': ('red', 'bold'),
172 'missing': ('red', 'bold'),
162 'unapplied': ('black', 'bold'), }
173 'unapplied': ('black', 'bold'), }
163
174
175 def colorwrap(orig, s):
176 '''wrap ui.write for colored diff output'''
177 lines = s.split('\n')
178 for i, line in enumerate(lines):
179 for prefix, style in _diff_prefixes:
180 if line.startswith(prefix):
181 effects = _diff_effects[style]
182 lines[i] = render_effects(line, *_diff_effects[style])
183 break
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'),
205 ('new', 'extended'),
206 ('deleted', 'extended'),
207 ('---', 'file_a'),
208 ('+++', 'file_b'),
209 ('@', 'hunk'),
210 ('-', 'deleted'),
211 ('+', 'inserted')]
212
213 _diff_effects = {'diffline': ('bold',),
214 'extended': ('cyan', 'bold'),
215 'file_a': ('red', 'bold'),
216 'file_b': ('green', 'bold'),
217 'hunk': ('magenta',),
218 'deleted': ('red',),
219 'inserted': ('green',),
220 'changed': ('white',)}
221
164 def uisetup(ui):
222 def uisetup(ui):
165 '''Initialize the extension.'''
223 '''Initialize the extension.'''
224 _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
225 _setupcmd(ui, 'incoming', commands.table, None, _diff_effects)
226 _setupcmd(ui, 'log', commands.table, None, _diff_effects)
227 _setupcmd(ui, 'outgoing', commands.table, None, _diff_effects)
228 _setupcmd(ui, 'tip', commands.table, None, _diff_effects)
166 _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
229 _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
167 if ui.config('extensions', 'hgext.mq') is not None or \
230 if ui.config('extensions', 'hgext.mq') is not None or \
168 ui.config('extensions', 'mq') is not None:
231 ui.config('extensions', 'mq') is not None:
169 from hgext import mq
232 from hgext import mq
233 _setupcmd(ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects)
170 _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
234 _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
171
235
172 def _setupcmd(ui, cmd, table, func, effectsmap):
236 def _setupcmd(ui, cmd, table, func, effectsmap):
@@ -178,9 +242,14 b' def _setupcmd(ui, cmd, table, func, effe'
178 or not sys.__stdout__.isatty()))):
242 or not sys.__stdout__.isatty()))):
179 return orig(*args, **opts)
243 return orig(*args, **opts)
180
244
181 if func is not None:
245 oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer,
182 return func(orig, *args, **opts)
246 'showpatch', colorshowpatch)
183 return orig(*args, **opts)
247 try:
248 if func is not None:
249 return func(orig, *args, **opts)
250 return orig(*args, **opts)
251 finally:
252 cmdutil.changeset_printer.showpatch = oldshowpatch
184
253
185 entry = extensions.wrapcommand(table, cmd, nocolor)
254 entry = extensions.wrapcommand(table, cmd, nocolor)
186 entry[1].extend([
255 entry[1].extend([
General Comments 0
You need to be logged in to leave comments. Login now