hgext/color.py
author Brodie Rao <brodie@bitheap.org>
Wed, 29 Jun 2011 13:20:39 -0400
branchstable
changeset 14768 55db12e54450
parent 14758 1e6661e09818
child 14769 9adce4b38ed1
permissions -rw-r--r--
color: fix TypeError with auto mode on win32 when colors aren't available (issue2871) This can happen when stdout isn't a terminal (e.g., it's being piped).
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
13638
72040d98ff0a color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents: 13635
diff changeset
    21
This extension modifies the status and resolve commands to add color
72040d98ff0a color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents: 13635
diff changeset
    22
to their output to reflect file status, the qseries command to add
72040d98ff0a color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents: 13635
diff changeset
    23
color to reflect patch status (applied, unapplied, missing), and to
72040d98ff0a color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents: 13635
diff changeset
    24
diff-related commands to highlight additions, removals, diff headers,
72040d98ff0a color: wrap lines in docstring
Martin Geisler <mg@aragost.com>
parents: 13635
diff changeset
    25
and trailing 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
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    28
also available. By default, the terminfo database is used to find the
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    29
terminal codes used to change color and effect.  If terminfo is not
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    30
available, then effects are rendered with the ECMA-48 SGR control
13635
c9ddc39c21b6 color: don't mention internal function in docstring
Martin Geisler <mg@aragost.com>
parents: 13361
diff changeset
    31
function (aka ANSI escape codes).
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    32
12083
ebfc46929f3e help: refer to user configuration file more consistently
Brodie Rao <brodie@bitheap.org>
parents: 11732
diff changeset
    33
Default effects may be overridden from your configuration file::
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    34
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    35
  [color]
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    36
  status.modified = blue bold underline red_background
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    37
  status.added = green bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    38
  status.removed = red bold blue_background
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    39
  status.deleted = cyan bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    40
  status.unknown = magenta bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    41
  status.ignored = black bold
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    42
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    43
  # 'none' turns off all effects
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    44
  status.clean = none
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    45
  status.copied = none
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    46
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    47
  qseries.applied = blue bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    48
  qseries.unapplied = black bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    49
  qseries.missing = red bold
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    50
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    51
  diff.diffline = bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    52
  diff.extended = cyan bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    53
  diff.file_a = red bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    54
  diff.file_b = green bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    55
  diff.hunk = magenta
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    56
  diff.deleted = red
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    57
  diff.inserted = green
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    58
  diff.changed = white
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    59
  diff.trailingwhitespace = bold red_background
10046
0c23b0b3516b color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents: 10045
diff changeset
    60
10223
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    61
  resolve.unresolved = red bold
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    62
  resolve.resolved = green bold
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    63
10046
0c23b0b3516b color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents: 10045
diff changeset
    64
  bookmarks.current = green
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    65
11969
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    66
  branches.active = none
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    67
  branches.closed = black bold
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    68
  branches.current = green
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    69
  branches.inactive = none
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    70
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    71
The available effects in terminfo mode are 'blink', 'bold', 'dim',
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    72
'inverse', 'invisible', 'italic', 'standout', and 'underline'; in
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    73
ECMA-48 mode, the options are 'bold', 'inverse', 'italic', and
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    74
'underline'.  How each is rendered depends on the terminal emulator.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    75
Some may not be available for a given terminal type, and will be
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    76
silently ignored.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    77
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    78
Because there are only eight standard colors, this module allows you
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    79
to define color names for other color slots which might be available
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    80
for your terminal type, assuming terminfo mode.  For instance::
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    81
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    82
  color.brightblue = 12
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    83
  color.pink = 207
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    84
  color.orange = 202
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    85
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    86
to set 'brightblue' to color slot 12 (useful for 16 color terminals
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    87
that have brighter colors defined in the upper eight) and, 'pink' and
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    88
'orange' to colors in 256-color xterm's default color cube.  These
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    89
defined colors may then be used as any of the pre-defined eight,
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    90
including appending '_background' to set the background to that color.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    91
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    92
The color extension will try to detect whether to use terminfo, ANSI
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    93
codes or Win32 console APIs, unless it is made explicit; e.g.::
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    94
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    95
  [color]
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    96
  mode = ansi
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    97
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    98
Any value other than 'ansi', 'win32', 'terminfo', or 'auto' will
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    99
disable color.
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   100
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   101
'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   102
14139
4e5a36eeefd1 remove unused imports
timeless <timeless@mozdev.org>
parents: 14103
diff changeset
   103
import os
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   104
12089
70f4a0f4e9a3 color: accept usual boolean values as synonyms for always and never
Augie Fackler <durin42@gmail.com>
parents: 12084
diff changeset
   105
from mercurial import commands, dispatch, extensions, ui as uimod, util
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   106
from mercurial.i18n import _
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   107
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   108
# 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
   109
_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
   110
            '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
   111
            '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
   112
            '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
   113
            '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
   114
            '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
   115
            '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
   116
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   117
def _terminfosetup(ui, mode):
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   118
    '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   119
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   120
    global _terminfo_params
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   121
    # If we failed to load curses, we go ahead and return.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   122
    if not _terminfo_params:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   123
        return
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   124
    # Otherwise, see what the config file says.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   125
    if mode not in ('auto', 'terminfo'):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   126
        return
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   127
13998
14c7526fed89 color: code simplification
Patrick Mezard <pmezard@gmail.com>
parents: 13987
diff changeset
   128
    _terminfo_params.update((key[6:], (False, int(val)))
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   129
        for key, val in ui.configitems('color')
13998
14c7526fed89 color: code simplification
Patrick Mezard <pmezard@gmail.com>
parents: 13987
diff changeset
   130
        if key.startswith('color.'))
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   131
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   132
    try:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   133
        curses.setupterm()
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   134
    except curses.error, e:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   135
        _terminfo_params = {}
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   136
        return
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   137
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   138
    for key, (b, e) in _terminfo_params.items():
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   139
        if not b:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   140
            continue
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   141
        if not curses.tigetstr(e):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   142
            # Most terminals don't support dim, invis, etc, so don't be
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   143
            # noisy and use ui.debug().
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   144
            ui.debug("no terminfo entry for %s\n" % e)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   145
            del _terminfo_params[key]
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   146
    if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
14758
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   147
        # Only warn about missing terminfo entries if we explicitly asked for
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   148
        # terminfo mode.
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   149
        if mode == "terminfo":
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   150
            ui.warn(_("no terminfo entry for setab/setaf: reverting to "
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   151
              "ECMA-48 color\n"))
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   152
        _terminfo_params = {}
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   153
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   154
def _modesetup(ui, opts):
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   155
    global _terminfo_params
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   156
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   157
    coloropt = opts['color']
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   158
    auto = coloropt == 'auto'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   159
    always = not auto and util.parsebool(coloropt)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   160
    if not always and not auto:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   161
        return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   162
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   163
    formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted())
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   164
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   165
    mode = ui.config('color', 'mode', 'auto')
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   166
    realmode = mode
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   167
    if mode == 'auto':
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   168
        if os.name == 'nt' and 'TERM' not in os.environ:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   169
            # looks line a cmd.exe console, use win32 API or nothing
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   170
            realmode = 'win32'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   171
        elif not formatted:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   172
            realmode = 'ansi'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   173
        else:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   174
            realmode = 'terminfo'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   175
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   176
    if realmode == 'win32':
