comparison hgext/color.py @ 7457:a70fb83cbb9e

diff colorization: finish highlighting trailing whitespace
author Georg Brandl <georg@python.org>
date Wed, 26 Nov 2008 22:58:07 +0100
parents 79eb16db5e4a
children 3fb5c142a9f0
comparison
equal deleted inserted replaced
7456:79eb16db5e4a 7457:a70fb83cbb9e
19 '''add color output to status, qseries, and diff-related 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, the qseries command to add color to reflect patch status 22 reflect file status, the qseries command to add color to reflect patch status
23 (applied, unapplied, missing), and to diff-related commands to highlight 23 (applied, unapplied, missing), and to diff-related commands to highlight
24 additions, removals, diff headers, and trailing whitespace. Other effects in 24 additions, removals, diff headers, and trailing whitespace.
25 addition to color, like bold and underlined text, are also available. 25
26 Effects are rendered with the ECMA-48 SGR control function (aka ANSI escape 26 Other effects in addition to color, like bold and underlined text, are also
27 codes). This module also provides the render_text function, which can be 27 available. Effects are rendered with the ECMA-48 SGR control function (aka
28 used to add effects to any text. 28 ANSI escape codes). This module also provides the render_text function,
29 which can be used to add effects to any text.
29 30
30 To enable this extension, add this to your .hgrc file: 31 To enable this extension, add this to your .hgrc file:
31 [extensions] 32 [extensions]
32 color = 33 color =
33 34
55 diff.file_b = green bold 56 diff.file_b = green bold
56 diff.hunk = magenta 57 diff.hunk = magenta
57 diff.deleted = red 58 diff.deleted = red
58 diff.inserted = green 59 diff.inserted = green
59 diff.changed = white 60 diff.changed = white
60 diff.whitespace = bold red_background 61 diff.trailingwhitespace = bold red_background
61 ''' 62 '''
62 63
63 import os, re, sys 64 import os, re, sys
64 65
65 from mercurial import cmdutil, commands, extensions 66 from mercurial import cmdutil, commands, extensions
174 175
175 def colorwrap(orig, s): 176 def colorwrap(orig, s):
176 '''wrap ui.write for colored diff output''' 177 '''wrap ui.write for colored diff output'''
177 lines = s.split('\n') 178 lines = s.split('\n')
178 for i, line in enumerate(lines): 179 for i, line in enumerate(lines):
180 stripline = line
181 if line and line[0] in '+-':
182 # highlight trailing whitespace, but only in changed lines
183 stripline = line.rstrip()
179 for prefix, style in _diff_prefixes: 184 for prefix, style in _diff_prefixes:
180 if line.startswith(prefix): 185 if stripline.startswith(prefix):
181 effects = _diff_effects[style] 186 lines[i] = render_effects(stripline, *_diff_effects[style])
182 lines[i] = render_effects(line, *_diff_effects[style])
183 break 187 break
188 if line != stripline:
189 lines[i] += render_effects(
190 line[len(stripline):], *_diff_effects['trailingwhitespace'])
184 orig('\n'.join(lines)) 191 orig('\n'.join(lines))
185 192
186 def colorshowpatch(orig, self, node): 193 def colorshowpatch(orig, self, node):
187 '''wrap cmdutil.changeset_printer.showpatch with colored output''' 194 '''wrap cmdutil.changeset_printer.showpatch with colored output'''
188 oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap) 195 oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap)
215 'file_a': ('red', 'bold'), 222 'file_a': ('red', 'bold'),
216 'file_b': ('green', 'bold'), 223 'file_b': ('green', 'bold'),
217 'hunk': ('magenta',), 224 'hunk': ('magenta',),
218 'deleted': ('red',), 225 'deleted': ('red',),
219 'inserted': ('green',), 226 'inserted': ('green',),
220 'changed': ('white',)} 227 'changed': ('white',),
228 'trailingwhitespace': ('bold', 'red_background'),}
221 229
222 def uisetup(ui): 230 def uisetup(ui):
223 '''Initialize the extension.''' 231 '''Initialize the extension.'''
224 _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects) 232 _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
225 _setupcmd(ui, 'incoming', commands.table, None, _diff_effects) 233 _setupcmd(ui, 'incoming', commands.table, None, _diff_effects)