hgext/color.py
author Sune Foldager <cryo@cyanite.org>
Mon, 04 May 2009 20:26:27 +0200
changeset 8333 89c80c3dc584
parent 8278 1f9787de17d9
child 8622 0a4f6e1b78dc
permissions -rw-r--r--
allow http authentication information to be specified in the configuration
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
7988
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    21
This extension modifies the status command to add color to its output
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    22
to reflect file status, the qseries command to add color to reflect
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    23
patch status (applied, unapplied, missing), and to diff-related
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    24
commands to highlight additions, removals, diff headers, and trailing
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    25
whitespace.
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
    26
7988
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    27
Other effects in addition to color, like bold and underlined text, are
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    28
also available. Effects are rendered with the ECMA-48 SGR control
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    29
function (aka ANSI escape codes). This module also provides the
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    30
render_text function, which can be 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
    31
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    32
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
    33
[extensions]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    34
color =
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
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
    37
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    38
[color]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    39
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
    40
status.added = green bold
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    41
status.removed = red bold blue_background
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    42
status.deleted = cyan bold underline
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    43
status.unknown = magenta bold underline
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    44
status.ignored = black bold
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    45
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
    46
# 'none' turns off all effects
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    47
status.clean = none
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    48
status.copied = none
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    49
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    50
qseries.applied = blue bold underline
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    51
qseries.unapplied = black bold
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    52
qseries.missing = red bold
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    53
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    54
diff.diffline = bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    55
diff.extended = cyan bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    56
diff.file_a = red bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    57
diff.file_b = green bold
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    58
diff.hunk = magenta
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    59
diff.deleted = red
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    60
diff.inserted = green
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    61
diff.changed = white
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
    62
diff.trailingwhitespace = bold red_background
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    63
'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    64
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
    65
import os, re, sys
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    66
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    67
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
    68
from mercurial.i18n import _
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    69
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    70
# start and stop parameters for effects
7459
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    71
_effect_params = {'none': 0,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    72
                  'black': 30,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    73
                  'red': 31,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    74
                  'green': 32,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    75
                  'yellow': 33,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    76
                  'blue': 34,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    77
                  'magenta': 35,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    78
                  'cyan': 36,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    79
                  'white': 37,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    80
                  'bold': 1,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    81
                  'italic': 3,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    82
                  'underline': 4,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    83
                  'inverse': 7,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    84
                  'black_background': 40,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    85
                  'red_background': 41,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    86
                  'green_background': 42,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    87
                  'yellow_background': 43,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    88
                  'blue_background': 44,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    89
                  'purple_background': 45,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    90
                  'cyan_background': 46,
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    91
                  'white_background': 47}
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    92
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    93
def render_effects(text, *effects):
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    94
    'Wrap text in commands to turn on each effect.'
7459
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    95
    start = [str(_effect_params[e]) for e in ('none',) + effects]
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    96
    start = '\033[' + ';'.join(start) + 'm'
7459
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    97
    stop = '\033[' + str(_effect_params['none']) + 'm'
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
    98
    return ''.join([start, text, stop])
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    99
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   100
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
   101
    '''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
   102
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   103
    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
   104
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   105
    nostatus = opts.get('no_status')
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   106
    opts['no_status'] = False
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   107
    # 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
   108
    ui.pushbuffer()
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   109
    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
   110
    # filter out empty strings
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   111
    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
   112
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   113
    if nostatus:
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   114
        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
   115
    else:
7419
efe31fbe6cf0 color: don't run status twice for -n
Brendan Cully <brendan@kublai.com>
parents: 7418
diff changeset
   116
        lines = lines_with_status
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   117
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   118
    # 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
   119
    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
   120
        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
   121
        effects = _status_effects[status]
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   122
        if effects:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   123
            lines[i] = render_effects(lines[i], *effects)
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   124
        ui.write(lines[i] + delimiter)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   125
    return retval
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   126
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   127
_status_abbreviations = { 'M': 'modified',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   128
                          'A': 'added',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   129
                          'R': 'removed',
5796
7705d308eb5e Fix status char in color extension for deleted (missing) files.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5792
diff changeset
   130
                          '!': 'deleted',
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   131
                          '?': 'unknown',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   132
                          'I': 'ignored',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   133
                          'C': 'clean',
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   134
                          ' ': 'copied', }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   135
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   136
_status_effects = { 'modified': ('blue', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   137
                    'added': ('green', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   138
                    'removed': ('red', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   139
                    'deleted': ('cyan', 'bold', 'underline'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   140
                    'unknown': ('magenta', 'bold', 'underline'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   141
                    'ignored': ('black', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   142
                    'clean': ('none', ),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   143
                    'copied': ('none', ), }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   144
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   145
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
   146
    '''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
   147
    ui.pushbuffer()
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   148
    retval = orig(ui, repo, **opts)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   149
    patches = ui.popbuffer().splitlines()
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   150
    for patch in patches:
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   151
        patchname = patch
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   152
        if opts['summary']:
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   153
            patchname = patchname.split(': ')[0]
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   154
        if ui.verbose:
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   155
            patchname = patchname.split(' ', 2)[-1]
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   156
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   157
        if opts['missing']:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   158
            effects = _patch_effects['missing']
