mercurial/color.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:45:02 -0400
changeset 43076 2372284d9457
parent 41750 a91615b71679
child 43077 687b865b95ad
permissions -rw-r--r--
formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30656
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
31527
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
    10
import re
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
    11
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
    12
from .i18n import _
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
    13
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
    14
from . import (
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
    15
    encoding,
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
    16
    pycompat,
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36532
diff changeset
    17
)
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36532
diff changeset
    18
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
    19
from .utils import stringutil
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
    20
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    21
try:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    22
    import curses
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
    23
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    24
    # 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: 30987
diff changeset
    25
    # color number.  This will also force-load the curses module.
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
    26
    _baseterminfoparams = {
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    27
        'none': (True, 'sgr0', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    28
        'standout': (True, 'smso', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    29
        'underline': (True, 'smul', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    30
        'reverse': (True, 'rev', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    31
        'inverse': (True, 'rev', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    32
        'blink': (True, 'blink', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    33
        'dim': (True, 'dim', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    34
        'bold': (True, 'bold', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    35
        'invisible': (True, 'invis', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    36
        'italic': (True, 'sitm', ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    37
        'black': (False, curses.COLOR_BLACK, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    38
        'red': (False, curses.COLOR_RED, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    39
        'green': (False, curses.COLOR_GREEN, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    40
        'yellow': (False, curses.COLOR_YELLOW, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    41
        'blue': (False, curses.COLOR_BLUE, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    42
        'magenta': (False, curses.COLOR_MAGENTA, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    43
        'cyan': (False, curses.COLOR_CYAN, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    44
        'white': (False, curses.COLOR_WHITE, ''),
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    45
    }
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    46
except ImportError:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    47
    curses = None
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
    48
    _baseterminfoparams = {}
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    49
30987
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30657
diff changeset
    50
# start and stop parameters for effects
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    51
_effects = {
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    52
    'none': 0,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    53
    'black': 30,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    54
    'red': 31,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    55
    'green': 32,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    56
    'yellow': 33,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    57
    'blue': 34,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    58
    'magenta': 35,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    59
    'cyan': 36,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    60
    'white': 37,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    61
    'bold': 1,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    62
    'italic': 3,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    63
    'underline': 4,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    64
    'inverse': 7,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    65
    'dim': 2,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    66
    'black_background': 40,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    67
    'red_background': 41,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    68
    'green_background': 42,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    69
    'yellow_background': 43,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    70
    'blue_background': 44,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    71
    'purple_background': 45,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    72
    'cyan_background': 46,
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    73
    'white_background': 47,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
    74
}
30987
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30657
diff changeset
    75
31133
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31132
diff changeset
    76
_defaultstyles = {
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    77
    'grep.match': 'red bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    78
    'grep.linenumber': 'green',
41750
a91615b71679 color: change color of grep.rev label (BC)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 41749
diff changeset
    79
    'grep.rev': 'blue',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    80
    'grep.sep': 'cyan',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    81
    'grep.filename': 'magenta',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    82
    'grep.user': 'magenta',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    83
    'grep.date': 'magenta',
41749
ee77a6dd8fb8 color: give colours to the grep.inserted and grep.deleted labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 41546
diff changeset
    84
    'grep.inserted': 'green bold',
ee77a6dd8fb8 color: give colours to the grep.inserted and grep.deleted labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 41546
diff changeset
    85
    'grep.deleted': 'red bold',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    86
    'bookmarks.active': 'green',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    87
    'branches.active': 'none',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    88
    'branches.closed': 'black bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    89
    'branches.current': 'green',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    90
    'branches.inactive': 'none',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    91
    'diff.changed': 'white',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    92
    'diff.deleted': 'red',
37799
1770d8b3e554 diff: restore original color scheme for worddiff
Yuya Nishihara <yuya@tcha.org>
parents: 37732
diff changeset
    93
    'diff.deleted.changed': 'red bold underline',
1770d8b3e554 diff: restore original color scheme for worddiff
Yuya Nishihara <yuya@tcha.org>
parents: 37732
diff changeset
    94
    'diff.deleted.unchanged': 'red',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    95
    'diff.diffline': 'bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    96
    'diff.extended': 'cyan bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    97
    'diff.file_a': 'red bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    98
    'diff.file_b': 'green bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    99
    'diff.hunk': 'magenta',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   100
    'diff.inserted': 'green',
37799
1770d8b3e554 diff: restore original color scheme for worddiff
Yuya Nishihara <yuya@tcha.org>
parents: 37732
diff changeset
   101
    'diff.inserted.changed': 'green bold underline',
1770d8b3e554 diff: restore original color scheme for worddiff
Yuya Nishihara <yuya@tcha.org>
parents: 37732
diff changeset
   102
    'diff.inserted.unchanged': 'green',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   103
    'diff.tab': '',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   104
    'diff.trailingwhitespace': 'bold red_background',
34486
a57c938e7ac8 style: never use a space before a colon or comma
Alex Gaynor <agaynor@mozilla.com>
parents: 34146
diff changeset
   105
    'changeset.public': '',
a57c938e7ac8 style: never use a space before a colon or comma
Alex Gaynor <agaynor@mozilla.com>
parents: 34146
diff changeset
   106
    'changeset.draft': '',
a57c938e7ac8 style: never use a space before a colon or comma
Alex Gaynor <agaynor@mozilla.com>
parents: 34146
diff changeset
   107
    'changeset.secret': '',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   108
    'diffstat.deleted': 'red',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   109
    'diffstat.inserted': 'green',
35347
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   110
    'formatvariant.name.mismatchconfig': 'red',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   111
    'formatvariant.name.mismatchdefault': 'yellow',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   112
    'formatvariant.name.uptodate': 'green',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   113
    'formatvariant.repo.mismatchconfig': 'red',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   114
    'formatvariant.repo.mismatchdefault': 'yellow',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   115
    'formatvariant.repo.uptodate': 'green',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   116
    'formatvariant.config.special': 'yellow',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   117
    'formatvariant.config.default': 'green',
bd326f3e0e14 debugformat: update label depending on value difference
Boris Feld <boris.feld@octobus.net>
parents: 35286
diff changeset
   118
    'formatvariant.default': '',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   119
    'histedit.remaining': 'red bold',
40367
824b687ff6af addremove: add "ui." prefix to message color keys
Yuya Nishihara <yuya@tcha.org>
parents: 40223
diff changeset
   120
    'ui.addremove.added': 'green',
824b687ff6af addremove: add "ui." prefix to message color keys
Yuya Nishihara <yuya@tcha.org>
parents: 40223
diff changeset
   121
    'ui.addremove.removed': 'red',
38809
afc4ad706f9c dispatch: making all hg abortions be output with a specific label
Rodrigo Damazio Bovendorp <rdamazio@google.com>
parents: 37799
diff changeset
   122
    'ui.error': 'red',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   123
    'ui.prompt': 'yellow',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   124
    'log.changeset': 'yellow',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   125
    'patchbomb.finalsummary': '',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   126
    'patchbomb.from': 'magenta',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   127
    'patchbomb.to': 'cyan',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   128
    'patchbomb.subject': 'green',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   129
    'patchbomb.diffstats': '',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   130
    'rebase.rebased': 'blue',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   131
    'rebase.remaining': 'red bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   132
    'resolve.resolved': 'green bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   133
    'resolve.unresolved': 'red bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   134
    'shelve.age': 'cyan',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   135
    'shelve.newest': 'green bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   136
    'shelve.name': 'blue bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   137
    'status.added': 'green bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   138
    'status.clean': 'none',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   139
    'status.copied': 'none',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   140
    'status.deleted': 'cyan bold underline',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   141
    'status.ignored': 'black bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   142
    'status.modified': 'blue bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   143
    'status.removed': 'red bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   144
    'status.unknown': 'magenta bold underline',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   145
    'tags.normal': 'green',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   146
    'tags.local': 'black bold',
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   147
}
30657
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30656
diff changeset
   148
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   149
30657
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30656
diff changeset
   150
def loadcolortable(ui, extname, colortable):
31133
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31132
diff changeset
   151
    _defaultstyles.update(colortable)
30989
ddc80d1777a6 color: move 'valideffect' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30988
diff changeset
   152
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   153
33706
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   154
def _terminfosetup(ui, mode, formatted):
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   155
    '''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: 31106
diff changeset
   156
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   157
    # 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: 31106
diff changeset
   158
    if curses is None:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   159
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   160
    # 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: 31106
diff changeset
   161
    if mode not in ('auto', 'terminfo'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   162
        return
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   163
    ui._terminfoparams.update(_baseterminfoparams)
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   164
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   165
    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: 31106
diff changeset
   166
        if key.startswith('color.'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   167
            newval = (False, int(val), '')
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   168
            ui._terminfoparams[key[6:]] = newval
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   169
        elif key.startswith('terminfo.'):
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   170
            newval = (True, '', val.replace('\\E', '\x1b'))
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   171
            ui._terminfoparams[key[9:]] = newval
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   172
    try:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   173
        curses.setupterm()
41387
876494fd967d cleanup: delete lots of unused local variables
Martin von Zweigbergk <martinvonz@google.com>
parents: 40983
diff changeset
   174
    except curses.error:
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   175
        ui._terminfoparams.clear()
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   176
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   177
37662
5340daa85c62 py3: iterate over a copy of dict while changing it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37087
diff changeset
   178
    for key, (b, e, c) in ui._terminfoparams.copy().items():
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   179
        if not b:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   180
            continue
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   181
        if not c and not curses.tigetstr(pycompat.sysstr(e)):
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   182
            # 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: 31106
diff changeset
   183
            # noisy and use ui.debug().
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   184
            ui.debug("no terminfo entry for %s\n" % e)
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   185
            del ui._terminfoparams[key]
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   186
    if not curses.tigetstr(r'setaf') or not curses.tigetstr(r'setab'):
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   187
        # Only warn about missing terminfo entries if we explicitly asked for
33706
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   188
        # terminfo mode and we're in a formatted terminal.
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   189
        if mode == "terminfo" and formatted:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   190
            ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   191
                _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   192
                    "no terminfo entry for setab/setaf: reverting to "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   193
                    "ECMA-48 color\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   194
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   195
            )
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   196
        ui._terminfoparams.clear()
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   197
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   198
31127
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31126
diff changeset
   199
def setup(ui):
31122
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   200
    """configure color on a ui
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   201
31127
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31126
diff changeset
   202
    That function both set the colormode for the ui object and read
31122
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   203
    the configuration looking for custom colors and effect definitions."""
31127
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31126
diff changeset
   204
    mode = _modesetup(ui)
31123
a185b903bda3 color: have the 'ui' object carry the '_colormode' directly
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31122
diff changeset
   205
    ui._colormode = mode
31122
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   206
    if mode and mode != 'debug':
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   207
        configstyles(ui)
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   209
31127
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31126
diff changeset
   210
def _modesetup(ui):
35192
d4805a5e7e70 color: respect HGPLAINEXCEPT=color to allow colors while scripting (issue5749)
Augie Fackler <augie@google.com>
parents: 34645
diff changeset
   211
    if ui.plain('color'):
31120
c1997c5d1ae3 color: handle 'ui.plain()' directly in mode setup
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31118
diff changeset
   212
        return None
33522
62b29ca72d1a configitems: register the 'ui.color' config
Boris Feld <boris.feld@octobus.net>
parents: 33521
diff changeset
   213
    config = ui.config('ui', 'color')
31127
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31126
diff changeset
   214
    if config == 'debug':
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   215
        return 'debug'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   216
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   217
    auto = config == 'auto'
32102
9a85ea1daf49 color: turn 'ui.color' into a boolean (auto or off)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32027
diff changeset
   218
    always = False
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36532
diff changeset
   219
    if not auto and stringutil.parsebool(config):
32103
9a98023ac8db color: special case 'always' in 'ui.color'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32102
diff changeset
   220
        # We want the config to behave like a boolean, "on" is actually auto,
9a98023ac8db color: special case 'always' in 'ui.color'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32102
diff changeset
   221
        # but "always" value is treated as a special case to reduce confusion.
9a98023ac8db color: special case 'always' in 'ui.color'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32102
diff changeset
   222
        if ui.configsource('ui', 'color') == '--color' or config == 'always':
32102
9a85ea1daf49 color: turn 'ui.color' into a boolean (auto or off)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32027
diff changeset
   223
            always = True
9a85ea1daf49 color: turn 'ui.color' into a boolean (auto or off)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32027
diff changeset
   224
        else:
9a85ea1daf49 color: turn 'ui.color' into a boolean (auto or off)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32027
diff changeset
   225
            auto = True
9a85ea1daf49 color: turn 'ui.color' into a boolean (auto or off)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32027
diff changeset
   226
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   227
    if not always and not auto:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   228
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   229
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   230
    formatted = always or (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   231
        encoding.environ.get('TERM') != 'dumb' and ui.formatted()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   232
    )
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   233
33181
95c57596b380 configitems: register the 'color.mode' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32685
diff changeset
   234
    mode = ui.config('color', 'mode')
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   235
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   236
    # 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: 31117
diff changeset
   237
    if getattr(ui, 'pageractive', False):
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   238
        mode = ui.config('color', 'pagermode', mode)
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   239
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   240
    realmode = mode
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
   241
    if pycompat.iswindows:
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   242
        from . import win32
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   243
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   244
        term = encoding.environ.get('TERM')
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   245
        # TERM won't be defined in a vanilla cmd.exe environment.
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   246
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   247
        # UNIX-like environments on Windows such as Cygwin and MSYS will
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   248
        # set TERM. They appear to make a best effort attempt at setting it
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   249
        # to something appropriate. However, not all environments with TERM
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   250
        # defined support ANSI.
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   251
        ansienviron = term and 'xterm' in term
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   252
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   253
        if mode == 'auto':
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   254
            # Since "ansi" could result in terminal gibberish, we error on the
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   255
            # side of selecting "win32". However, if w32effects is not defined,
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   256
            # we almost certainly don't support "win32", so don't even try.
40983
7654291091cf color: fix a documentation typo
Matt Harbison <matt_harbison@yahoo.com>
parents: 40572
diff changeset
   257
            # w32effects is not populated when stdout is redirected, so checking
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   258
            # it first avoids win32 calls in a state known to error out.
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   259
            if ansienviron or not w32effects or win32.enablevtmode():
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   260
                realmode = 'ansi'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   261
            else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   262
                realmode = 'win32'
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   263
        # An empty w32effects is a clue that stdout is redirected, and thus
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   264
        # cannot enable VT mode.
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   265
        elif mode == 'ansi' and w32effects and not ansienviron:
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   266
            win32.enablevtmode()
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   267
    elif mode == 'auto':
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   268
        realmode = 'ansi'
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   269
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   270
    def modewarn():
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   271
        # 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: 31117
diff changeset
   272
        # a formatted terminal
33706
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   273
        if mode == realmode and formatted:
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   274
            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: 31117
diff changeset
   275
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   276
    if realmode == 'win32':
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   277
        ui._terminfoparams.clear()
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   278
        if not w32effects:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   279
            modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   280
            return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   281
    elif realmode == 'ansi':
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   282
        ui._terminfoparams.clear()
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   283
    elif realmode == 'terminfo':
33706
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   284
        _terminfosetup(ui, mode, formatted)
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   285
        if not ui._terminfoparams:
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   286
            ## 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: 31117
diff changeset
   287
            modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   288
            realmode = 'ansi'
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   289
    else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   290
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   291
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   292
    if always or (auto and formatted):
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   293
        return realmode
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   294
    return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   295
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   296
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   297
def configstyles(ui):
31133
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31132
diff changeset
   298
    ui._styles.update(_defaultstyles)
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   299
    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: 30990
diff changeset
   300
        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: 30990
diff changeset
   301
            continue
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   302
        cfgeffects = ui.configlist('color', status)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   303
        if cfgeffects:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   304
            good = []
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   305
            for e in cfgeffects:
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   306
                if valideffect(ui, e):
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   307
                    good.append(e)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   308
                else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   309
                    ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   310
                        _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   311
                            "ignoring unknown color/effect %s "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   312
                            "(configured in color.%s)\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   313
                        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   314
                        % (stringutil.pprint(e), status)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   315
                    )
31132
f5131d4f512a color: move 'styles' definition on the 'ui' object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31131
diff changeset
   316
            ui._styles[status] = ' '.join(good)
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   317
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   318
31694
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   319
def _activeeffects(ui):
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   320
    '''Return the effects map for the color mode set on the ui.'''
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   321
    if ui._colormode == 'win32':
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   322
        return w32effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   323
    elif ui._colormode is not None:
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   324
        return _effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   325
    return {}
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   326
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   327
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   328
def valideffect(ui, effect):
30989
ddc80d1777a6 color: move 'valideffect' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30988
diff changeset
   329
    'Determine if the effect is valid or not.'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   330
    return (not ui._terminfoparams and effect in _activeeffects(ui)) or (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   331
        effect in ui._terminfoparams or effect[:-11] in ui._terminfoparams
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   332
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   333
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   334
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   335
def _effect_str(ui, effect):
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   336
    '''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: 30991
diff changeset
   337
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   338
    bg = False
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   339
    if effect.endswith('_background'):
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   340
        bg = True
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   341
        effect = effect[:-11]
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   342
    try:
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   343
        attr, val, termcode = ui._terminfoparams[effect]
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   344
    except KeyError:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   345
        return ''
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   346
    if attr:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   347
        if termcode:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   348
            return termcode
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   349
        else:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   350
            return curses.tigetstr(pycompat.sysstr(val))
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   351
    elif bg:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   352
        return curses.tparm(curses.tigetstr(r'setab'), val)
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   353
    else:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   354
        return curses.tparm(curses.tigetstr(r'setaf'), val)
30993
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30992
diff changeset
   355
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   356
31524
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   357
def _mergeeffects(text, start, stop):
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   358
    """Insert start sequence at every occurrence of stop sequence
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   359
34146
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33706
diff changeset
   360
    >>> s = _mergeeffects(b'cyan', b'[C]', b'|')
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33706
diff changeset
   361
    >>> s = _mergeeffects(s + b'yellow', b'[Y]', b'|')
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33706
diff changeset
   362
    >>> s = _mergeeffects(b'ma' + s + b'genta', b'[M]', b'|')
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33706
diff changeset
   363
    >>> s = _mergeeffects(b'red' + s, b'[R]', b'|')
31524
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   364
    >>> s
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   365
    '[R]red[M]ma[Y][C]cyan|[R][M][Y]yellow|[R][M]genta|'
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   366
    """
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   367
    parts = []
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   368
    for t in text.split(stop):
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   369
        if not t:
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   370
            continue
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   371
        parts.extend([start, t, stop])
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   372
    return ''.join(parts)
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   373
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   374
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   375
def _render_effects(ui, text, effects):
30993
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30992
diff changeset
   376
    '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: 30992
diff changeset
   377
    if not text:
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30992
diff changeset
   378
        return text
31130
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31129
diff changeset
   379
    if ui._terminfoparams:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   380
        start = ''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   381
            _effect_str(ui, effect) for effect in ['none'] + effects.split()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   382
        )
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   383
        stop = _effect_str(ui, 'none')
31088
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31084
diff changeset
   384
    else:
31694
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   385
        activeeffects = _activeeffects(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   386
        start = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   387
            pycompat.bytestr(activeeffects[e])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   388
            for e in ['none'] + effects.split()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   389
        ]
30993
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30992
diff changeset
   390
        start = '\033[' + ';'.join(start) + 'm'
31721
439a387ca6f1 color: replace str() with pycompat.bytestr()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31694
diff changeset
   391
        stop = '\033[' + pycompat.bytestr(activeeffects['none']) + 'm'
31524
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   392
    return _mergeeffects(text, start, stop)
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   393
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   394
31527
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   395
_ansieffectre = re.compile(br'\x1b\[[0-9;]*m')
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   396
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   397
31527
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   398
def stripeffects(text):
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   399
    """Strip ANSI control codes which could be inserted by colorlabel()"""
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   400
    return _ansieffectre.sub('', text)
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   401
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   402
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   403
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: 31088
diff changeset
   404
    """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: 31088
diff changeset
   405
    if ui._colormode == 'debug':
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   406
        if label and msg:
36532
faaabe0dc4d1 py3: use bytes.endswith('\n') to strip off '\n' from debug color output
Yuya Nishihara <yuya@tcha.org>
parents: 35360
diff changeset
   407
            if msg.endswith('\n'):
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   408
                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: 31088
diff changeset
   409
            else:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   410
                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: 31088
diff changeset
   411
    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: 31088
diff changeset
   412
        effects = []
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   413
        for l in label.split():
31132
f5131d4f512a color: move 'styles' definition on the 'ui' object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31131
diff changeset
   414
            s = ui._styles.get(l, '')
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   415
            if s:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   416
                effects.append(s)
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   417
            elif valideffect(ui, l):
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   418
                effects.append(l)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   419
        effects = ' '.join(effects)
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   420
        if effects:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   421
            msg = '\n'.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   422
                [_render_effects(ui, line, effects) for line in msg.split('\n')]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   423
            )
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   424
    return msg
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   425
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   426
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   427
w32effects = None
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
   428
if pycompat.iswindows:
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   429
    import ctypes
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   430
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   431
    _kernel32 = ctypes.windll.kernel32
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   432
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   433
    _WORD = ctypes.c_ushort
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   434
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   435
    _INVALID_HANDLE_VALUE = -1
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   436
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   437
    class _COORD(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   438
        _fields_ = [(r'X', ctypes.c_short), (r'Y', ctypes.c_short)]
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   439
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   440
    class _SMALL_RECT(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   441
        _fields_ = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   442
            (r'Left', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   443
            (r'Top', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   444
            (r'Right', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   445
            (r'Bottom', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   446
        ]
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   447
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   448
    class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   449
        _fields_ = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   450
            (r'dwSize', _COORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   451
            (r'dwCursorPosition', _COORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   452
            (r'wAttributes', _WORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   453
            (r'srWindow', _SMALL_RECT),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   454
            (r'dwMaximumWindowSize', _COORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   455
        ]
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   456
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   457
    _STD_OUTPUT_HANDLE = 0xFFFFFFF5  # (DWORD)-11
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   458
    _STD_ERROR_HANDLE = 0xFFFFFFF4  # (DWORD)-12
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   459
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   460
    _FOREGROUND_BLUE = 0x0001
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   461
    _FOREGROUND_GREEN = 0x0002
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   462
    _FOREGROUND_RED = 0x0004
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   463
    _FOREGROUND_INTENSITY = 0x0008
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   464
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   465
    _BACKGROUND_BLUE = 0x0010
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   466
    _BACKGROUND_GREEN = 0x0020
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   467
    _BACKGROUND_RED = 0x0040
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   468
    _BACKGROUND_INTENSITY = 0x0080
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   469
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   470
    _COMMON_LVB_REVERSE_VIDEO = 0x4000
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   471
    _COMMON_LVB_UNDERSCORE = 0x8000
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   472
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   473
    # 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: 30993
diff changeset
   474
    w32effects = {
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   475
        'none': -1,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   476
        'black': 0,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   477
        'red': _FOREGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   478
        'green': _FOREGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   479
        'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   480
        'blue': _FOREGROUND_BLUE,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   481
        'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   482
        'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   483
        '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: 30993
diff changeset
   484
        'bold': _FOREGROUND_INTENSITY,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   485
        'black_background': 0x100,  # unused value > 0x0f
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   486
        'red_background': _BACKGROUND_RED,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   487
        'green_background': _BACKGROUND_GREEN,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   488
        '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: 30993
diff changeset
   489
        'blue_background': _BACKGROUND_BLUE,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   490
        '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: 30993
diff changeset
   491
        'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   492
        'white_background': (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   493
            _BACKGROUND_RED | _BACKGROUND_GREEN | _BACKGROUND_BLUE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   494
        ),
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   495
        'bold_background': _BACKGROUND_INTENSITY,
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   496
        'underline': _COMMON_LVB_UNDERSCORE,  # double-byte charsets only
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   497
        'inverse': _COMMON_LVB_REVERSE_VIDEO,  # double-byte charsets only
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   498
    }
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   499
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   500
    passthrough = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   501
        _FOREGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   502
        _BACKGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   503
        _COMMON_LVB_UNDERSCORE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   504
        _COMMON_LVB_REVERSE_VIDEO,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   505
    }
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   506
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   507
    stdout = _kernel32.GetStdHandle(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   508
        _STD_OUTPUT_HANDLE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   509
    )  # don't close the handle returned
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   510
    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: 30993
diff changeset
   511
        w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   512
    else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   513
        csbi = _CONSOLE_SCREEN_BUFFER_INFO()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   514
        if not _kernel32.GetConsoleScreenBufferInfo(stdout, ctypes.byref(csbi)):
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   515
            # stdout may not support GetConsoleScreenBufferInfo()
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   516
            # 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: 30993
diff changeset
   517
            w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   518
        else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   519
            origattr = csbi.wAttributes
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   520
            ansire = re.compile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   521
                br'\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   522
            )
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   523
40572
51091816a355 ui: simply concatenate messages before applying color labels
Yuya Nishihara <yuya@tcha.org>
parents: 40571
diff changeset
   524
    def win32print(ui, writefunc, text, **opts):
35360
a4478f74ad56 py3: handle keyword arguments correctly in color.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35347
diff changeset
   525
        label = opts.get(r'label', '')
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   526
        attr = origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   527
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   528
        def mapcolor(val, attr):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   529
            if val == -1:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   530
                return origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   531
            elif val in passthrough:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   532
                return attr | val
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   533
            elif val > 0x0F:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   534
                return (val & 0x70) | (attr & 0x8F)
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   535
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   536
                return (val & 0x07) | (attr & 0xF8)
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   537
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   538
        # 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: 30993
diff changeset
   539
        for l in label.split():
31132
f5131d4f512a color: move 'styles' definition on the 'ui' object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31131
diff changeset
   540
            style = ui._styles.get(l, '')
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   541
            for effect in style.split():
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   542
                try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   543
                    attr = mapcolor(w32effects[effect], attr)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   544
                except KeyError:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   545
                    # 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: 30993
diff changeset
   546
                    # them if not found
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   547
                    pass
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   548
        # hack to ensure regexp finds data
39660
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39128
diff changeset
   549
        if not text.startswith(b'\033['):
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39128
diff changeset
   550
            text = b'\033[m' + text
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   551
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   552
        # 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: 30993
diff changeset
   553
        m = re.match(ansire, text)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   554
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   555
        try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   556
            while m:
39660
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39128
diff changeset
   557
                for sattr in m.group(1).split(b';'):
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   558
                    if sattr:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   559
                        attr = mapcolor(int(sattr), attr)
31506
31d2ddfd338c color: sync text attributes and buffered text output on Windows (issue5508)
Matt Harbison <matt_harbison@yahoo.com>
parents: 31138
diff changeset
   560
                ui.flush()
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   561
                _kernel32.SetConsoleTextAttribute(stdout, attr)
40571
49746e53ac92 ui: simplify interface of low-level write() functions
Yuya Nishihara <yuya@tcha.org>
parents: 40367
diff changeset
   562
                writefunc(m.group(2))
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   563
                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: 30993
diff changeset
   564
        finally:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   565
            # Explicitly reset original attributes
31506
31d2ddfd338c color: sync text attributes and buffered text output on Windows (issue5508)
Matt Harbison <matt_harbison@yahoo.com>
parents: 31138
diff changeset
   566
            ui.flush()
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   567
            _kernel32.SetConsoleTextAttribute(stdout, origattr)