color.py
194 lines
| 7.0 KiB
| text/x-python
|
PythonLexer
/ hgext / color.py
|
r5787 | # color.py color output for the status and qseries commands | ||
# | ||||
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> | ||||
# | ||||
|
r5792 | # This program is free software; you can redistribute it and/or modify it | ||
|
r5787 | # under the terms of the GNU General Public License as published by the | ||
|
r5792 | # Free Software Foundation; either version 2 of the License, or (at your | ||
|
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 | ||||
|
r5792 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
|
r5787 | |||
'''add color output to the status and qseries commands | ||||
This extension modifies the status command to add color to its output to | ||||
reflect file status, and the qseries command to add color to reflect patch | ||||
status (applied, unapplied, missing). 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. | ||||
To enable this extension, add this to your .hgrc file: | ||||
[extensions] | ||||
color = | ||||
Default effects my be overriden from the .hgrc file: | ||||
[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 | ||||
|
r6855 | # 'none' turns off all effects | ||
|
r5787 | status.clean = none | ||
status.copied = none | ||||
qseries.applied = blue bold underline | ||||
qseries.unapplied = black bold | ||||
qseries.missing = red bold | ||||
''' | ||||
|
r7455 | import os, re, sys | ||
|
r5787 | |||
|
r7418 | from mercurial import commands, extensions | ||
|
r5787 | from mercurial.i18n import _ | ||
# start and stop parameters for effects | ||||
_effect_params = { 'none': (0, 0), | ||||
'black': (30, 39), | ||||
'red': (31, 39), | ||||
'green': (32, 39), | ||||
'yellow': (33, 39), | ||||
'blue': (34, 39), | ||||
'magenta': (35, 39), | ||||
'cyan': (36, 39), | ||||
'white': (37, 39), | ||||
'bold': (1, 22), | ||||
'italic': (3, 23), | ||||
'underline': (4, 24), | ||||
'inverse': (7, 27), | ||||
'black_background': (40, 49), | ||||
'red_background': (41, 49), | ||||
'green_background': (42, 49), | ||||
'yellow_background': (43, 49), | ||||
'blue_background': (44, 49), | ||||
'purple_background': (45, 49), | ||||
'cyan_background': (46, 49), | ||||
'white_background': (47, 49), } | ||||
def render_effects(text, *effects): | ||||
'Wrap text in commands to turn on each effect.' | ||||
|
r6856 | start = [ str(_effect_params['none'][0]) ] | ||
|
r5787 | stop = [] | ||
for effect in effects: | ||||
start.append(str(_effect_params[effect][0])) | ||||
stop.append(str(_effect_params[effect][1])) | ||||
|
r6856 | stop.append(str(_effect_params['none'][1])) | ||
|
r5787 | start = '\033[' + ';'.join(start) + 'm' | ||
stop = '\033[' + ';'.join(stop) + 'm' | ||||
return start + text + stop | ||||
|
r7216 | def colorstatus(orig, ui, repo, *pats, **opts): | ||
|
r5787 | '''run the status command with colored output''' | ||
delimiter = opts['print0'] and '\0' or '\n' | ||||
|
r7419 | nostatus = opts.get('no_status') | ||
opts['no_status'] = False | ||||
# run status and capture its output | ||||
|
r5787 | ui.pushbuffer() | ||
|
r7216 | retval = orig(ui, repo, *pats, **opts) | ||
|
r5787 | # filter out empty strings | ||
|
r7419 | lines_with_status = [ line for line in ui.popbuffer().split(delimiter) if line ] | ||
|
r5787 | |||
|
r7419 | if nostatus: | ||
lines = [l[2:] for l in lines_with_status] | ||||
|
r5787 | else: | ||
|
r7419 | lines = lines_with_status | ||
|
r5787 | |||
# apply color to output and display it | ||||
for i in xrange(0, len(lines)): | ||||
status = _status_abbreviations[lines_with_status[i][0]] | ||||
effects = _status_effects[status] | ||||
if effects: | ||||
lines[i] = render_effects(lines[i], *effects) | ||||
|
r7455 | ui.write(lines[i] + delimiter) | ||
|
r5787 | return retval | ||
_status_abbreviations = { 'M': 'modified', | ||||
'A': 'added', | ||||
'R': 'removed', | ||||
|
r5796 | '!': 'deleted', | ||
|
r5787 | '?': 'unknown', | ||
'I': 'ignored', | ||||
'C': 'clean', | ||||
' ': 'copied', } | ||||
_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', ), } | ||||
|
r7216 | def colorqseries(orig, ui, repo, *dummy, **opts): | ||
|
r5787 | '''run the qseries command with colored output''' | ||
ui.pushbuffer() | ||||
|
r7216 | retval = orig(ui, repo, **opts) | ||
|
r5787 | patches = ui.popbuffer().splitlines() | ||
for patch in patches: | ||||
|
r6855 | patchname = patch | ||
if opts['summary']: | ||||
patchname = patchname.split(': ')[0] | ||||
if ui.verbose: | ||||
patchname = patchname.split(' ', 2)[-1] | ||||
|
r5787 | if opts['missing']: | ||
effects = _patch_effects['missing'] | ||||
|
r6855 | # Determine if patch is applied. | ||
|
r5787 | elif [ applied for applied in repo.mq.applied | ||
|
r6855 | if patchname == applied.name ]: | ||
|
r5787 | effects = _patch_effects['applied'] | ||
else: | ||||
effects = _patch_effects['unapplied'] | ||||
|
r7455 | ui.write(render_effects(patch, *effects) + '\n') | ||
|
r5787 | return retval | ||
_patch_effects = { 'applied': ('blue', 'bold', 'underline'), | ||||
'missing': ('red', 'bold'), | ||||
'unapplied': ('black', 'bold'), } | ||||
def uisetup(ui): | ||||
'''Initialize the extension.''' | ||||
|
r7216 | _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects) | ||
|
r6854 | if ui.config('extensions', 'hgext.mq') is not None or \ | ||
ui.config('extensions', 'mq') is not None: | ||||
|
r5787 | from hgext import mq | ||
|
r7216 | _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects) | ||
|
r5787 | |||
|
r7216 | def _setupcmd(ui, cmd, table, func, effectsmap): | ||
'''patch in command to command table and load effect map''' | ||||
|
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) | ||||
if func is not None: | ||||
return func(orig, *args, **opts) | ||||
return orig(*args, **opts) | ||||
|
r5787 | |||
|
r7216 | entry = extensions.wrapcommand(table, cmd, nocolor) | ||
|
r7455 | entry[1].extend([ | ||
('', 'color', 'auto', _("when to colorize (always, auto, or never)")), | ||||
('', 'no-color', None, _("don't colorize output")), | ||||
]) | ||||
|
r5787 | |||
for status in effectsmap: | ||||
|
r7216 | effects = ui.config('color', cmd + '.' + status) | ||
|
r5787 | if effects: | ||
effectsmap[status] = re.split('\W+', effects) | ||||