mercurial/color.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Tue, 21 Feb 2017 17:51:43 +0100
changeset 31103 c1997c5d1ae3
parent 31101 9021a94a7dbf
child 31105 45be7590301d
permissions -rw-r--r--
color: handle 'ui.plain()' directly in mode setup If 'ui.plain()' is set we should not colorize. We move that logic into the function that determine and setup the color mode. As all other code respect the resulting mode this will be equivalent.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30652
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     1
# utility for color output for Mercurial commands
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     2
#
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     3
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> and other
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     4
#
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     7
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     8
from __future__ import absolute_import
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     9
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
    10
from .i18n import _
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
    11
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    12
from . import (
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    13
    encoding,
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    14
    pycompat,
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    15
    util
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    16
)
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
    17
30968
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    18
try:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    19
    import curses
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    20
    # Mapping from effect name to terminfo attribute name (or raw code) or
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    21
    # color number.  This will also force-load the curses module.
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    22
    _terminfo_params = {'none': (True, 'sgr0', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    23
                        'standout': (True, 'smso', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    24
                        'underline': (True, 'smul', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    25
                        'reverse': (True, 'rev', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    26
                        'inverse': (True, 'rev', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    27
                        'blink': (True, 'blink', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    28
                        'dim': (True, 'dim', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    29
                        'bold': (True, 'bold', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    30
                        'invisible': (True, 'invis', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    31
                        'italic': (True, 'sitm', ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    32
                        'black': (False, curses.COLOR_BLACK, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    33
                        'red': (False, curses.COLOR_RED, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    34
                        'green': (False, curses.COLOR_GREEN, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    35
                        'yellow': (False, curses.COLOR_YELLOW, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    36
                        'blue': (False, curses.COLOR_BLUE, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    37
                        'magenta': (False, curses.COLOR_MAGENTA, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    38
                        'cyan': (False, curses.COLOR_CYAN, ''),
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    39
                        'white': (False, curses.COLOR_WHITE, '')}
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    40
except ImportError:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    41
    curses = None
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    42
    _terminfo_params = {}
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    43
30967
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    44
# start and stop parameters for effects
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    45
_effects = {'none': 0,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    46
            'black': 30,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    47
            'red': 31,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    48
            'green': 32,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    49
            'yellow': 33,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    50
            'blue': 34,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    51
            'magenta': 35,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    52
            'cyan': 36,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    53
            'white': 37,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    54
            'bold': 1,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    55
            'italic': 3,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    56
            'underline': 4,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    57
            'inverse': 7,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    58
            'dim': 2,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    59
            'black_background': 40,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    60
            'red_background': 41,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    61
            'green_background': 42,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    62
            'yellow_background': 43,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    63
            'blue_background': 44,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    64
            'purple_background': 45,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    65
            'cyan_background': 46,
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    66
            'white_background': 47}
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    67
30652
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    68
_styles = {'grep.match': 'red bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    69
           'grep.linenumber': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    70
           'grep.rev': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    71
           'grep.change': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    72
           'grep.sep': 'cyan',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    73
           'grep.filename': 'magenta',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    74
           'grep.user': 'magenta',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    75
           'grep.date': 'magenta',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    76
           'bookmarks.active': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    77
           'branches.active': 'none',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    78
           'branches.closed': 'black bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    79
           'branches.current': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    80
           'branches.inactive': 'none',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    81
           'diff.changed': 'white',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    82
           'diff.deleted': 'red',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    83
           'diff.diffline': 'bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    84
           'diff.extended': 'cyan bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    85
           'diff.file_a': 'red bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    86
           'diff.file_b': 'green bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    87
           'diff.hunk': 'magenta',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    88
           'diff.inserted': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    89
           'diff.tab': '',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    90
           'diff.trailingwhitespace': 'bold red_background',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    91
           'changeset.public' : '',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    92
           'changeset.draft' : '',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    93
           'changeset.secret' : '',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    94
           'diffstat.deleted': 'red',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    95
           'diffstat.inserted': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    96
           'histedit.remaining': 'red bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    97
           'ui.prompt': 'yellow',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    98
           'log.changeset': 'yellow',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    99
           'patchbomb.finalsummary': '',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   100
           'patchbomb.from': 'magenta',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   101
           'patchbomb.to': 'cyan',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   102
           'patchbomb.subject': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   103
           'patchbomb.diffstats': '',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   104
           'rebase.rebased': 'blue',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   105
           'rebase.remaining': 'red bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   106
           'resolve.resolved': 'green bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   107
           'resolve.unresolved': 'red bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   108
           'shelve.age': 'cyan',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   109
           'shelve.newest': 'green bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   110
           'shelve.name': 'blue bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   111
           'status.added': 'green bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   112
           'status.clean': 'none',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   113
           'status.copied': 'none',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   114
           'status.deleted': 'cyan bold underline',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   115
           'status.ignored': 'black bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   116
           'status.modified': 'blue bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   117
           'status.removed': 'red bold',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   118
           'status.unknown': 'magenta bold underline',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   119
           'tags.normal': 'green',
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
   120
           'tags.local': 'black bold'}
30653
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
   121
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
   122
def loadcolortable(ui, extname, colortable):
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
   123
    _styles.update(colortable)
30969
ddc80d1777a6 color: move 'valideffect' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30968
diff changeset
   124
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   125
def _terminfosetup(ui, mode):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   126
    '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   127
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   128
    # If we failed to load curses, we go ahead and return.
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   129
    if curses is None:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   130
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   131
    # Otherwise, see what the config file says.
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   132
    if mode not in ('auto', 'terminfo'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   133
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   134
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   135
    for key, val in ui.configitems('color'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   136
        if key.startswith('color.'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   137
            newval = (False, int(val), '')
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   138
            _terminfo_params[key[6:]] = newval
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   139
        elif key.startswith('terminfo.'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   140
            newval = (True, '', val.replace('\\E', '\x1b'))
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   141
            _terminfo_params[key[9:]] = newval
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   142
    try:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   143
        curses.setupterm()
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   144
    except curses.error as e:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   145
        _terminfo_params.clear()
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   146
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   147
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   148
    for key, (b, e, c) in _terminfo_params.items():
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   149
        if not b:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   150
            continue
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   151
        if not c and not curses.tigetstr(e):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   152
            # Most terminals don't support dim, invis, etc, so don't be
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   153
            # noisy and use ui.debug().
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   154
            ui.debug("no terminfo entry for %s\n" % e)
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   155
            del _terminfo_params[key]
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   156
    if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   157
        # Only warn about missing terminfo entries if we explicitly asked for
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   158
        # terminfo mode.
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   159
        if mode == "terminfo":
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   160
            ui.warn(_("no terminfo entry for setab/setaf: reverting to "
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   161
              "ECMA-48 color\n"))
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   162
        _terminfo_params.clear()
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   163
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   164
def _modesetup(ui, coloropt):
31103
c1997c5d1ae3 color: handle 'ui.plain()' directly in mode setup
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31101
diff changeset
   165
    if ui.plain():
c1997c5d1ae3 color: handle 'ui.plain()' directly in mode setup
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31101
diff changeset
   166
        return None
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   167
    if coloropt == 'debug':
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   168
        return 'debug'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   169
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   170
    auto = (coloropt == 'auto')
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   171
    always = not auto and util.parsebool(coloropt)
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   172
    if not always and not auto:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   173
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   174
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   175
    formatted = (always or (encoding.environ.get('TERM') != 'dumb'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   176
                 and ui.formatted()))
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   177
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   178
    mode = ui.config('color', 'mode', 'auto')
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   179
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   180
    # If pager is active, color.pagermode overrides color.mode.
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   181
    if getattr(ui, 'pageractive', False):
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   182
        mode = ui.config('color', 'pagermode', mode)
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   183
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   184
    realmode = mode
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   185
    if mode == 'auto':
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   186
        if pycompat.osname == 'nt':
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   187
            term = encoding.environ.get('TERM')
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   188
            # TERM won't be defined in a vanilla cmd.exe environment.
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   189
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   190
            # UNIX-like environments on Windows such as Cygwin and MSYS will
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   191
            # set TERM. They appear to make a best effort attempt at setting it
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   192
            # to something appropriate. However, not all environments with TERM
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   193
            # defined support ANSI. Since "ansi" could result in terminal
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   194
            # gibberish, we error on the side of selecting "win32". However, if
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   195
            # w32effects is not defined, we almost certainly don't support
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   196
            # "win32", so don't even try.
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   197
            if (term and 'xterm' in term) or not w32effects:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   198
                realmode = 'ansi'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   199
            else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   200
                realmode = 'win32'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   201
        else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   202
            realmode = 'ansi'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   203
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   204
    def modewarn():
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   205
        # only warn if color.mode was explicitly set and we're in
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   206
        # a formatted terminal
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   207
        if mode == realmode and ui.formatted():
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   208
            ui.warn(_('warning: failed to set color mode to %s\n') % mode)
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   209
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   210
    if realmode == 'win32':
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   211
        _terminfo_params.clear()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   212
        if not w32effects:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   213
            modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   214
            return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   215
        _effects.update(w32effects)
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   216
    elif realmode == 'ansi':
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   217
        _terminfo_params.clear()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   218
    elif realmode == 'terminfo':
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   219
        _terminfosetup(ui, mode)
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   220
        if not _terminfo_params:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   221
            ## FIXME Shouldn't we return None in this case too?
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   222
            modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   223
            realmode = 'ansi'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   224
    else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   225
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   226
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   227
    if always or (auto and formatted):
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   228
        return realmode
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   229
    return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   230
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   231
def configstyles(ui):
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   232
    for status, cfgeffects in ui.configitems('color'):
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   233
        if '.' not in status or status.startswith(('color.', 'terminfo.')):
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   234
            continue
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   235
        cfgeffects = ui.configlist('color', status)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   236
        if cfgeffects:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   237
            good = []
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   238
            for e in cfgeffects:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   239
                if valideffect(e):
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   240
                    good.append(e)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   241
                else:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   242
                    ui.warn(_("ignoring unknown color/effect %r "
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   243
                              "(configured in color.%s)\n")
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   244
                            % (e, status))
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   245
            _styles[status] = ' '.join(good)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   246
30969
ddc80d1777a6 color: move 'valideffect' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30968
diff changeset
   247
def valideffect(effect):
ddc80d1777a6 color: move 'valideffect' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30968
diff changeset
   248
    'Determine if the effect is valid or not.'
30970
683606a829e8 color: rework conditional 'valideffect'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30969
diff changeset
   249
    return ((not _terminfo_params and effect in _effects)
683606a829e8 color: rework conditional 'valideffect'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30969
diff changeset
   250
             or (effect in _terminfo_params
683606a829e8 color: rework conditional 'valideffect'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30969
diff changeset
   251
                 or effect[:-11] in _terminfo_params))
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   252
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   253
def _effect_str(effect):
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   254
    '''Helper function for render_effects().'''
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   255
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   256
    bg = False
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   257
    if effect.endswith('_background'):
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   258
        bg = True
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   259
        effect = effect[:-11]
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   260
    try:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   261
        attr, val, termcode = _terminfo_params[effect]
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   262
    except KeyError:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   263
        return ''
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   264
    if attr:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   265
        if termcode:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   266
            return termcode
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   267
        else:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   268
            return curses.tigetstr(val)
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   269
    elif bg:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   270
        return curses.tparm(curses.tigetstr('setab'), val)
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   271
    else:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   272
        return curses.tparm(curses.tigetstr('setaf'), val)
30973
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   273
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   274
def _render_effects(text, effects):
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   275
    'Wrap text in commands to turn on each effect.'
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   276
    if not text:
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   277
        return text
31071
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31067
diff changeset
   278
    if _terminfo_params:
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31067
diff changeset
   279
        start = ''.join(_effect_str(effect)
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31067
diff changeset
   280
                        for effect in ['none'] + effects.split())
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31067
diff changeset
   281
        stop = _effect_str('none')
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31067
diff changeset
   282
    else:
30973
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   283
        start = [str(_effects[e]) for e in ['none'] + effects.split()]
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   284
        start = '\033[' + ';'.join(start) + 'm'
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   285
        stop = '\033[' + str(_effects['none']) + 'm'
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   286
    return ''.join([start, text, stop])
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   287
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   288
def colorlabel(ui, msg, label):
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   289
    """add color control code according to the mode"""
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   290
    if ui._colormode == 'debug':
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   291
        if label and msg:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   292
            if msg[-1] == '\n':
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   293
                msg = "[%s|%s]\n" % (label, msg[:-1])
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   294
            else:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   295
                msg = "[%s|%s]" % (label, msg)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   296
    elif ui._colormode is not None:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   297
        effects = []
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   298
        for l in label.split():
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   299
            s = _styles.get(l, '')
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   300
            if s:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   301
                effects.append(s)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   302
            elif valideffect(l):
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   303
                effects.append(l)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   304
        effects = ' '.join(effects)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   305
        if effects:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   306
            msg = '\n'.join([_render_effects(line, effects)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   307
                             for line in msg.split('\n')])
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   308
    return msg
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   309
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   310
w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   311
if pycompat.osname == 'nt':
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   312
    import ctypes
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   313
    import re
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   314
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   315
    _kernel32 = ctypes.windll.kernel32
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   316
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   317
    _WORD = ctypes.c_ushort
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   318
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   319
    _INVALID_HANDLE_VALUE = -1
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   320
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   321
    class _COORD(ctypes.Structure):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   322
        _fields_ = [('X', ctypes.c_short),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   323
                    ('Y', ctypes.c_short)]
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   324
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   325
    class _SMALL_RECT(ctypes.Structure):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   326
        _fields_ = [('Left', ctypes.c_short),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   327
                    ('Top', ctypes.c_short),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   328
                    ('Right', ctypes.c_short),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   329
                    ('Bottom', ctypes.c_short)]
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   330
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   331
    class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   332
        _fields_ = [('dwSize', _COORD),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   333
                    ('dwCursorPosition', _COORD),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   334
                    ('wAttributes', _WORD),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   335
                    ('srWindow', _SMALL_RECT),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   336
                    ('dwMaximumWindowSize', _COORD)]
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   337
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   338
    _STD_OUTPUT_HANDLE = 0xfffffff5 # (DWORD)-11
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   339
    _STD_ERROR_HANDLE = 0xfffffff4  # (DWORD)-12
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   340
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   341
    _FOREGROUND_BLUE = 0x0001
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   342
    _FOREGROUND_GREEN = 0x0002
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   343
    _FOREGROUND_RED = 0x0004
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   344
    _FOREGROUND_INTENSITY = 0x0008
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   345
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   346
    _BACKGROUND_BLUE = 0x0010
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   347
    _BACKGROUND_GREEN = 0x0020
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   348
    _BACKGROUND_RED = 0x0040
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   349
    _BACKGROUND_INTENSITY = 0x0080
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   350
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   351
    _COMMON_LVB_REVERSE_VIDEO = 0x4000
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   352
    _COMMON_LVB_UNDERSCORE = 0x8000
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   353
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   354
    # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   355
    w32effects = {
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   356
        'none': -1,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   357
        'black': 0,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   358
        'red': _FOREGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   359
        'green': _FOREGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   360
        'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   361
        'blue': _FOREGROUND_BLUE,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   362
        'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   363
        'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   364
        'white': _FOREGROUND_RED | _FOREGROUND_GREEN | _FOREGROUND_BLUE,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   365
        'bold': _FOREGROUND_INTENSITY,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   366
        'black_background': 0x100,                  # unused value > 0x0f
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   367
        'red_background': _BACKGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   368
        'green_background': _BACKGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   369
        'yellow_background': _BACKGROUND_RED | _BACKGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   370
        'blue_background': _BACKGROUND_BLUE,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   371
        'purple_background': _BACKGROUND_BLUE | _BACKGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   372
        'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   373
        'white_background': (_BACKGROUND_RED | _BACKGROUND_GREEN |
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   374
                             _BACKGROUND_BLUE),
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   375
        'bold_background': _BACKGROUND_INTENSITY,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   376
        'underline': _COMMON_LVB_UNDERSCORE,  # double-byte charsets only
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   377
        'inverse': _COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   378
    }
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   379
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   380
    passthrough = set([_FOREGROUND_INTENSITY,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   381
                       _BACKGROUND_INTENSITY,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   382
                       _COMMON_LVB_UNDERSCORE,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   383
                       _COMMON_LVB_REVERSE_VIDEO])
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   384
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   385
    stdout = _kernel32.GetStdHandle(
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   386
                  _STD_OUTPUT_HANDLE)  # don't close the handle returned
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   387
    if stdout is None or stdout == _INVALID_HANDLE_VALUE:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   388
        w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   389
    else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   390
        csbi = _CONSOLE_SCREEN_BUFFER_INFO()
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   391
        if not _kernel32.GetConsoleScreenBufferInfo(
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   392
                    stdout, ctypes.byref(csbi)):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   393
            # stdout may not support GetConsoleScreenBufferInfo()
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   394
            # when called from subprocess or redirected
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   395
            w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   396
        else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   397
            origattr = csbi.wAttributes
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   398
            ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)',
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   399
                                re.MULTILINE | re.DOTALL)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   400
31089
a2ee25ff75e5 color: add multiple messages input support to 'win32print'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   401
    def win32print(writefunc, *msgs, **opts):
a2ee25ff75e5 color: add multiple messages input support to 'win32print'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   402
        for text in msgs:
a2ee25ff75e5 color: add multiple messages input support to 'win32print'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   403
            _win32print(text, writefunc, **opts)
a2ee25ff75e5 color: add multiple messages input support to 'win32print'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   404
a2ee25ff75e5 color: add multiple messages input support to 'win32print'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   405
    def _win32print(text, writefunc, **opts):
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   406
        label = opts.get('label', '')
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   407
        attr = origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   408
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   409
        def mapcolor(val, attr):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   410
            if val == -1:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   411
                return origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   412
            elif val in passthrough:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   413
                return attr | val
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   414
            elif val > 0x0f:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   415
                return (val & 0x70) | (attr & 0x8f)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   416
            else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   417
                return (val & 0x07) | (attr & 0xf8)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   418
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   419
        # determine console attributes based on labels
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   420
        for l in label.split():
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   421
            style = _styles.get(l, '')
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   422
            for effect in style.split():
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   423
                try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   424
                    attr = mapcolor(w32effects[effect], attr)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   425
                except KeyError:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   426
                    # w32effects could not have certain attributes so we skip
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   427
                    # them if not found
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   428
                    pass
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   429
        # hack to ensure regexp finds data
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   430
        if not text.startswith('\033['):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   431
            text = '\033[m' + text
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   432
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   433
        # Look for ANSI-like codes embedded in text
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   434
        m = re.match(ansire, text)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   435
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   436
        try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   437
            while m:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   438
                for sattr in m.group(1).split(';'):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   439
                    if sattr:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   440
                        attr = mapcolor(int(sattr), attr)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   441
                _kernel32.SetConsoleTextAttribute(stdout, attr)
31088
75c4aafee490 color: clarify name of an argument of 'win32print'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31086
diff changeset
   442
                writefunc(m.group(2), **opts)
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   443
                m = re.match(ansire, m.group(3))
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   444
        finally:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   445
            # Explicitly reset original attributes
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   446
            _kernel32.SetConsoleTextAttribute(stdout, origattr)