mercurial/color.py
author Augie Fackler <raf@durin42.com>
Tue, 21 Jan 2020 13:16:30 -0500
branchstable
changeset 44131 3246cd5310b0
parent 43823 ad84fc97d120
child 44311 d3f776c4760e
permissions -rw-r--r--
Added signature for changeset 84a0102c05c7
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 _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    13
from .pycompat import getattr
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
    14
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
    15
from . import (
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
    16
    encoding,
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
    17
    pycompat,
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36532
diff changeset
    18
)
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36532
diff changeset
    19
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
    20
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
    21
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    22
try:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    23
    import curses
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
    24
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    25
    # 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
    26
    # 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
    27
    _baseterminfoparams = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
        b'none': (True, b'sgr0', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
        b'standout': (True, b'smso', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    30
        b'underline': (True, b'smul', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    31
        b'reverse': (True, b'rev', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    32
        b'inverse': (True, b'rev', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
        b'blink': (True, b'blink', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
        b'dim': (True, b'dim', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
        b'bold': (True, b'bold', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
        b'invisible': (True, b'invis', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    37
        b'italic': (True, b'sitm', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    38
        b'black': (False, curses.COLOR_BLACK, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    39
        b'red': (False, curses.COLOR_RED, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    40
        b'green': (False, curses.COLOR_GREEN, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
        b'yellow': (False, curses.COLOR_YELLOW, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    42
        b'blue': (False, curses.COLOR_BLUE, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    43
        b'magenta': (False, curses.COLOR_MAGENTA, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    44
        b'cyan': (False, curses.COLOR_CYAN, b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
        b'white': (False, curses.COLOR_WHITE, b''),
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    46
    }
30988
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30987
diff changeset
    47
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
    48
    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
    49
    _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
    50
30987
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30657
diff changeset
    51
# start and stop parameters for effects
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
    52
_effects = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    53
    b'none': 0,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
    b'black': 30,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
    b'red': 31,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    56
    b'green': 32,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    57
    b'yellow': 33,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    58
    b'blue': 34,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    59
    b'magenta': 35,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    60
    b'cyan': 36,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    61
    b'white': 37,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    62
    b'bold': 1,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
    b'italic': 3,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    64
    b'underline': 4,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    65
    b'inverse': 7,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    66
    b'dim': 2,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    67
    b'black_background': 40,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    68
    b'red_background': 41,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    69
    b'green_background': 42,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
    b'yellow_background': 43,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
    b'blue_background': 44,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
    b'purple_background': 45,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    73
    b'cyan_background': 46,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
    b'white_background': 47,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
    75
}
30987
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30657
diff changeset
    76
31133
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31132
diff changeset
    77
_defaultstyles = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
    b'grep.match': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
    b'grep.linenumber': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    80
    b'grep.rev': b'blue',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
    b'grep.sep': b'cyan',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    82
    b'grep.filename': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    83
    b'grep.user': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    84
    b'grep.date': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
    b'grep.inserted': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    86
    b'grep.deleted': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    87
    b'bookmarks.active': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    88
    b'branches.active': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    89
    b'branches.closed': b'black bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    90
    b'branches.current': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    91
    b'branches.inactive': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    92
    b'diff.changed': b'white',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    93
    b'diff.deleted': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    94
    b'diff.deleted.changed': b'red bold underline',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
    b'diff.deleted.unchanged': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    96
    b'diff.diffline': b'bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    97
    b'diff.extended': b'cyan bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    98
    b'diff.file_a': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
    b'diff.file_b': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
    b'diff.hunk': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   101
    b'diff.inserted': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
    b'diff.inserted.changed': b'green bold underline',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   103
    b'diff.inserted.unchanged': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   104
    b'diff.tab': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   105
    b'diff.trailingwhitespace': b'bold red_background',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   106
    b'changeset.public': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
    b'changeset.draft': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   108
    b'changeset.secret': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   109
    b'diffstat.deleted': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   110
    b'diffstat.inserted': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   111
    b'formatvariant.name.mismatchconfig': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   112
    b'formatvariant.name.mismatchdefault': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   113
    b'formatvariant.name.uptodate': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
    b'formatvariant.repo.mismatchconfig': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   115
    b'formatvariant.repo.mismatchdefault': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   116
    b'formatvariant.repo.uptodate': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   117
    b'formatvariant.config.special': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   118
    b'formatvariant.config.default': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   119
    b'formatvariant.default': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   120
    b'histedit.remaining': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   121
    b'ui.addremove.added': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   122
    b'ui.addremove.removed': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   123
    b'ui.error': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   124
    b'ui.prompt': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   125
    b'log.changeset': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
    b'patchbomb.finalsummary': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   127
    b'patchbomb.from': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   128
    b'patchbomb.to': b'cyan',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   129
    b'patchbomb.subject': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   130
    b'patchbomb.diffstats': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   131
    b'rebase.rebased': b'blue',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   132
    b'rebase.remaining': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   133
    b'resolve.resolved': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   134
    b'resolve.unresolved': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   135
    b'shelve.age': b'cyan',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   136
    b'shelve.newest': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   137
    b'shelve.name': b'blue bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   138
    b'status.added': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   139
    b'status.clean': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   140
    b'status.copied': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   141
    b'status.deleted': b'cyan bold underline',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   142
    b'status.ignored': b'black bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   143
    b'status.modified': b'blue bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   144
    b'status.removed': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   145
    b'status.unknown': b'magenta bold underline',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   146
    b'tags.normal': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   147
    b'tags.local': b'black bold',
43823
ad84fc97d120 upgrade-repo: colorize some of the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43807
diff changeset
   148
    b'upgrade-repo.requirement.preserved': b'cyan',
ad84fc97d120 upgrade-repo: colorize some of the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43807
diff changeset
   149
    b'upgrade-repo.requirement.added': b'green',
ad84fc97d120 upgrade-repo: colorize some of the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43807
diff changeset
   150
    b'upgrade-repo.requirement.removed': b'red',
31126
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31123
diff changeset
   151
}
30657
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30656
diff changeset
   152
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   153
30657
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30656
diff changeset
   154
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
   155
    _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
   156
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   157
33706
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   158
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
   159
    '''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
   160
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   161
    # 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
   162
    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
   163
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   164
    # Otherwise, see what the config file says.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   165
    if mode not in (b'auto', b'terminfo'):
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   166
        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
   167
    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
   168
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   169
    for key, val in ui.configitems(b'color'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   170
        if key.startswith(b'color.'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   171
            newval = (False, int(val), b'')
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
   172
            ui._terminfoparams[key[6:]] = newval
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   173
        elif key.startswith(b'terminfo.'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   174
            newval = (True, b'', val.replace(b'\\E', b'\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
   175
            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
   176
    try:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   177
        curses.setupterm()
41387
876494fd967d cleanup: delete lots of unused local variables
Martin von Zweigbergk <martinvonz@google.com>
parents: 40983
diff changeset
   178
    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
   179
        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
   180
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   181
37662
5340daa85c62 py3: iterate over a copy of dict while changing it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37087
diff changeset
   182
    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
   183
        if not b:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   184
            continue
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   185
        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
   186
            # 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
   187
            # noisy and use ui.debug().
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   188
            ui.debug(b"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
   189
            del ui._terminfoparams[key]
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   190
    if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
31117
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   191
        # 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
   192
        # terminfo mode and we're in a formatted terminal.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   193
        if mode == b"terminfo" and formatted:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   194
            ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   195
                _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   196
                    b"no terminfo entry for setab/setaf: reverting to "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   197
                    b"ECMA-48 color\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   198
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   199
            )
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
   200
        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
   201
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   202
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
   203
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
   204
    """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
   205
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
   206
    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
   207
    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
   208
    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
   209
    ui._colormode = mode
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   210
    if mode and mode != b'debug':
31122
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31120
diff changeset
   211
        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
   212
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   213
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
def _modesetup(ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   215
    if ui.plain(b'color'):
31120
c1997c5d1ae3 color: handle 'ui.plain()' directly in mode setup
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31118
diff changeset
   216
        return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   217
    config = ui.config(b'ui', b'color')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   218
    if config == b'debug':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   219
        return b'debug'
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   220
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   221
    auto = config == b'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
   222
    always = False
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36532
diff changeset
   223
    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
   224
        # 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
   225
        # but "always" value is treated as a special case to reduce confusion.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   226
        if (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   227
            ui.configsource(b'ui', b'color') == b'--color'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   228
            or config == b'always'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   229
        ):
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
   230
            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
   231
        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
   232
            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
   233
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   234
    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
   235
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   236
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   237
    formatted = always or (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   238
        encoding.environ.get(b'TERM') != b'dumb' and ui.formatted()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   239
    )
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   240
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   241
    mode = ui.config(b'color', b'mode')
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   242
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   243
    # 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
   244
    if getattr(ui, 'pageractive', False):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   245
        mode = ui.config(b'color', b'pagermode', mode)
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   246
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   247
    realmode = mode
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
   248
    if pycompat.iswindows:
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   249
        from . import win32
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   250
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   251
        term = encoding.environ.get(b'TERM')
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   252
        # 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
   253
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   254
        # 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
   255
        # 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
   256
        # 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
   257
        # defined support ANSI.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   258
        ansienviron = term and b'xterm' in term
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   259
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   260
        if mode == b'auto':
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   261
            # 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
   262
            # 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
   263
            # 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
   264
            # 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
   265
            # 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
   266
            if ansienviron or not w32effects or win32.enablevtmode():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   267
                realmode = b'ansi'
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   268
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   269
                realmode = b'win32'
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   270
        # 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
   271
        # cannot enable VT mode.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   272
        elif mode == b'ansi' and w32effects and not ansienviron:
32685
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32331
diff changeset
   273
            win32.enablevtmode()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   274
    elif mode == b'auto':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   275
        realmode = b'ansi'
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   276
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   277
    def modewarn():
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   278
        # 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
   279
        # 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
   280
        if mode == realmode and formatted:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   281
            ui.warn(_(b'warning: failed to set color mode to %s\n') % mode)
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   282
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   283
    if realmode == b'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
   284
        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
   285
        if not w32effects:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   286
            modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   287
            return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   288
    elif realmode == b'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
   289
        ui._terminfoparams.clear()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   290
    elif realmode == b'terminfo':
33706
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   291
        _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
   292
        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
   293
            ## 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
   294
            modewarn()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   295
            realmode = b'ansi'
31118
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   296
    else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   297
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   298
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   299
    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
   300
        return realmode
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   301
    return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31117
diff changeset
   302
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   303
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   304
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
   305
    ui._styles.update(_defaultstyles)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   306
    for status, cfgeffects in ui.configitems(b'color'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   307
        if b'.' not in status or status.startswith((b'color.', b'terminfo.')):
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   308
            continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   309
        cfgeffects = ui.configlist(b'color', status)
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   310
        if cfgeffects:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   311
            good = []
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   312
            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
   313
                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
   314
                    good.append(e)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   315
                else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   316
                    ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   317
                        _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   318
                            b"ignoring unknown color/effect %s "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   319
                            b"(configured in color.%s)\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   320
                        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   321
                        % (stringutil.pprint(e), status)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   322
                    )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   323
            ui._styles[status] = b' '.join(good)
30991
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30990
diff changeset
   324
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   325
31694
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   326
def _activeeffects(ui):
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   327
    '''Return the effects map for the color mode set on the ui.'''
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   328
    if ui._colormode == b'win32':
31694
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   329
        return w32effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   330
    elif ui._colormode is not None:
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   331
        return _effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   332
    return {}
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   333
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
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 valideffect(ui, effect):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   336
    """Determine if the effect is valid or not."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   337
    return (not ui._terminfoparams and effect in _activeeffects(ui)) or (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   338
        effect in ui._terminfoparams or effect[:-11] in ui._terminfoparams
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   339
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   340
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   341
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   342
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
   343
    '''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
   344
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   345
    bg = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   346
    if effect.endswith(b'_background'):
30992
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30991
diff changeset
   347
        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
   348
        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
   349
    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
   350
        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
   351
    except KeyError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   352
        return b''
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
    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
   354
        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
   355
            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
   356
        else:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   357
            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
   358
    elif bg:
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   359
        return curses.tparm(curses.tigetstr('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
   360
    else:
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   361
        return curses.tparm(curses.tigetstr('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
   362
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   363
31524
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   364
def _mergeeffects(text, start, stop):
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   365
    """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
   366
34146
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33706
diff changeset
   367
    >>> 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
   368
    >>> 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
   369
    >>> 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
   370
    >>> 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
   371
    >>> s
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   372
    '[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
   373
    """
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   374
    parts = []
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   375
    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
   376
        if not t:
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   377
            continue
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   378
        parts.extend([start, t, stop])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   379
    return b''.join(parts)
31524
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   380
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   381
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   382
def _render_effects(ui, text, effects):
43807
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43554
diff changeset
   383
    """Wrap text in commands to turn on each effect."""
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
   384
    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
   385
        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
   386
    if ui._terminfoparams:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   387
        start = b''.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   388
            _effect_str(ui, effect) for effect in [b'none'] + effects.split()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   389
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   390
        stop = _effect_str(ui, b'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
   391
    else:
31694
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31527
diff changeset
   392
        activeeffects = _activeeffects(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   393
        start = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   394
            pycompat.bytestr(activeeffects[e])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   395
            for e in [b'none'] + effects.split()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   396
        ]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   397
        start = b'\033[' + b';'.join(start) + b'm'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   398
        stop = b'\033[' + pycompat.bytestr(activeeffects[b'none']) + b'm'
31524
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31506
diff changeset
   399
    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
   400
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   401
31527
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   402
_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
   403
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   404
31527
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   405
def stripeffects(text):
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   406
    """Strip ANSI control codes which could be inserted by colorlabel()"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   407
    return _ansieffectre.sub(b'', text)
31527
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31524
diff changeset
   408
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   409
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   410
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
   411
    """add color control code according to the mode"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   412
    if ui._colormode == b'debug':
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   413
        if label and msg:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   414
            if msg.endswith(b'\n'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   415
                msg = b"[%s|%s]\n" % (label, msg[:-1])
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   416
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   417
                msg = b"[%s|%s]" % (label, msg)
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
    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
   419
        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
        for l in label.split():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   421
            s = ui._styles.get(l, b'')
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   422
            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
   423
                effects.append(s)
31129
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31127
diff changeset
   424
            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
   425
                effects.append(l)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   426
        effects = b' '.join(effects)
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   427
        if effects:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   428
            msg = b'\n'.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   429
                [
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   430
                    _render_effects(ui, line, effects)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   431
                    for line in msg.split(b'\n')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   432
                ]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   433
            )
31103
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31088
diff changeset
   434
    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
   435
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   436
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   437
w32effects = None
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
   438
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
   439
    import ctypes
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   440
43524
949b4d545c90 color: suppress pytype warning on a windows-only module
Augie Fackler <augie@google.com>
parents: 43089
diff changeset
   441
    _kernel32 = ctypes.windll.kernel32  # pytype: disable=module-attr
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   442
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   443
    _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
   444
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   445
    _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
   446
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   447
    class _COORD(ctypes.Structure):
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   448
        _fields_ = [('X', ctypes.c_short), ('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
   449
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   450
    class _SMALL_RECT(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   451
        _fields_ = [
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   452
            ('Left', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   453
            ('Top', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   454
            ('Right', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   455
            ('Bottom', ctypes.c_short),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   456
        ]
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   457
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   458
    class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   459
        _fields_ = [
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   460
            ('dwSize', _COORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   461
            ('dwCursorPosition', _COORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   462
            ('wAttributes', _WORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   463
            ('srWindow', _SMALL_RECT),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   464
            ('dwMaximumWindowSize', _COORD),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   465
        ]
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   466
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   467
    _STD_OUTPUT_HANDLE = 0xFFFFFFF5  # (DWORD)-11
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   468
    _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
   469
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   470
    _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
   471
    _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
   472
    _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
   473
    _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
   474
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   475
    _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
   476
    _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
   477
    _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
   478
    _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
   479
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   480
    _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
   481
    _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
   482
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   483
    # 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
   484
    w32effects = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   485
        b'none': -1,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   486
        b'black': 0,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   487
        b'red': _FOREGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   488
        b'green': _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   489
        b'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   490
        b'blue': _FOREGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   491
        b'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   492
        b'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   493
        b'white': _FOREGROUND_RED | _FOREGROUND_GREEN | _FOREGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   494
        b'bold': _FOREGROUND_INTENSITY,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   495
        b'black_background': 0x100,  # unused value > 0x0f
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   496
        b'red_background': _BACKGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   497
        b'green_background': _BACKGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   498
        b'yellow_background': _BACKGROUND_RED | _BACKGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   499
        b'blue_background': _BACKGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   500
        b'purple_background': _BACKGROUND_BLUE | _BACKGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   501
        b'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   502
        b'white_background': (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   503
            _BACKGROUND_RED | _BACKGROUND_GREEN | _BACKGROUND_BLUE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   504
        ),
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   505
        b'bold_background': _BACKGROUND_INTENSITY,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   506
        b'underline': _COMMON_LVB_UNDERSCORE,  # double-byte charsets only
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   507
        b'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
   508
    }
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   509
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   510
    passthrough = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   511
        _FOREGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   512
        _BACKGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   513
        _COMMON_LVB_UNDERSCORE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   514
        _COMMON_LVB_REVERSE_VIDEO,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   515
    }
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   516
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   517
    stdout = _kernel32.GetStdHandle(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   518
        _STD_OUTPUT_HANDLE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   519
    )  # 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
   520
    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
   521
        w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   522
    else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   523
        csbi = _CONSOLE_SCREEN_BUFFER_INFO()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   524
        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
   525
            # 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
   526
            # 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
   527
            w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   528
        else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   529
            origattr = csbi.wAttributes
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   530
            ansire = re.compile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   531
                br'\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   532
            )
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   533
40572
51091816a355 ui: simply concatenate messages before applying color labels
Yuya Nishihara <yuya@tcha.org>
parents: 40571
diff changeset
   534
    def win32print(ui, writefunc, text, **opts):
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43524
diff changeset
   535
        label = opts.get('label', b'')
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   536
        attr = origattr
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
        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
   539
            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
   540
                return origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   541
            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
   542
                return attr | val
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   543
            elif val > 0x0F:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   544
                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
   545
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41750
diff changeset
   546
                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
   547
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   548
        # 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
   549
        for l in label.split():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   550
            style = ui._styles.get(l, b'')
31084
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   551
            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
   552
                try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   553
                    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
   554
                except KeyError:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   555
                    # 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
   556
                    # 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
   557
                    pass
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   558
        # 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
   559
        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
   560
            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
   561
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   562
        # 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
   563
        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
   564
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   565
        try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   566
            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
   567
                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
   568
                    if sattr:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   569
                        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
   570
                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
   571
                _kernel32.SetConsoleTextAttribute(stdout, attr)
40571
49746e53ac92 ui: simplify interface of low-level write() functions
Yuya Nishihara <yuya@tcha.org>
parents: 40367
diff changeset
   572
                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
   573
                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
   574
        finally:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30993
diff changeset
   575
            # 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
   576
            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
   577
            _kernel32.SetConsoleTextAttribute(stdout, origattr)