14768
55db12e54450 color: fix TypeError with auto mode on win32 when colors aren't available (issue2871)
Brodie Rao <brodie@bitheap.org>
parents: 14758
diff changeset
   177
        if not w32effects:
55db12e54450 color: fix TypeError with auto mode on win32 when colors aren't available (issue2871)
Brodie Rao <brodie@bitheap.org>
parents: 14758
diff changeset
   178
            if mode == 'win32':
55db12e54450 color: fix TypeError with auto mode on win32 when colors aren't available (issue2871)
Brodie Rao <brodie@bitheap.org>
parents: 14758
diff changeset
   179
                # only warn if color.mode is explicitly set to win32
55db12e54450 color: fix TypeError with auto mode on win32 when colors aren't available (issue2871)
Brodie Rao <brodie@bitheap.org>
parents: 14758
diff changeset
   180
                ui.warn(_('warning: failed to set color mode to %s\n') % mode)
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   181
            return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   182
        _effects.update(w32effects)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   183
    elif realmode == 'ansi':
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   184
        _terminfo_params = {}
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   185
    elif realmode == 'terminfo':
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   186
        _terminfosetup(ui, mode)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   187
        if not _terminfo_params:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   188
            if mode == 'terminfo':
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   189
                ## FIXME Shouldn't we return None in this case too?
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   190
                # only warn if color.mode is explicitly set to win32
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   191
                ui.warn(_('warning: failed to set color mode to %s\n') % mode)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   192
            realmode = 'ansi'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   193
    else:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   194
        return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   195
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   196
    if always or (auto and formatted):
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   197
        return realmode
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   198
    return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   199
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   200
try:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   201
    import curses
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   202
    # Mapping from effect name to terminfo attribute name or color number.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   203
    # This will also force-load the curses module.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   204
    _terminfo_params = {'none': (True, 'sgr0'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   205
                        'standout': (True, 'smso'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   206
                        'underline': (True, 'smul'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   207
                        'reverse': (True, 'rev'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   208
                        'inverse': (True, 'rev'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   209
                        'blink': (True, 'blink'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   210
                        'dim': (True, 'dim'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   211
                        'bold': (True, 'bold'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   212
                        'invisible': (True, 'invis'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   213
                        'italic': (True, 'sitm'),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   214
                        'black': (False, curses.COLOR_BLACK),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   215
                        'red': (False, curses.COLOR_RED),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   216
                        'green': (False, curses.COLOR_GREEN),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   217
                        'yellow': (False, curses.COLOR_YELLOW),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   218
                        'blue': (False, curses.COLOR_BLUE),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   219
                        'magenta': (False, curses.COLOR_MAGENTA),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   220
                        'cyan': (False, curses.COLOR_CYAN),
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   221
                        'white': (False, curses.COLOR_WHITE)}
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   222
except ImportError:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   223
    _terminfo_params = False
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   224
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   225
_styles = {'grep.match': 'red bold',
13361
5b4252364ff9 bookmarks: move color style to color
Matt Mackall <mpm@selenic.com>
parents: 12807
diff changeset
   226
           'bookmarks.current': 'green',
11969
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   227
           'branches.active': 'none',
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   228
           'branches.closed': 'black bold',
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   229
           'branches.current': 'green',
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   230
           'branches.inactive': 'none',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   231
           'diff.changed': 'white',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   232
           'diff.deleted': 'red',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   233
           'diff.diffline': 'bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   234
           'diff.extended': 'cyan bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   235
           '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
   236
           '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
   237
           'diff.hunk': 'magenta',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   238
           'diff.inserted': 'green',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   239
           '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
   240
           'diffstat.deleted': 'red',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   241
           'diffstat.inserted': 'green',
13774
1ce0e80799c0 ui: label prompts, default to yellow prompts
Martin Geisler <mg@lazybytes.net>
parents: 13648
diff changeset
   242
           'ui.prompt': 'yellow',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   243
           'log.changeset': 'yellow',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   244
           'resolve.resolved': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   245
           'resolve.unresolved': 'red bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   246
           'status.added': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   247
           'status.clean': 'none',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   248
           'status.copied': 'none',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   249
           '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
   250
           'status.ignored': 'black bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   251
           'status.modified': 'blue bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   252
           'status.removed': 'red bold',
11310
ac873ecfc3c2 Backed out changeset: e1dde7363601
Steve Borho <steve@borho.org>
parents: 11302
diff changeset
   253
           'status.unknown': 'magenta bold underline'}
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   254
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   255
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   256
def _effect_str(effect):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   257
    '''Helper function for render_effects().'''
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   258
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   259
    bg = False
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   260
    if effect.endswith('_background'):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   261
        bg = True
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   262
        effect = effect[:-11]
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   263
    attr, val = _terminfo_params[effect]
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   264
    if attr:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   265
        return curses.tigetstr(val)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   266
    elif bg:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   267
        return curses.tparm(curses.tigetstr('setab'), val)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   268
    else:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   269
        return curses.tparm(curses.tigetstr('setaf'), val)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   270
8622
0a4f6e1b78dc color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents: 8278
diff changeset
   271
def render_effects(text, effects):
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   272
    '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
   273
    if not text:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   274
        return text
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   275
    if not _terminfo_params:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   276
        start = [str(_effects[e]) for e in ['none'] + effects.split()]
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   277
        start = '\033[' + ';'.join(start) + 'm'
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   278
        stop = '\033[' + str(_effects['none']) + 'm'
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   279
    else:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   280
        start = ''.join(_effect_str(effect)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   281
                        for effect in ['none'] + effects.split())
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   282
        stop = _effect_str('none')
7459
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
   283
    return ''.join([start, text, stop])
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   284
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   285
def extstyles():
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   286
    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
   287
        _styles.update(getattr(ext, 'colortable', {}))
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   288
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   289
def configstyles(ui):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   290
    for status, cfgeffects in ui.configitems('color'):
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   291
        if '.' not in status or status.startswith('color.'):
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   292
            continue
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   293
        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
   294
        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
   295
            good = []
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   296
            for e in cfgeffects:
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   297
                if not _terminfo_params and e in _effects:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   298
                    good.append(e)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   299
                elif e in _terminfo_params or e[:-11] in _terminfo_params:
8945
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   300
                    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
   301
                else:
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   302
                    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
   303
                              "(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
   304
                            % (e, status))
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   305
            _styles[status] = ' '.join(good)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   306
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   307
class colorui(uimod.ui):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   308
    def popbuffer(self, labeled=False):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   309
        if labeled:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   310
            return ''.join(self.label(a, label) for a, label
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   311
                           in self._buffers.pop())
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   312
        return ''.join(a for a, label in self._buffers.pop())
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   313
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   314
    _colormode = 'ansi'
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   315
    def write(self, *args, **opts):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   316
        label = opts.get('label', '')
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   317
        if self._buffers:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   318
            self._buffers[-1].extend([(str(a), label) for a in args])
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   319
        elif self._colormode == 'win32':
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   320
            for a in args:
11727
c34a1ab80550 color: pass write/write_err to win32print correctly (issue2312)
Brodie Rao <brodie@bitheap.org>
parents: 11555
diff changeset
   321
                win32print(a, super(colorui, self).write, **opts)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   322
        else:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   323
            return super(colorui, self).write(
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   324
                *[self.label(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
   325
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   326
    def write_err(self, *args, **opts):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   327
        label = opts.get('label', '')
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   328
        if self._colormode == 'win32':
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   329
            for a in args:
11727
c34a1ab80550 color: pass write/write_err to win32print correctly (issue2312)
Brodie Rao <brodie@bitheap.org>
parents: 11555
diff changeset
   330
                win32print(a, super(colorui, self).write_err, **opts)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   331
        else:
11732
386e56ecfb78 color: call correct superclass method in write_err
Brodie Rao <brodie@bitheap.org>
parents: 11727
diff changeset
   332
            return super(colorui, self).write_err(
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   333
                *[self.label(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
   334
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   335
    def label(self, msg, label):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   336
        effects = []
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   337
        for l in label.split():
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   338
            s = _styles.get(l, '')
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   339
            if s:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   340
                effects.append(s)
14145
4b7e4b9db8fb color: fix using multiple effects
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14139
diff changeset
   341
        effects = ' '.join(effects)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   342
        if effects:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   343
            return '\n'.join([render_effects(s, effects)
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   344
                              for s in msg.split('\n')])
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   345
        return msg
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   346
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   347
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   348
def uisetup(ui):
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   349
    global _terminfo_params
10871
3f30190781a3 color: respect HGPLAIN
Steve Borho <steve@borho.org>
parents: 10870
diff changeset
   350
    if ui.plain():
3f30190781a3 color: respect HGPLAIN
Steve Borho <steve@borho.org>
parents: 10870
diff changeset
   351
        return
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   352
    def colorcmd(orig, ui_, opts, cmd, cmdfunc):
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   353
        mode = _modesetup(ui_, opts)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   354
        if mode:
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   355
            colorui._colormode = mode
14516
842a9179132c color: check if ui is already a subclass of colorui before wrapping it
Idan Kamara <idankk86@gmail.com>
parents: 14495
diff changeset
   356
            if not issubclass(ui_.__class__, colorui):
842a9179132c color: check if ui is already a subclass of colorui before wrapping it
Idan Kamara <idankk86@gmail.com>
parents: 14495
diff changeset
   357
                colorui.__bases__ = (ui_.__class__,)
842a9179132c color: check if ui is already a subclass of colorui before wrapping it
Idan Kamara <idankk86@gmail.com>
parents: 14495
diff changeset
   358
                ui_.__class__ = colorui
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   359
            extstyles()
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   360
            configstyles(ui_)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   361
        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
   362
    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
   363
12693
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   364
def extsetup(ui):
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   365
    commands.globalopts.append(
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   366
        ('', 'color', 'auto',
12807
6bbf0139a36d color: give hint to translators about untranslated keywords
Martin Geisler <mg@lazybytes.net>
parents: 12693
diff changeset
   367
         # i18n: 'always', 'auto', and 'never' are keywords and should
6bbf0139a36d color: give hint to translators about untranslated keywords
Martin Geisler <mg@lazybytes.net>
parents: 12693
diff changeset
   368
         # not be translated
12693
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   369
         _("when to colorize (boolean, always, auto, or never)"),
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   370
         _('TYPE')))
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   371
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   372
if os.name != 'nt':
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   373
    w32effects = None
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   374
else:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   375
    import re, ctypes
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   376
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   377
    _kernel32 = ctypes.windll.kernel32
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   378
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   379
    _WORD = ctypes.c_ushort
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   380
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   381
    _INVALID_HANDLE_VALUE = -1
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   382
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   383
    class _COORD(ctypes.Structure):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   384
        _fields_ = [('X', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   385
                    ('Y', ctypes.c_short)]
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   386
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   387
    class _SMALL_RECT(ctypes.Structure):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   388
        _fields_ = [('Left', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   389
                    ('Top', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   390
                    ('Right', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   391
                    ('Bottom', ctypes.c_short)]
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   392
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   393
    class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   394
        _fields_ = [('dwSize', _COORD),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   395
                    ('dwCursorPosition', _COORD),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   396
                    ('wAttributes', _WORD),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   397
                    ('srWindow', _SMALL_RECT),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   398
                    ('dwMaximumWindowSize', _COORD)]
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   399
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   400
    _STD_OUTPUT_HANDLE = 0xfffffff5L # (DWORD)-11
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   401
    _STD_ERROR_HANDLE = 0xfffffff4L  # (DWORD)-12
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   402
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   403
    _FOREGROUND_BLUE = 0x0001
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   404
    _FOREGROUND_GREEN = 0x0002
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   405
    _FOREGROUND_RED = 0x0004
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   406
    _FOREGROUND_INTENSITY = 0x0008
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   407
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   408
    _BACKGROUND_BLUE = 0x0010
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   409
    _BACKGROUND_GREEN = 0x0020
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   410
    _BACKGROUND_RED = 0x0040
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   411
    _BACKGROUND_INTENSITY = 0x0080
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   412
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   413
    _COMMON_LVB_REVERSE_VIDEO = 0x4000
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   414
    _COMMON_LVB_UNDERSCORE = 0x8000
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   415
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   416
    # 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
   417
    w32effects = {
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   418
        'none': -1,
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   419
        'black': 0,
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   420
        'red': _FOREGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   421
        'green': _FOREGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   422
        'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   423
        'blue': _FOREGROUND_BLUE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   424
        'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   425
        'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   426
        'white': _FOREGROUND_RED | _FOREGROUND_GREEN | _FOREGROUND_BLUE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   427
        'bold': _FOREGROUND_INTENSITY,
12278
c4c2ba553401 merge with stable
Sune Foldager <cryo@cyanite.org>
parents: 12089 12277
diff changeset
   428
        'black_background': 0x100,                  # unused value > 0x0f
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   429
        'red_background': _BACKGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   430
        'green_background': _BACKGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   431
        'yellow_background': _BACKGROUND_RED | _BACKGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   432
        'blue_background': _BACKGROUND_BLUE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   433
        'purple_background': _BACKGROUND_BLUE | _BACKGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   434
        'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   435
        'white_background': (_BACKGROUND_RED | _BACKGROUND_GREEN |
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   436
                             _BACKGROUND_BLUE),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   437
        'bold_background': _BACKGROUND_INTENSITY,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   438
        'underline': _COMMON_LVB_UNDERSCORE,  # double-byte charsets only
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   439
        'inverse': _COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   440
    }
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   441
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   442
    passthrough = set([_FOREGROUND_INTENSITY,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   443
                       _BACKGROUND_INTENSITY,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   444
                       _COMMON_LVB_UNDERSCORE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   445
                       _COMMON_LVB_REVERSE_VIDEO])
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   446
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   447
    stdout = _kernel32.GetStdHandle(
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   448
                  _STD_OUTPUT_HANDLE)  # don't close the handle returned
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   449
    if stdout is None or stdout == _INVALID_HANDLE_VALUE:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   450
        w32effects = None
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   451
    else:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   452
        csbi = _CONSOLE_SCREEN_BUFFER_INFO()
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   453
        if not _kernel32.GetConsoleScreenBufferInfo(
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   454
                    stdout, ctypes.byref(csbi)):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   455
            # stdout may not support GetConsoleScreenBufferInfo()
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   456
            # when called from subprocess or redirected
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   457
            w32effects = None
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   458
        else:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   459
            origattr = csbi.wAttributes
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   460
            ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)',
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   461
                                re.MULTILINE | re.DOTALL)
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   462
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   463
    def win32print(text, orig, **opts):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   464
        label = opts.get('label', '')
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   465
        attr = origattr
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   466
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   467
        def mapcolor(val, attr):
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   468
            if val == -1:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   469
                return origattr
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   470
            elif val in passthrough:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   471
                return attr | val
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   472
            elif val > 0x0f:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   473
                return (val & 0x70) | (attr & 0x8f)
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   474
            else:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   475
                return (val & 0x07) | (attr & 0xf8)
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   476
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   477
        # determine console attributes based on labels
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   478
        for l in label.split():
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   479
            style = _styles.get(l, '')
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   480
            for effect in style.split():
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   481
                attr = mapcolor(w32effects[effect], attr)
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   482
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   483
        # hack to ensure regexp finds data
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   484
        if not text.startswith('\033['):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   485
            text = '\033[m' + text
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   486
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   487
        # Look for ANSI-like codes embedded in text
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   488
        m = re.match(ansire, text)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   489
13919
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   490
        try:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   491
            while m:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   492
                for sattr in m.group(1).split(';'):
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   493
                    if sattr:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   494
                        attr = mapcolor(int(sattr), attr)
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   495
                _kernel32.SetConsoleTextAttribute(stdout, attr)
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   496
                orig(m.group(2), **opts)
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   497
                m = re.match(ansire, m.group(3))
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   498
        finally:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   499
            # Explicity reset original attributes
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   500
            _kernel32.SetConsoleTextAttribute(stdout, origattr)