hgext/color.py
author Steve Borho <steve@borho.org>
Thu, 03 Jun 2010 23:18:18 -0500
changeset 11302 e1dde7363601
parent 11207 1d714c1546b5
child 11310 ac873ecfc3c2
permissions -rw-r--r--
color: labeled text should be passed to ui.write() as ui.labeled Some implementations of ui.label() (HTML versions in particular) must escape the provided text and then markup the text with their tags. When this marked up text is then passed to ui.write(), we must label the text as 'ui.labeled' so the implementation knows not to escape it a second time (exposing the initial markup). This required the addition of a 'ui.plain' label for text that is purposefully not marked up. I was a little pedantic here, passing even ' ' strings to ui.label() when it would be included with other labeled text in a ui.write() call. But it seemed appropriate to lean to the side of caution.
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
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8866
diff changeset
    19
'''colorize output from some commands
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    20
10223
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    21
This extension modifies the status and resolve commands to add color to their
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    22
output to reflect file status, the qseries command to add color to reflect
7988
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
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    32
Default effects may be overridden from the .hgrc file::
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    33
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    34
  [color]
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    35
  status.modified = blue bold underline red_background
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    36
  status.added = green bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    37
  status.removed = red bold blue_background
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    38
  status.deleted = cyan bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    39
  status.unknown = magenta bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    40
  status.ignored = black bold
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    41
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    42
  # 'none' turns off all effects
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    43
  status.clean = none
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    44
  status.copied = none
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    45
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    46
  qseries.applied = blue bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    47
  qseries.unapplied = black bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    48
  qseries.missing = red bold
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    49
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    50
  diff.diffline = bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    51
  diff.extended = cyan bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    52
  diff.file_a = red bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    53
  diff.file_b = green bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    54
  diff.hunk = magenta
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    55
  diff.deleted = red
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    56
  diff.inserted = green
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    57
  diff.changed = white
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    58
  diff.trailingwhitespace = bold red_background
10046
0c23b0b3516b color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents: 10045
diff changeset
    59
10223
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    60
  resolve.unresolved = red bold
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    61
  resolve.resolved = green bold
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    62
10046
0c23b0b3516b color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents: 10045
diff changeset
    63
  bookmarks.current = green
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    64
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    65
The color extension will try to detect whether to use ANSI codes or
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    66
Win32 console APIs, unless it is made explicit::
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    67
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    68
  [color]
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    69
  mode = ansi
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    70
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    71
Any value other than 'ansi', 'win32', or 'auto' will disable color.
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    72
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    73
'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    74
8623
0c159aa7b329 color: replace re.split with ui.configlist
Martin Geisler <mg@lazybytes.net>
parents: 8622
diff changeset
    75
import os, sys
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    76
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    77
from mercurial import commands, dispatch, extensions
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    78
from mercurial.i18n import _
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    79
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    80
# start and stop parameters for effects
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    81
_effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    82
            'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    83
            'italic': 3, 'underline': 4, 'inverse': 7,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    84
            'black_background': 40, 'red_background': 41,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    85
            'green_background': 42, 'yellow_background': 43,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    86
            'blue_background': 44, 'purple_background': 45,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    87
            'cyan_background': 46, 'white_background': 47}
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    88
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    89
_styles = {'grep.match': 'red bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    90
           'diff.changed': 'white',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    91
           'diff.deleted': 'red',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    92
           'diff.diffline': 'bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    93
           'diff.extended': 'cyan bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    94
           'diff.file_a': 'red bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    95
           'diff.file_b': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    96
           'diff.hunk': 'magenta',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    97
           'diff.inserted': 'green',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    98
           'diff.trailingwhitespace': 'bold red_background',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
    99
           'diffstat.deleted': 'red',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   100
           'diffstat.inserted': 'green',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   101
           'log.changeset': 'yellow',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   102
           'resolve.resolved': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   103
           'resolve.unresolved': 'red bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   104
           'status.added': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   105
           'status.clean': 'none',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   106
           'status.copied': 'none',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   107
           'status.deleted': 'cyan bold underline',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   108
           'status.ignored': 'black bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   109
           'status.modified': 'blue bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   110
           'status.removed': 'red bold',
11302
e1dde7363601 color: labeled text should be passed to ui.write() as ui.labeled
Steve Borho <steve@borho.org>
parents: 11207
diff changeset
   111
           'status.unknown': 'magenta bold underline',
e1dde7363601 color: labeled text should be passed to ui.write() as ui.labeled
Steve Borho <steve@borho.org>
parents: 11207
diff changeset
   112
           'ui.labeled': 'none',
e1dde7363601 color: labeled text should be passed to ui.write() as ui.labeled
Steve Borho <steve@borho.org>
parents: 11207
diff changeset
   113
           'ui.plain': 'none'}
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   114
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   115
8622
0a4f6e1b78dc color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents: 8278
diff changeset
   116
def render_effects(text, effects):
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   117
    'Wrap text in commands to turn on each effect.'
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   118
    if not text:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   119
        return text
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   120
    start = [str(_effects[e]) for e in ['none'] + effects.split()]
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   121
    start = '\033[' + ';'.join(start) + 'm'
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   122
    stop = '\033[' + str(_effects['none']) + 'm'
7459
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
   123
    return ''.join([start, text, stop])
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   124
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   125
def extstyles():
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   126
    for name, ext in extensions.extensions():
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   127
        _styles.update(getattr(ext, 'colortable', {}))
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   128
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   129
def configstyles(ui):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   130
    for status, cfgeffects in ui.configitems('color'):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   131
        if '.' not in status:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   132
            continue
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   133
        cfgeffects = ui.configlist('color', status)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   134
        if cfgeffects:
8945
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   135
            good = []
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   136
            for e in cfgeffects:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   137
                if e in _effects:
8945
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   138
                    good.append(e)
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   139
                else:
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   140
                    ui.warn(_("ignoring unknown color/effect %r "
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   141
                              "(configured in color.%s)\n")
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   142
                            % (e, status))
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   143
            _styles[status] = ' '.join(good)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   144
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   145
_buffers = None
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   146
def style(msg, label):
11302
e1dde7363601 color: labeled text should be passed to ui.write() as ui.labeled
Steve Borho <steve@borho.org>
parents: 11207
diff changeset
   147
    if label in ('ui.plain', 'ui.labeled'):
e1dde7363601 color: labeled text should be passed to ui.write() as ui.labeled
Steve Borho <steve@borho.org>
parents: 11207
diff changeset
   148
        return msg
10831
441f5d66da03 color: concatenate effects correctly and avoid printing empty effects
Brodie Rao <brodie@bitheap.org>
parents: 10827
diff changeset
   149
    effects = []
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   150
    for l in label.split():
10831
441f5d66da03 color: concatenate effects correctly and avoid printing empty effects
Brodie Rao <brodie@bitheap.org>
parents: 10827
diff changeset
   151
        s = _styles.get(l, '')
441f5d66da03 color: concatenate effects correctly and avoid printing empty effects
Brodie Rao <brodie@bitheap.org>
parents: 10827
diff changeset
   152
        if s:
441f5d66da03 color: concatenate effects correctly and avoid printing empty effects
Brodie Rao <brodie@bitheap.org>
parents: 10827
diff changeset
   153
            effects.append(s)
441f5d66da03 color: concatenate effects correctly and avoid printing empty effects
Brodie Rao <brodie@bitheap.org>
parents: 10827
diff changeset
   154
    effects = ''.join(effects)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   155
    if effects:
10827
b66388f6adfa color: don't split colors across lines (which confuses less -R)
Brodie Rao <brodie@bitheap.org>
parents: 10826
diff changeset
   156
        return '\n'.join([render_effects(s, effects)
b66388f6adfa color: don't split colors across lines (which confuses less -R)
Brodie Rao <brodie@bitheap.org>
parents: 10826
diff changeset
   157
                          for s in msg.split('\n')])
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   158
    return msg
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   159
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   160
def popbuffer(orig, labeled=False):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   161
    global _buffers
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   162
    if labeled:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   163
        return ''.join(style(a, label) for a, label in _buffers.pop())
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   164
    return ''.join(a for a, label in _buffers.pop())
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   165
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   166
mode = 'ansi'
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   167
def write(orig, *args, **opts):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   168
    label = opts.get('label', '')
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   169
    global _buffers
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   170
    if _buffers:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   171
        _buffers[-1].extend([(str(a), label) for a in args])
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   172
    elif mode == 'win32':
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   173
        for a in args:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   174
            win32print(a, orig, **opts)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   175
    else:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   176
        return orig(*[style(str(a), label) for a in args], **opts)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   177
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   178
def write_err(orig, *args, **opts):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   179
    label = opts.get('label', '')
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   180
    if mode == 'win32':
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   181
        for a in args:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   182
            win32print(a, orig, **opts)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   183
    else:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   184
        return orig(*[style(str(a), label) for a in args], **opts)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   185
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   186
def uisetup(ui):
10871
3f30190781a3 color: respect HGPLAIN
Steve Borho <steve@borho.org>
parents: 10870
diff changeset
   187
    if ui.plain():
3f30190781a3 color: respect HGPLAIN
Steve Borho <steve@borho.org>
parents: 10870
diff changeset
   188
        return
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   189
    global mode
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   190
    mode = ui.config('color', 'mode', 'auto')
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   191
    if mode == 'auto':
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   192
        if os.name == 'nt' and 'TERM' not in os.environ:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   193
            # looks line a cmd.exe console, use win32 API or nothing
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   194
            mode = w32effects and 'win32' or 'none'
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   195
        else:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   196
            mode = 'ansi'
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   197
    if mode == 'win32':
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   198
        if w32effects is None:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   199
            # only warn if color.mode is explicitly set to win32
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   200
            ui.warn(_('win32console not found, please install pywin32\n'))
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   201
            return
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   202
        _effects.update(w32effects)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   203
    elif mode != 'ansi':
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   204
        return
11207
1d714c1546b5 color: bring back colors with pager
Yuya Nishihara <yuya@tcha.org>
parents: 11186
diff changeset
   205
1d714c1546b5 color: bring back colors with pager
Yuya Nishihara <yuya@tcha.org>
parents: 11186
diff changeset
   206
    # check isatty() before anything else changes it (like pager)
1d714c1546b5 color: bring back colors with pager
Yuya Nishihara <yuya@tcha.org>
parents: 11186
diff changeset
   207
    isatty = sys.__stdout__.isatty()
1d714c1546b5 color: bring back colors with pager
Yuya Nishihara <yuya@tcha.org>
parents: 11186
diff changeset
   208
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   209
    def colorcmd(orig, ui_, opts, cmd, cmdfunc):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   210
        if (opts['color'] == 'always' or
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   211
            (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb'
11207
1d714c1546b5 color: bring back colors with pager
Yuya Nishihara <yuya@tcha.org>
parents: 11186
diff changeset
   212
                                          and isatty))):
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   213
            global _buffers
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   214
            _buffers = ui_._buffers
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   215
            extensions.wrapfunction(ui_, 'popbuffer', popbuffer)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   216
            extensions.wrapfunction(ui_, 'write', write)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   217
            extensions.wrapfunction(ui_, 'write_err', write_err)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   218
            ui_.label = style
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   219
            extstyles()
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   220
            configstyles(ui)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   221
        return orig(ui_, opts, cmd, cmdfunc)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   222
    extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   223
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   224
commands.globalopts.append(('', 'color', 'auto',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   225
                            _("when to colorize (always, auto, or never)")))
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   226
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   227
try:
10989
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   228
    import re, pywintypes
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   229
    from win32console import *
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   230
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   231
    # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   232
    w32effects = {
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   233
        'none': 0,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   234
        'black': 0,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   235
        'red': FOREGROUND_RED,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   236
        'green': FOREGROUND_GREEN,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   237
        'yellow': FOREGROUND_RED | FOREGROUND_GREEN,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   238
        'blue': FOREGROUND_BLUE,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   239
        'magenta': FOREGROUND_BLUE | FOREGROUND_RED,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   240
        'cyan': FOREGROUND_BLUE | FOREGROUND_GREEN,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   241
        'white': FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   242
        'bold': FOREGROUND_INTENSITY,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   243
        'black_background': 0,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   244
        'red_background': BACKGROUND_RED,
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   245
        'green_background': BACKGROUND_GREEN,
11118
24f904ae5a26 color: add some missing background colors for win32 console
Stanimir Stamenkov <s7an10@netscape.net>
parents: 11084
diff changeset
   246
        'yellow_background': BACKGROUND_RED | BACKGROUND_GREEN,
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   247
        'blue_background': BACKGROUND_BLUE,
11118
24f904ae5a26 color: add some missing background colors for win32 console
Stanimir Stamenkov <s7an10@netscape.net>
parents: 11084
diff changeset
   248
        'purple_background': BACKGROUND_BLUE | BACKGROUND_RED,
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   249
        'cyan_background': BACKGROUND_BLUE | BACKGROUND_GREEN,
11118
24f904ae5a26 color: add some missing background colors for win32 console
Stanimir Stamenkov <s7an10@netscape.net>
parents: 11084
diff changeset
   250
        'white_background': BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
11084
4a8d4179362b color: fix typo in w32effect for bold_background
Steve Borho <steve@borho.org>
parents: 10989
diff changeset
   251
        'bold_background': BACKGROUND_INTENSITY,
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   252
        'underline': COMMON_LVB_UNDERSCORE,     # double-byte charsets only
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   253
        'inverse': COMMON_LVB_REVERSE_VIDEO,    # double-byte charsets only
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   254
    }
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   255
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   256
    stdout = GetStdHandle(STD_OUTPUT_HANDLE)
10989
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   257
    try:
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   258
        origattr = stdout.GetConsoleScreenBufferInfo()['Attributes']
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   259
    except pywintypes.error:
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   260
        # stdout may be defined but not support
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   261
        # GetConsoleScreenBufferInfo(), when called from subprocess or
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   262
        # redirected.
9fb09fc3ea3c color: handle non-standard stdout on win32
Patrick Mezard <pmezard@gmail.com>
parents: 10871
diff changeset
   263
        raise ImportError()
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   264
    ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   265
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   266
    def win32print(text, orig, **opts):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   267
        label = opts.get('label', '')
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   268
        attr = 0
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   269
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   270
        # determine console attributes based on labels
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   271
        for l in label.split():
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   272
            style = _styles.get(l, '')
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   273
            for effect in style.split():
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   274
                attr |= w32effects[effect]
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   275
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   276
        # hack to ensure regexp finds data
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   277
        if not text.startswith('\033['):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   278
            text = '\033[m' + text
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   279
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   280
        # Look for ANSI-like codes embedded in text
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   281
        m = re.match(ansire, text)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   282
        while m:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   283
            for sattr in m.group(1).split(';'):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   284
                if sattr:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   285
                    val = int(sattr)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   286
                    attr = val and attr|val or 0
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   287
            stdout.SetConsoleTextAttribute(attr or origattr)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   288
            orig(m.group(2), **opts)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   289
            m = re.match(ansire, m.group(3))
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   290
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   291
        # Explicity reset original attributes
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   292
        stdout.SetConsoleTextAttribute(origattr)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   293
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   294
except ImportError:
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   295
    w32effects = None