hgext/color.py
author Brodie Rao <me+hg@dackz.net>
Tue, 02 Dec 2008 16:46:45 +0100
changeset 7456 79eb16db5e4a
parent 7455 c9fd5474a707
child 7457 a70fb83cbb9e
permissions -rw-r--r--
color: diff colorization This colorizes diff, qdiff, log -p, outgoing -p, incoming -p, and tip -p.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     1
# color.py color output for the status and qseries commands
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     2
#
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     3
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com>
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     4
#
5792
956e01c31914 color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents: 5787
diff changeset
     5
# This program is free software; you can redistribute it and/or modify it
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     6
# under the terms of the GNU General Public License as published by the
5792
956e01c31914 color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents: 5787
diff changeset
     7
# Free Software Foundation; either version 2 of the License, or (at your
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     8
# option) any later version.
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     9
#
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    10
# This program is distributed in the hope that it will be useful, but
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    11
# WITHOUT ANY WARRANTY; without even the implied warranty of
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    13
# Public License for more details.
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    14
#
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License along
5792
956e01c31914 color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents: 5787
diff changeset
    16
# with this program; if not, write to the Free Software Foundation, Inc.,
956e01c31914 color extension: change from GPL3 to 2
Kevin Christen <kevin.christen@gmail.com>
parents: 5787
diff changeset
    17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    18
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    19
'''add color output to status, qseries, and diff-related commands
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    20
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    21
This extension modifies the status command to add color to its output to
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    22
reflect file status, the qseries command to add color to reflect patch status
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    23
(applied, unapplied, missing), and to diff-related commands to highlight
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    24
additions, removals, diff headers, and trailing whitespace.  Other effects in
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    25
addition to color, like bold and underlined text, are also available.
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    26
Effects are rendered with the ECMA-48 SGR control function (aka ANSI escape
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    27
codes).  This module also provides the render_text function, which can be
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    28
used to add effects to any text.
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    29
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    30
To enable this extension, add this to your .hgrc file:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    31
[extensions]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    32
color =
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    33
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    34
Default effects my be overriden from the .hgrc file:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    35
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    36
[color]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    37
status.modified = blue bold underline red_background
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    38
status.added = green bold
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    39
status.removed = red bold blue_background
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    40
status.deleted = cyan bold underline
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    41
status.unknown = magenta bold underline
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    42
status.ignored = black bold
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    43
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
    44
# 'none' turns off all effects
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    45
status.clean = none
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    46
status.copied = none
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    47
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    48
qseries.applied = blue bold underline
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    49
qseries.unapplied = black bold
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    50
qseries.missing = red bold
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    51
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    52
diff.diffline = bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    53
diff.extended = cyan bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    54
diff.file_a = red bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    55
diff.file_b = green bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    56
diff.hunk = magenta
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    57
diff.deleted = red
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    58
diff.inserted = green
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    59
diff.changed = white
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    60
diff.whitespace = bold red_background
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    61
'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    62
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
    63
import os, re, sys
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    64
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    65
from mercurial import cmdutil, commands, extensions
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    66
from mercurial.i18n import _
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    67
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    68
# start and stop parameters for effects
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    69
_effect_params = { 'none': (0, 0),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    70
                   'black': (30, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    71
                   'red': (31, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    72
                   'green': (32, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    73
                   'yellow': (33, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    74
                   'blue': (34, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    75
                   'magenta': (35, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    76
                   'cyan': (36, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    77
                   'white': (37, 39),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    78
                   'bold': (1, 22),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    79
                   'italic': (3, 23),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    80
                   'underline': (4, 24),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    81
                   'inverse': (7, 27),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    82
                   'black_background': (40, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    83
                   'red_background': (41, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    84
                   'green_background': (42, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    85
                   'yellow_background': (43, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    86
                   'blue_background': (44, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    87
                   'purple_background': (45, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    88
                   'cyan_background': (46, 49),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    89
                   'white_background': (47, 49), }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    90
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    91
def render_effects(text, *effects):
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    92
    'Wrap text in commands to turn on each effect.'
6856
c6890cfc2253 Add a reset before and after colorized output
Kevin Christen <kevin.christen@gmail.com>
parents: 6855
diff changeset
    93
    start = [ str(_effect_params['none'][0]) ]
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    94
    stop = []
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    95
    for effect in effects:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    96
        start.append(str(_effect_params[effect][0]))
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    97
        stop.append(str(_effect_params[effect][1]))
6856
c6890cfc2253 Add a reset before and after colorized output
Kevin Christen <kevin.christen@gmail.com>
parents: 6855
diff changeset
    98
    stop.append(str(_effect_params['none'][1]))
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    99
    start = '\033[' + ';'.join(start) + 'm'
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   100
    stop = '\033[' + ';'.join(stop) + 'm'
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   101
    return start + text + stop
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   102
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   103
def colorstatus(orig, ui, repo, *pats, **opts):
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   104
    '''run the status command with colored output'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   105
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   106
    delimiter = opts['print0'] and '\0' or '\n'
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   107
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   108
    nostatus = opts.get('no_status')
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   109
    opts['no_status'] = False
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   110
    # run status and capture its output
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   111
    ui.pushbuffer()
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   112
    retval = orig(ui, repo, *pats, **opts)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   113
    # filter out empty strings
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   114
    lines_with_status = [ line for line in ui.popbuffer().split(delimiter) if line ]
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   115
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   116
    if nostatus:
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   117
        lines = [l[2:] for l in lines_with_status]
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   118
    else:
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   119
        lines = lines_with_status
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   120
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   121
    # apply color to output and display it
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   122
    for i in xrange(0, len(lines)):
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   123
        status = _status_abbreviations[lines_with_status[i][0]]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   124
        effects = _status_effects[status]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   125
        if effects:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   126
            lines[i] = render_effects(lines[i], *effects)
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   127
        ui.write(lines[i] + delimiter)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   128
    return retval
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   129
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   130
_status_abbreviations = { 'M': 'modified',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   131
                          'A': 'added',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   132
                          'R': 'removed',
5796
7705d308eb5e Fix status char in color extension for deleted (missing) files.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5792
diff changeset
   133
                          '!': 'deleted',
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   134
                          '?': 'unknown',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   135
                          'I': 'ignored',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   136
                          'C': 'clean',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   137
                          ' ': 'copied', }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   138
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   139
_status_effects = { 'modified': ('blue', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   140
                    'added': ('green', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   141
                    'removed': ('red', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   142
                    'deleted': ('cyan', 'bold', 'underline'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   143
                    'unknown': ('magenta', 'bold', 'underline'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   144
                    'ignored': ('black', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   145
                    'clean': ('none', ),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   146
                    'copied': ('none', ), }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   147
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   148
def colorqseries(orig, ui, repo, *dummy, **opts):
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   149
    '''run the qseries command with colored output'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   150
    ui.pushbuffer()
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   151
    retval = orig(ui, repo, **opts)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   152
    patches = ui.popbuffer().splitlines()
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   153
    for patch in patches:
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   154
        patchname = patch
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   155
        if opts['summary']:
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   156
            patchname = patchname.split(': ')[0]
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   157
        if ui.verbose:
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   158
            patchname = patchname.split(' ', 2)[-1]
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   159
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   160
        if opts['missing']:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   161
            effects = _patch_effects['missing']
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   162
        # Determine if patch is applied.
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   163
        elif [ applied for applied in repo.mq.applied
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   164
               if patchname == applied.name ]:
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   165
            effects = _patch_effects['applied']
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   166
        else:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   167
            effects = _patch_effects['unapplied']
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   168
        ui.write(render_effects(patch, *effects) + '\n')
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   169
    return retval
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   170
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   171
_patch_effects = { 'applied': ('blue', 'bold', 'underline'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   172
                   'missing': ('red', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   173
                   'unapplied': ('black', 'bold'), }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   174
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   175
def colorwrap(orig, s):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   176
    '''wrap ui.write for colored diff output'''
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   177
    lines = s.split('\n')
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   178
    for i, line in enumerate(lines):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   179
        for prefix, style in _diff_prefixes:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   180
            if line.startswith(prefix):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   181
                effects = _diff_effects[style]
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   182
                lines[i] = render_effects(line, *_diff_effects[style])
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   183
                break
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   184
    orig('\n'.join(lines))
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   185
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   186
def colorshowpatch(orig, self, node):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   187
    '''wrap cmdutil.changeset_printer.showpatch with colored output'''
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   188
    oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   189
    try:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   190
        orig(self, node)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   191
    finally:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   192
        self.ui.write = oldwrite
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   193
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   194
def colordiff(orig, ui, repo, *pats, **opts):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   195
    '''run the diff command with colored output'''
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   196
    oldwrite = extensions.wrapfunction(ui, 'write', colorwrap)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   197
    try:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   198
        orig(ui, repo, *pats, **opts)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   199
    finally:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   200
        ui.write = oldwrite
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   201
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   202
_diff_prefixes = [('diff', 'diffline'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   203
                  ('copy', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   204
                  ('rename', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   205
                  ('new', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   206
                  ('deleted', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   207
                  ('---', 'file_a'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   208
                  ('+++', 'file_b'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   209
                  ('@', 'hunk'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   210
                  ('-', 'deleted'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   211
                  ('+', 'inserted')]
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   212
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   213
_diff_effects = {'diffline': ('bold',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   214
                 'extended': ('cyan', 'bold'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   215
                 'file_a': ('red', 'bold'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   216
                 'file_b': ('green', 'bold'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   217
                 'hunk': ('magenta',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   218
                 'deleted': ('red',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   219
                 'inserted': ('green',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   220
                 'changed': ('white',)}
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   221
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   222
def uisetup(ui):
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   223
    '''Initialize the extension.'''
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   224
    _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   225
    _setupcmd(ui, 'incoming', commands.table, None, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   226
    _setupcmd(ui, 'log', commands.table, None, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   227
    _setupcmd(ui, 'outgoing', commands.table, None, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   228
    _setupcmd(ui, 'tip', commands.table, None, _diff_effects)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   229
    _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
6854
9d11faecb0f1 color: improve mq extension detection
Patrick Mezard <pmezard@gmail.com>
parents: 6212
diff changeset
   230
    if ui.config('extensions', 'hgext.mq') is not None or \
9d11faecb0f1 color: improve mq extension detection
Patrick Mezard <pmezard@gmail.com>
parents: 6212
diff changeset
   231
            ui.config('extensions', 'mq') is not None:
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   232
        from hgext import mq
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   233
        _setupcmd(ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   234
        _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   235
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   236
def _setupcmd(ui, cmd, table, func, effectsmap):
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   237
    '''patch in command to command table and load effect map'''
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   238
    def nocolor(orig, *args, **opts):
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   239
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   240
        if (opts['no_color'] or opts['color'] == 'never' or
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   241
            (opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb'
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   242
                                          or not sys.__stdout__.isatty()))):
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   243
            return orig(*args, **opts)
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   244
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   245
        oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer,
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   246
                                               'showpatch', colorshowpatch)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   247
        try:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   248
            if func is not None:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   249
                return func(orig, *args, **opts)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   250
            return orig(*args, **opts)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   251
        finally:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   252
            cmdutil.changeset_printer.showpatch = oldshowpatch
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   253
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   254
    entry = extensions.wrapcommand(table, cmd, nocolor)
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   255
    entry[1].extend([
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   256
        ('', 'color', 'auto', _("when to colorize (always, auto, or never)")),
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   257
        ('', 'no-color', None, _("don't colorize output")),
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   258
    ])
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   259
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   260
    for status in effectsmap:
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   261
        effects = ui.config('color', cmd + '.' + status)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   262
        if effects:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   263
            effectsmap[status] = re.split('\W+', effects)