summaryrefslogtreecommitdiff
path: root/sys/lib/python/hgext/color.py
blob: 4a736db430a7ad89dbd3687c918276d840bc99e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# color.py color output for the status and qseries commands
#
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# 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
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

'''colorize output from some commands

This extension modifies the status command to add color to its output
to reflect file status, the qseries command to add color to reflect
patch status (applied, unapplied, missing), and to diff-related
commands to highlight additions, removals, diff headers, and trailing
whitespace.

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.

Default effects may be overridden 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

  # 'none' turns off all effects
  status.clean = none
  status.copied = none

  qseries.applied = blue bold underline
  qseries.unapplied = black bold
  qseries.missing = red bold

  diff.diffline = bold
  diff.extended = cyan bold
  diff.file_a = red bold
  diff.file_b = green bold
  diff.hunk = magenta
  diff.deleted = red
  diff.inserted = green
  diff.changed = white
  diff.trailingwhitespace = bold red_background
'''

import os, sys
import itertools

from mercurial import cmdutil, commands, extensions, error
from mercurial.i18n import _

# start and stop parameters for effects
_effect_params = {'none': 0,
                  'black': 30,
                  'red': 31,
                  'green': 32,
                  'yellow': 33,
                  'blue': 34,
                  'magenta': 35,
                  'cyan': 36,
                  'white': 37,
                  'bold': 1,
                  'italic': 3,
                  'underline': 4,
                  'inverse': 7,
                  'black_background': 40,
                  'red_background': 41,
                  'green_background': 42,
                  'yellow_background': 43,
                  'blue_background': 44,
                  'purple_background': 45,
                  'cyan_background': 46,
                  'white_background': 47}

def render_effects(text, effects):
    'Wrap text in commands to turn on each effect.'
    start = [str(_effect_params[e]) for e in ['none'] + effects]
    start = '\033[' + ';'.join(start) + 'm'
    stop = '\033[' + str(_effect_params['none']) + 'm'
    return ''.join([start, text, stop])

def colorstatus(orig, ui, repo, *pats, **opts):
    '''run the status command with colored output'''

    delimiter = opts['print0'] and '\0' or '\n'

    nostatus = opts.get('no_status')
    opts['no_status'] = False
    # run status and capture its output
    ui.pushbuffer()
    retval = orig(ui, repo, *pats, **opts)
    # filter out empty strings
    lines_with_status = [ line for line in ui.popbuffer().split(delimiter) if line ]

    if nostatus:
        lines = [l[2:] for l in lines_with_status]
    else:
        lines = lines_with_status

    # apply color to output and display it
    for i in xrange(len(lines)):
        status = _status_abbreviations[lines_with_status[i][0]]
        effects = _status_effects[status]
        if effects:
            lines[i] = render_effects(lines[i], effects)
        ui.write(lines[i] + delimiter)
    return retval

_status_abbreviations = { 'M': 'modified',
                          'A': 'added',
                          'R': 'removed',
                          '!': 'deleted',
                          '?': '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'], }

def colorqseries(orig, ui, repo, *dummy, **opts):
    '''run the qseries command with colored output'''
    ui.pushbuffer()
    retval = orig(ui, repo, **opts)
    patchlines = ui.popbuffer().splitlines()
    patchnames = repo.mq.series

    for patch, patchname in itertools.izip(patchlines, patchnames):
        if opts['missing']:
            effects = _patch_effects['missing']
        # Determine if patch is applied.
        elif [ applied for applied in repo.mq.applied
               if patchname == applied.name ]:
            effects = _patch_effects['applied']
        else:
            effects = _patch_effects['unapplied']

        patch = patch.replace(patchname, render_effects(patchname, effects), 1)
        ui.write(patch + '\n')
    return retval

_patch_effects = { 'applied': ['blue', 'bold', 'underline'],
                   'missing': ['red', 'bold'],
                   'unapplied': ['black', 'bold'], }

