##// END OF EJS Templates
gpg: add test
gpg: add test

File last commit:

r8685:5306c6b0 default
r8809:6fce3633 default
Show More
color.py
270 lines | 9.6 KiB | text/x-python | PythonLexer
Kevin Christen
Add colored output to status and qseries commands
r5787 # color.py color output for the status and qseries commands
#
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com>
#
Kevin Christen
color extension: change from GPL3 to 2
r5792 # This program is free software; you can redistribute it and/or modify it
Kevin Christen
Add colored output to status and qseries commands
r5787 # under the terms of the GNU General Public License as published by the
Kevin Christen
color extension: change from GPL3 to 2
r5792 # Free Software Foundation; either version 2 of the License, or (at your
Kevin Christen
Add colored output to status and qseries commands
r5787 # option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
Kevin Christen
color extension: change from GPL3 to 2
r5792 # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Kevin Christen
Add colored output to status and qseries commands
r5787
Brodie Rao
color: diff colorization...
r7456 '''add color output to status, qseries, and diff-related commands
Kevin Christen
Add colored output to status and qseries commands
r5787
Martin Geisler
color: word-wrap help texts at 70 characters
r7988 This extension modifies the status command to add color to its output
to reflect file status, the qseries command to add color to reflect
patch status (applied, unapplied, missing), and to diff-related
commands to highlight additions, removals, diff headers, and trailing
whitespace.
Georg Brandl
diff colorization: finish highlighting trailing whitespace
r7457
Martin Geisler
color: word-wrap help texts at 70 characters
r7988 Other effects in addition to color, like bold and underlined text, are
also available. Effects are rendered with the ECMA-48 SGR control
function (aka ANSI escape codes). This module also provides the
render_text function, which can be used to add effects to any text.
Kevin Christen
Add colored output to status and qseries commands
r5787
To enable this extension, add this to your .hgrc file:
[extensions]
color =
Martin Geisler
color: yet another typo
r8685 Default effects may be overridden from the .hgrc file:
Kevin Christen
Add colored output to status and qseries commands
r5787
[color]
status.modified = blue bold underline red_background
status.added = green bold
status.removed = red bold blue_background
status.deleted = cyan bold underline
status.unknown = magenta bold underline
status.ignored = black bold
Kevin Christen
Apply color to output of qseries --verbose...
r6855 # 'none' turns off all effects
Kevin Christen
Add colored output to status and qseries commands
r5787 status.clean = none
status.copied = none
qseries.applied = blue bold underline
qseries.unapplied = black bold
qseries.missing = red bold
Brodie Rao
color: diff colorization...
r7456
diff.diffline = bold
diff.extended = cyan bold
diff.file_a = red bold
diff.file_b = green bold
diff.hunk = magenta
diff.deleted = red
diff.inserted = green
diff.changed = white
Georg Brandl
diff colorization: finish highlighting trailing whitespace
r7457 diff.trailingwhitespace = bold red_background
Kevin Christen
Add colored output to status and qseries commands
r5787 '''
Martin Geisler
color: replace re.split with ui.configlist
r8623 import os, sys
Kevin Christen
Add colored output to status and qseries commands
r5787
Brodie Rao
color: diff colorization...
r7456 from mercurial import cmdutil, commands, extensions
Kevin Christen
Add colored output to status and qseries commands
r5787 from mercurial.i18n import _
# start and stop parameters for effects
Brodie Rao
color: replace effect-specific reset control codes with general purpose one
r7459 _effect_params = {'none': 0,
'black': 30,
'red': 31,
'green': 32,
'yellow': 33,
'blue': 34,
'magenta': 35,
'cyan': 36,
'white': 37,
'bold': 1,
'italic': 3,
'underline': 4,
'inverse': 7,
'black_background': 40,
'red_background': 41,
'green_background': 42,
'yellow_background': 43,
'blue_background': 44,
'purple_background': 45,
'cyan_background': 46,
'white_background': 47}
Kevin Christen
Add colored output to status and qseries commands
r5787
Martin Geisler
color: use lists instead of tuples for effects
r8622 def render_effects(text, effects):
Kevin Christen
Add colored output to status and qseries commands
r5787 'Wrap text in commands to turn on each effect.'
Martin Geisler
color: use lists instead of tuples for effects
r8622 start = [str(_effect_params[e]) for e in ['none'] + effects]
Kevin Christen
Add colored output to status and qseries commands
r5787 start = '\033[' + ';'.join(start) + 'm'
Brodie Rao
color: replace effect-specific reset control codes with general purpose one
r7459 stop = '\033[' + str(_effect_params['none']) + 'm'
return ''.join([start, text, stop])
Kevin Christen
Add colored output to status and qseries commands
r5787
Matt Mackall
extensions: use new wrapper functions
r7216 def colorstatus(orig, ui, repo, *pats, **opts):
Kevin Christen
Add colored output to status and qseries commands
r5787 '''run the status command with colored output'''
delimiter = opts['print0'] and '\0' or '\n'
Brendan Cully
color: don't run status twice for -n...
r7419 nostatus = opts.get('no_status')
opts['no_status'] = False
# run status and capture its output
Kevin Christen
Add colored output to status and qseries commands
r5787 ui.pushbuffer()
Matt Mackall
extensions: use new wrapper functions
r7216 retval = orig(ui, repo, *pats, **opts)
Kevin Christen
Add colored output to status and qseries commands
r5787 # filter out empty strings
Brendan Cully
color: don't run status twice for -n...
r7419 lines_with_status = [ line for line in ui.popbuffer().split(delimiter) if line ]
Kevin Christen
Add colored output to status and qseries commands
r5787
Brendan Cully
color: don't run status twice for -n...
r7419 if nostatus:
lines = [l[2:] for l in lines_with_status]
Kevin Christen
Add colored output to status and qseries commands
r5787 else:
Brendan Cully
color: don't run status twice for -n...
r7419 lines = lines_with_status
Kevin Christen
Add colored output to status and qseries commands
r5787
# apply color to output and display it
Martin Geisler
replace xrange(0, n) with xrange(n)
r8624 for i in xrange(len(lines)):
Kevin Christen
Add colored output to status and qseries commands
r5787 status = _status_abbreviations[lines_with_status[i][0]]
effects = _status_effects[status]
if effects:
Martin Geisler
color: use lists instead of tuples for effects
r8622 lines[i] = render_effects(lines[i], effects)
Brodie Rao
color: add --color switch...
r7455 ui.write(lines[i] + delimiter)
Kevin Christen
Add colored output to status and qseries commands
r5787 return retval
_status_abbreviations = { 'M': 'modified',
'A': 'added',
'R': 'removed',
Thomas Arendsen Hein
Fix status char in color extension for deleted (missing) files.
r5796 '!': 'deleted',
Kevin Christen
Add colored output to status and qseries commands
r5787 '?': 'unknown',
'I': 'ignored',
'C': 'clean',
' ': 'copied', }
Martin Geisler
color: use lists instead of tuples for effects
r8622 _status_effects = { 'modified': ['blue', 'bold'],
'added': ['green', 'bold'],
'removed': ['red', 'bold'],
'deleted': ['cyan', 'bold', 'underline'],
'unknown': ['magenta', 'bold', 'underline'],
'ignored': ['black', 'bold'],
'clean': ['none'],
'copied': ['none'], }
Kevin Christen
Add colored output to status and qseries commands
r5787
Matt Mackall
extensions: use new wrapper functions
r7216 def colorqseries(orig, ui, repo, *dummy, **opts):
Kevin Christen
Add colored output to status and qseries commands
r5787 '''run the qseries command with colored output'''
ui.pushbuffer()
Matt Mackall
extensions: use new wrapper functions
r7216 retval = orig(ui, repo, **opts)
Kevin Christen
Add colored output to status and qseries commands
r5787 patches = ui.popbuffer().splitlines()
for patch in patches:
Kevin Christen
Apply color to output of qseries --verbose...
r6855 patchname = patch
if opts['summary']:
patchname = patchname.split(': ')[0]
if ui.verbose:
patchname = patchname.split(' ', 2)[-1]
Kevin Christen
Add colored output to status and qseries commands
r5787 if opts['missing']:
effects = _patch_effects['missing']
Kevin Christen
Apply color to output of qseries --verbose...
r6855 # Determine if patch is applied.
Kevin Christen
Add colored output to status and qseries commands
r5787 elif [ applied for applied in repo.mq.applied
Kevin Christen
Apply color to output of qseries --verbose...
r6855 if patchname == applied.name ]:
Kevin Christen
Add colored output to status and qseries commands
r5787 effects = _patch_effects['applied']
else:
effects = _patch_effects['unapplied']
Martin Geisler
color: use lists instead of tuples for effects
r8622 ui.write(render_effects(patch, effects) + '\n')
Kevin Christen
Add colored output to status and qseries commands
r5787 return retval
Martin Geisler
color: use lists instead of tuples for effects
r8622 _patch_effects = { 'applied': ['blue', 'bold', 'underline'],
'missing': ['red', 'bold'],
'unapplied': ['black', 'bold'], }
Kevin Christen
Add colored output to status and qseries commands
r5787
Brodie Rao
color: diff colorization...
r7456 def colorwrap(orig, s):
'''wrap ui.write for colored diff output'''
lines = s.split('\n')
for i, line in enumerate(lines):
Georg Brandl
diff colorization: finish highlighting trailing whitespace
r7457 stripline = line
if line and line[0] in '+-':
# highlight trailing whitespace, but only in changed lines
stripline = line.rstrip()
Brodie Rao
color: diff colorization...
r7456 for prefix, style in _diff_prefixes:
Georg Brandl
diff colorization: finish highlighting trailing whitespace
r7457 if stripline.startswith(prefix):
Martin Geisler
color: use lists instead of tuples for effects
r8622 lines[i] = render_effects(stripline, _diff_effects[style])
Brodie Rao
color: diff colorization...
r7456 break
Georg Brandl
diff colorization: finish highlighting trailing whitespace
r7457 if line != stripline:
lines[i] += render_effects(
Martin Geisler
color: use lists instead of tuples for effects
r8622 line[len(stripline):], _diff_effects['trailingwhitespace'])
Brodie Rao
color: diff colorization...
r7456 orig('\n'.join(lines))
def colorshowpatch(orig, self, node):
'''wrap cmdutil.changeset_printer.showpatch with colored output'''
oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap)
try:
orig(self, node)
finally:
self.ui.write = oldwrite
def colordiff(orig, ui, repo, *pats, **opts):
'''run the diff command with colored output'''
oldwrite = extensions.wrapfunction(ui, 'write', colorwrap)
try:
orig(ui, repo, *pats, **opts)
finally:
ui.write = oldwrite
_diff_prefixes = [('diff', 'diffline'),
('copy', 'extended'),
('rename', 'extended'),
Gilles Moris
color: fix colorization of the 'old mode' git diff metadata
r7539 ('old', 'extended'),
Brodie Rao
color: diff colorization...
r7456 ('new', 'extended'),
('deleted', 'extended'),
('---', 'file_a'),
('+++', 'file_b'),
('@', 'hunk'),
('-', 'deleted'),
('+', 'inserted')]
Martin Geisler
color: cleanup extra commas
r8630 _diff_effects = {'diffline': ['bold'],
Martin Geisler
color: use lists instead of tuples for effects
r8622 'extended': ['cyan', 'bold'],
'file_a': ['red', 'bold'],
'file_b': ['green', 'bold'],
Martin Geisler
color: cleanup extra commas
r8630 'hunk': ['magenta'],
'deleted': ['red'],
'inserted': ['green'],
'changed': ['white'],
'trailingwhitespace': ['bold', 'red_background']}
Brodie Rao
color: diff colorization...
r7456
Kevin Christen
Add colored output to status and qseries commands
r5787 def uisetup(ui):
'''Initialize the extension.'''
Brodie Rao
color: diff colorization...
r7456 _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
_setupcmd(ui, 'incoming', commands.table, None, _diff_effects)
_setupcmd(ui, 'log', commands.table, None, _diff_effects)
_setupcmd(ui, 'outgoing', commands.table, None, _diff_effects)
_setupcmd(ui, 'tip', commands.table, None, _diff_effects)
Matt Mackall
extensions: use new wrapper functions
r7216 _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
Martin Geisler
color: look for mq with extensions.find
r8278 try:
mq = extensions.find('mq')
Brodie Rao
color: diff colorization...
r7456 _setupcmd(ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects)
Matt Mackall
extensions: use new wrapper functions
r7216 _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
Martin Geisler
color: look for mq with extensions.find
r8278 except KeyError:
# The mq extension is not enabled
pass
Kevin Christen
Add colored output to status and qseries commands
r5787
Matt Mackall
extensions: use new wrapper functions
r7216 def _setupcmd(ui, cmd, table, func, effectsmap):
'''patch in command to command table and load effect map'''
Brodie Rao
color: add --color switch...
r7455 def nocolor(orig, *args, **opts):
if (opts['no_color'] or opts['color'] == 'never' or
(opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb'
or not sys.__stdout__.isatty()))):
return orig(*args, **opts)
Brodie Rao
color: diff colorization...
r7456 oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer,
'showpatch', colorshowpatch)
try:
if func is not None:
return func(orig, *args, **opts)
return orig(*args, **opts)
finally:
cmdutil.changeset_printer.showpatch = oldshowpatch
Kevin Christen
Add colored output to status and qseries commands
r5787
Matt Mackall
extensions: use new wrapper functions
r7216 entry = extensions.wrapcommand(table, cmd, nocolor)
Brodie Rao
color: add --color switch...
r7455 entry[1].extend([
('', 'color', 'auto', _("when to colorize (always, auto, or never)")),
('', 'no-color', None, _("don't colorize output")),
])
Kevin Christen
Add colored output to status and qseries commands
r5787
for status in effectsmap:
Martin Geisler
color: replace re.split with ui.configlist
r8623 effects = ui.configlist('color', cmd + '.' + status)
Kevin Christen
Add colored output to status and qseries commands
r5787 if effects:
Martin Geisler
color: replace re.split with ui.configlist
r8623 effectsmap[status] = effects