6855
09db2b8236ec Apply color to output of qseries --verbose
Kevin Christen <kevin.christen@gmail.com>
parents: 6854
diff changeset
   159
        # Determine if patch is applied.
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   160
        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
   161
               if patchname == applied.name ]:
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   162
            effects = _patch_effects['applied']
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   163
        else:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   164
            effects = _patch_effects['unapplied']
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   165
        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
   166
    return retval
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   167
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   168
_patch_effects = { 'applied': ('blue', 'bold', 'underline'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   169
                   'missing': ('red', 'bold'),
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   170
                   'unapplied': ('black', 'bold'), }
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   171
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   172
def colorwrap(orig, s):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   173
    '''wrap ui.write for colored diff output'''
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   174
    lines = s.split('\n')
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   175
    for i, line in enumerate(lines):
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   176
        stripline = line
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   177
        if line and line[0] in '+-':
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   178
            # highlight trailing whitespace, but only in changed lines
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   179
            stripline = line.rstrip()
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   180
        for prefix, style in _diff_prefixes:
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   181
            if stripline.startswith(prefix):
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   182
                lines[i] = render_effects(stripline, *_diff_effects[style])
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   183
                break
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   184
        if line != stripline:
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   185
            lines[i] += render_effects(
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   186
                line[len(stripline):], *_diff_effects['trailingwhitespace'])
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   187
    orig('\n'.join(lines))
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   188
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   189
def colorshowpatch(orig, self, node):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   190
    '''wrap cmdutil.changeset_printer.showpatch with colored output'''
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   191
    oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   192
    try:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   193
        orig(self, node)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   194
    finally:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   195
        self.ui.write = oldwrite
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   196
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   197
def colordiff(orig, ui, repo, *pats, **opts):
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   198
    '''run the diff command with colored output'''
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   199
    oldwrite = extensions.wrapfunction(ui, 'write', colorwrap)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   200
    try:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   201
        orig(ui, repo, *pats, **opts)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   202
    finally:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   203
        ui.write = oldwrite
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   204
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   205
_diff_prefixes = [('diff', 'diffline'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   206
                  ('copy', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   207
                  ('rename', 'extended'),
7539
92c373f8135f color: fix colorization of the 'old mode' git diff metadata
Gilles Moris <gilles.moris@free.fr>
parents: 7459
diff changeset
   208
                  ('old', 'extended'),
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   209
                  ('new', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   210
                  ('deleted', 'extended'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   211
                  ('---', 'file_a'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   212
                  ('+++', 'file_b'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   213
                  ('@', 'hunk'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   214
                  ('-', 'deleted'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   215
                  ('+', 'inserted')]
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   216
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   217
_diff_effects = {'diffline': ('bold',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   218
                 'extended': ('cyan', 'bold'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   219
                 'file_a': ('red', 'bold'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   220
                 'file_b': ('green', 'bold'),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   221
                 'hunk': ('magenta',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   222
                 'deleted': ('red',),
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   223
                 'inserted': ('green',),
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   224
                 'changed': ('white',),
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
   225
                 'trailingwhitespace': ('bold', 'red_background'),}
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   226
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   227
def uisetup(ui):
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   228
    '''Initialize the extension.'''
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   229
    _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   230
    _setupcmd(ui, 'incoming', commands.table, None, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   231
    _setupcmd(ui, 'log', commands.table, None, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   232
    _setupcmd(ui, 'outgoing', commands.table, None, _diff_effects)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   233
    _setupcmd(ui, 'tip', commands.table, None, _diff_effects)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   234
    _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)
8278
1f9787de17d9 color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents: 7988
diff changeset
   235
    try:
1f9787de17d9 color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents: 7988
diff changeset
   236
        mq = extensions.find('mq')
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   237
        _setupcmd(ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   238
        _setupcmd(ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
8278
1f9787de17d9 color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents: 7988
diff changeset
   239
    except KeyError:
1f9787de17d9 color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents: 7988
diff changeset
   240
        # The mq extension is not enabled
1f9787de17d9 color: look for mq with extensions.find
Martin Geisler <mg@lazybytes.net>
parents: 7988
diff changeset
   241
        pass
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   242
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   243
def _setupcmd(ui, cmd, table, func, effectsmap):
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   244
    '''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
   245
    def nocolor(orig, *args, **opts):
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   246
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   247
        if (opts['no_color'] or opts['color'] == 'never' or
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   248
            (opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb'
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   249
                                          or not sys.__stdout__.isatty()))):
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   250
            return orig(*args, **opts)
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   251
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   252
        oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer,
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   253
                                               'showpatch', colorshowpatch)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   254
        try:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   255
            if func is not None:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   256
                return func(orig, *args, **opts)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   257
            return orig(*args, **opts)
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   258
        finally:
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   259
            cmdutil.changeset_printer.showpatch = oldshowpatch
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   260
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   261
    entry = extensions.wrapcommand(table, cmd, nocolor)
7455
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   262
    entry[1].extend([
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   263
        ('', 'color', 'auto', _("when to colorize (always, auto, or never)")),
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   264
        ('', 'no-color', None, _("don't colorize output")),
c9fd5474a707 color: add --color switch
Brodie Rao <me+hg@dackz.net>
parents: 7419
diff changeset
   265
    ])
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   266
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   267
    for status in effectsmap:
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7213
diff changeset
   268
        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
   269
        if effects:
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   270
            effectsmap[status] = re.split('\W+', effects)