def colorwrap(orig, s):
    '''wrap ui.write for colored diff output'''
    lines = s.split('\n')
    for i, line in enumerate(lines):
        stripline = line
        if line and line[0] in '+-':
            # highlight trailing whitespace, but only in changed lines
            stripline = line.rstrip()
        for prefix, style in _diff_prefixes:
            if stripline.startswith(prefix):
                lines[i] = render_effects(stripline, _diff_effects[style])
                break
        if line != stripline:
            lines[i] += render_effects(
                line[len(stripline):], _diff_effects['trailingwhitespace'])
    orig('\n'.join(lines))

def colorshowpatch(orig, self, node):
    '''wrap cmdutil.changeset_printer.showpatch with colored output'''
    oldwrite = extensions.wrapfunction(self.ui, 'write', colorwrap)
    try:
        orig(self, node)
    finally:
        self.ui.write = oldwrite

def colordiff(orig, ui, repo, *pats, **opts):
    '''run the diff command with colored output'''
    oldwrite = extensions.wrapfunction(ui, 'write', colorwrap)
    try:
        orig(ui, repo, *pats, **opts)
    finally:
        ui.write = oldwrite

_diff_prefixes = [('diff', 'diffline'),
                  ('copy', 'extended'),
                  ('rename', 'extended'),
                  ('old', 'extended'),
                  ('new', 'extended'),
                  ('deleted', 'extended'),
                  ('---', 'file_a'),
                  ('+++', 'file_b'),
                  ('@', 'hunk'),
                  ('-', 'deleted'),
                  ('+', 'inserted')]

_diff_effects = {'diffline': ['bold'],
                 'extended': ['cyan', 'bold'],
                 'file_a': ['red', 'bold'],
                 'file_b': ['green', 'bold'],
                 'hunk': ['magenta'],
                 'deleted': ['red'],
                 'inserted': ['green'],
                 'changed': ['white'],
                 'trailingwhitespace': ['bold', 'red_background']}

_ui = None

def uisetup(ui):
    '''Initialize the extension.'''
    global _ui
    _ui = ui
    _setupcmd(ui, 'diff', commands.table, colordiff, _diff_effects)
    _setupcmd(ui, 'incoming', commands.table, None, _diff_effects)
    _setupcmd(ui, 'log', commands.table, None, _diff_effects)
    _setupcmd(ui, 'outgoing', commands.table, None, _diff_effects)
    _setupcmd(ui, 'tip', commands.table, None, _diff_effects)
    _setupcmd(ui, 'status', commands.table, colorstatus, _status_effects)

def extsetup():
    try:
        mq = extensions.find('mq')
        try:
            # If we are loaded after mq, we must wrap commands.table
            _setupcmd(_ui, 'qdiff', commands.table, colordiff, _diff_effects)
            _setupcmd(_ui, 'qseries', commands.table, colorqseries, _patch_effects)
        except error.UnknownCommand:
            # Otherwise we wrap mq.cmdtable
            _setupcmd(_ui, 'qdiff', mq.cmdtable, colordiff, _diff_effects)
            _setupcmd(_ui, 'qseries', mq.cmdtable, colorqseries, _patch_effects)
    except KeyError:
        # The mq extension is not enabled
        pass

def _setupcmd(ui, cmd, table, func, effectsmap):
    '''patch in command to command table and load effect map'''
    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)

        oldshowpatch = extensions.wrapfunction(cmdutil.changeset_printer,
                                               'showpatch', colorshowpatch)
        try:
            if func is not None:
                return func(orig, *args, **opts)
            return orig(*args, **opts)
        finally:
            cmdutil.changeset_printer.showpatch = oldshowpatch

    entry = extensions.wrapcommand(table, cmd, nocolor)
    entry[1].extend([
        ('', 'color', 'auto', _("when to colorize (always, auto, or never)")),
        ('', 'no-color', None, _("don't colorize output")),
    ])

    for status in effectsmap:
        configkey = cmd + '.' + status
        effects = ui.configlist('color', configkey)
        if effects:
            good = []
            for e in effects:
                if e in _effect_params:
                    good.append(e)
                else:
                    ui.warn(_("ignoring unknown color/effect %r "
                              "(configured in color.%s)\n")
                            % (e, configkey))
            effectsmap[status] = good