mercurial/color.py
author pacien <pacien.trangirard@pacien.net>
Thu, 22 Sep 2022 16:09:53 +0200
changeset 49499 4f36738a869a
parent 48875 6000f5b25c9b
child 50929 18c8c18993f0
permissions -rw-r--r--
tests: fix http-bad-server expected errors for python 3.10 (issue6643) The format of the error message changed with this version of Python. This also removes obsolete Python 3 checks.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30652
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     1
# utility for color output for Mercurial commands
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     2
#
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     3
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> and other
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     4
#
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     7
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     8
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
     9
import re
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
    10
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
    11
from .i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    12
from .pycompat import getattr
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
    13
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    14
from . import (
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    15
    encoding,
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
    16
    pycompat,
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36502
diff changeset
    17
)
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36502
diff changeset
    18
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
    19
from .utils import stringutil
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
    20
30968
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    21
try:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    22
    import curses
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
    23
30968
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    24
    # Mapping from effect name to terminfo attribute name (or raw code) or
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    25
    # color number.  This will also force-load the curses module.
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
    26
    _baseterminfoparams = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    27
        b'none': (True, b'sgr0', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
        b'standout': (True, b'smso', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
        b'underline': (True, b'smul', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    30
        b'reverse': (True, b'rev', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    31
        b'inverse': (True, b'rev', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    32
        b'blink': (True, b'blink', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
        b'dim': (True, b'dim', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
        b'bold': (True, b'bold', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
        b'invisible': (True, b'invis', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
        b'italic': (True, b'sitm', b''),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    37
        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
    38
        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
    39
        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
    40
        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
    41
        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
    42
        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
    43
        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
    44
        b'white': (False, curses.COLOR_WHITE, b''),
31109
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
    45
    }
44264
d3f776c4760e py3: catch AttributeError too with ImportError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 43805
diff changeset
    46
except (ImportError, AttributeError):
30968
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    47
    curses = None
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
    48
    _baseterminfoparams = {}
30968
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
    49
30967
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    50
# start and stop parameters for effects
31109
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
    51
_effects = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    52
    b'none': 0,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    53
    b'black': 30,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
    b'red': 31,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
    b'green': 32,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    56
    b'yellow': 33,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    57
    b'blue': 34,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    58
    b'magenta': 35,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    59
    b'cyan': 36,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    60
    b'white': 37,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    61
    b'bold': 1,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    62
    b'italic': 3,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
    b'underline': 4,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    64
    b'inverse': 7,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    65
    b'dim': 2,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    66
    b'black_background': 40,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    67
    b'red_background': 41,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    68
    b'green_background': 42,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    69
    b'yellow_background': 43,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
    b'blue_background': 44,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
    b'purple_background': 45,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
    b'cyan_background': 46,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    73
    b'white_background': 47,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
    74
}
30967
20990991d384 color: move '_effect' mapping into core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30653
diff changeset
    75
31116
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31115
diff changeset
    76
_defaultstyles = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    77
    b'grep.match': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    78
    b'grep.linenumber': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
    b'grep.rev': b'blue',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    80
    b'grep.sep': b'cyan',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
    b'grep.filename': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    82
    b'grep.user': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    83
    b'grep.date': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    84
    b'grep.inserted': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
    b'grep.deleted': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    86
    b'bookmarks.active': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    87
    b'branches.active': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    88
    b'branches.closed': b'black bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    89
    b'branches.current': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    90
    b'branches.inactive': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    91
    b'diff.changed': b'white',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    92
    b'diff.deleted': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    93
    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
    94
    b'diff.deleted.unchanged': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
    b'diff.diffline': b'bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    96
    b'diff.extended': b'cyan bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    97
    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
    98
    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
    99
    b'diff.hunk': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
    b'diff.inserted': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   101
    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
   102
    b'diff.inserted.unchanged': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   103
    b'diff.tab': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   104
    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
   105
    b'changeset.public': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   106
    b'changeset.draft': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
    b'changeset.secret': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   108
    b'diffstat.deleted': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   109
    b'diffstat.inserted': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   110
    b'formatvariant.name.mismatchconfig': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   111
    b'formatvariant.name.mismatchdefault': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   112
    b'formatvariant.name.uptodate': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   113
    b'formatvariant.repo.mismatchconfig': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
    b'formatvariant.repo.mismatchdefault': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   115
    b'formatvariant.repo.uptodate': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   116
    b'formatvariant.config.special': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   117
    b'formatvariant.config.default': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   118
    b'formatvariant.default': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   119
    b'histedit.remaining': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   120
    b'ui.addremove.added': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   121
    b'ui.addremove.removed': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   122
    b'ui.error': b'red',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   123
    b'ui.prompt': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   124
    b'log.changeset': b'yellow',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   125
    b'patchbomb.finalsummary': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
    b'patchbomb.from': b'magenta',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   127
    b'patchbomb.to': b'cyan',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   128
    b'patchbomb.subject': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   129
    b'patchbomb.diffstats': b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   130
    b'rebase.rebased': b'blue',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   131
    b'rebase.remaining': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   132
    b'resolve.resolved': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   133
    b'resolve.unresolved': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   134
    b'shelve.age': b'cyan',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   135
    b'shelve.newest': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   136
    b'shelve.name': b'blue bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   137
    b'status.added': b'green bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   138
    b'status.clean': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   139
    b'status.copied': b'none',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   140
    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
   141
    b'status.ignored': b'black bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   142
    b'status.modified': b'blue bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   143
    b'status.removed': b'red bold',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   144
    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
   145
    b'tags.normal': b'green',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   146
    b'tags.local': b'black bold',
43805
ad84fc97d120 upgrade-repo: colorize some of the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43787
diff changeset
   147
    b'upgrade-repo.requirement.preserved': b'cyan',
ad84fc97d120 upgrade-repo: colorize some of the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43787
diff changeset
   148
    b'upgrade-repo.requirement.added': b'green',
ad84fc97d120 upgrade-repo: colorize some of the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43787
diff changeset
   149
    b'upgrade-repo.requirement.removed': b'red',
31109
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
   150
}
30653
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
   151
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   152
30653
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
   153
def loadcolortable(ui, extname, colortable):
31116
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31115
diff changeset
   154
    _defaultstyles.update(colortable)
30969
ddc80d1777a6 color: move 'valideffect' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30968
diff changeset
   155
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   156
33680
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   157
def _terminfosetup(ui, mode, formatted):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   158
    '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   159
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   160
    # If we failed to load curses, we go ahead and return.
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   161
    if curses is None:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   162
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   163
    # 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
   164
    if mode not in (b'auto', b'terminfo'):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   165
        return
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   166
    ui._terminfoparams.update(_baseterminfoparams)
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   167
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   168
    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
   169
        if key.startswith(b'color.'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   170
            newval = (False, int(val), b'')
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   171
            ui._terminfoparams[key[6:]] = newval
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   172
        elif key.startswith(b'terminfo.'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   173
            newval = (True, b'', val.replace(b'\\E', b'\x1b'))
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   174
            ui._terminfoparams[key[9:]] = newval
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   175
    try:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   176
        curses.setupterm()
41365
876494fd967d cleanup: delete lots of unused local variables
Martin von Zweigbergk <martinvonz@google.com>
parents: 40970
diff changeset
   177
    except curses.error:
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   178
        ui._terminfoparams.clear()
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   179
        return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   180
37662
5340daa85c62 py3: iterate over a copy of dict while changing it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37084
diff changeset
   181
    for key, (b, e, c) in ui._terminfoparams.copy().items():
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   182
        if not b:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   183
            continue
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   184
        if not c and not curses.tigetstr(pycompat.sysstr(e)):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   185
            # Most terminals don't support dim, invis, etc, so don't be
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   186
            # noisy and use ui.debug().
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   187
            ui.debug(b"no terminfo entry for %s\n" % e)
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   188
            del ui._terminfoparams[key]
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   189
    if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   190
        # Only warn about missing terminfo entries if we explicitly asked for
33680
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   191
        # 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
   192
        if mode == b"terminfo" and formatted:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   193
            ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   194
                _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   195
                    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
   196
                    b"ECMA-48 color\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   197
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   198
            )
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   199
        ui._terminfoparams.clear()
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
   200
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   201
31110
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31109
diff changeset
   202
def setup(ui):
31105
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31103
diff changeset
   203
    """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: 31103
diff changeset
   204
31110
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31109
diff changeset
   205
    That function both set the colormode for the ui object and read
31105
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31103
diff changeset
   206
    the configuration looking for custom colors and effect definitions."""
31110
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31109
diff changeset
   207
    mode = _modesetup(ui)
31106
a185b903bda3 color: have the 'ui' object carry the '_colormode' directly
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31105
diff changeset
   208
    ui._colormode = mode
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   209
    if mode and mode != b'debug':
31105
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31103
diff changeset
   210
        configstyles(ui)
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31103
diff changeset
   211
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   212
31110
7fec37746417 color: add a 'ui.color' option to control color behavior
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31109
diff changeset
   213
def _modesetup(ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   214
    if ui.plain(b'color'):
31103
c1997c5d1ae3 color: handle 'ui.plain()' directly in mode setup
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31101
diff changeset
   215
        return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   216
    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
   217
    if config == b'debug':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   218
        return b'debug'
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   219
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   220
    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
   221
    always = False
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36502
diff changeset
   222
    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
   223
        # 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
   224
        # 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
   225
        if (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   226
            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
   227
            or config == b'always'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   228
        ):
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
   229
            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
   230
        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
   231
            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
   232
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   233
    if not always and not auto:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   234
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   235
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   236
    formatted = always or (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   237
        encoding.environ.get(b'TERM') != b'dumb' and ui.formatted()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   238
    )
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   239
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   240
    mode = ui.config(b'color', b'mode')
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   241
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   242
    # If pager is active, color.pagermode overrides color.mode.
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   243
    if getattr(ui, 'pageractive', False):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   244
        mode = ui.config(b'color', b'pagermode', mode)
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   245
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   246
    realmode = mode
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
   247
    if pycompat.iswindows:
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
   248
        from . import win32
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
   249
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   250
        if mode == b'auto':
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
   251
            # 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: 32291
diff changeset
   252
            # 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: 32291
diff changeset
   253
            # we almost certainly don't support "win32", so don't even try.
40970
7654291091cf color: fix a documentation typo
Matt Harbison <matt_harbison@yahoo.com>
parents: 40522
diff changeset
   254
            # w32effects is not populated when stdout is redirected, so checking
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
   255
            # it first avoids win32 calls in a state known to error out.
48736
fd2cf9e0c64e color: don't infer vt status from TERM on Windows
Mitchell Hentges <mhentges@mozilla.com>
parents: 44264
diff changeset
   256
            if not w32effects or win32.enablevtmode():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   257
                realmode = b'ansi'
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   258
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   259
                realmode = b'win32'
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
   260
        # 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: 32291
diff changeset
   261
        # cannot enable VT mode.
48736
fd2cf9e0c64e color: don't infer vt status from TERM on Windows
Mitchell Hentges <mhentges@mozilla.com>
parents: 44264
diff changeset
   262
        elif mode == b'ansi' and w32effects:
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
   263
            win32.enablevtmode()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   264
    elif mode == b'auto':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   265
        realmode = b'ansi'
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   266
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   267
    def modewarn():
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   268
        # only warn if color.mode was explicitly set and we're in
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   269
        # a formatted terminal
33680
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   270
        if mode == realmode and formatted:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   271
            ui.warn(_(b'warning: failed to set color mode to %s\n') % mode)
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   272
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   273
    if realmode == b'win32':
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   274
        ui._terminfoparams.clear()
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   275
        if not w32effects:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   276
            modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   277
            return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   278
    elif realmode == b'ansi':
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   279
        ui._terminfoparams.clear()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   280
    elif realmode == b'terminfo':
33680
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
   281
        _terminfosetup(ui, mode, formatted)
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   282
        if not ui._terminfoparams:
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   283
            ## FIXME Shouldn't we return None in this case too?
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   284
            modewarn()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   285
            realmode = b'ansi'
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   286
    else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   287
        return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   288
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   289
    if always or (auto and formatted):
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   290
        return realmode
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   291
    return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
   292
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   293
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   294
def configstyles(ui):
31116
6483e49204ee color: rename '_styles' to '_defaultstyles' for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31115
diff changeset
   295
    ui._styles.update(_defaultstyles)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   296
    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
   297
        if b'.' not in status or status.startswith((b'color.', b'terminfo.')):
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   298
            continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   299
        cfgeffects = ui.configlist(b'color', status)
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   300
        if cfgeffects:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   301
            good = []
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   302
            for e in cfgeffects:
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
   303
                if valideffect(ui, e):
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   304
                    good.append(e)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   305
                else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   306
                    ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   307
                        _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   308
                            b"ignoring unknown color/effect %s "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   309
                            b"(configured in color.%s)\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   310
                        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   311
                        % (stringutil.pprint(e), status)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   312
                    )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   313
            ui._styles[status] = b' '.join(good)
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
   314
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   315
31689
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   316
def _activeeffects(ui):
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   317
    '''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
   318
    if ui._colormode == b'win32':
31689
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   319
        return w32effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   320
    elif ui._colormode is not None:
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   321
        return _effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   322
    return {}
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   323
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   324
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
   325
def valideffect(ui, effect):
43787
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43506
diff changeset
   326
    """Determine if the effect is valid or not."""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   327
    return (not ui._terminfoparams and effect in _activeeffects(ui)) or (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   328
        effect in ui._terminfoparams or effect[:-11] in ui._terminfoparams
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   329
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   330
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   331
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
   332
def _effect_str(ui, effect):
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   333
    '''Helper function for render_effects().'''
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   334
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   335
    bg = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   336
    if effect.endswith(b'_background'):
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   337
        bg = True
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   338
        effect = effect[:-11]
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   339
    try:
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   340
        attr, val, termcode = ui._terminfoparams[effect]
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   341
    except KeyError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   342
        return b''
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   343
    if attr:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   344
        if termcode:
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   345
            return termcode
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   346
        else:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
   347
            return curses.tigetstr(pycompat.sysstr(val))
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   348
    elif bg:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   349
        return curses.tparm(curses.tigetstr('setab'), val)
30972
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
   350
    else:
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   351
        return curses.tparm(curses.tigetstr('setaf'), val)
30973
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   352
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   353
31518
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   354
def _mergeeffects(text, start, stop):
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   355
    """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: 31499
diff changeset
   356
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33680
diff changeset
   357
    >>> s = _mergeeffects(b'cyan', b'[C]', b'|')
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33680
diff changeset
   358
    >>> 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: 33680
diff changeset
   359
    >>> 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: 33680
diff changeset
   360
    >>> s = _mergeeffects(b'red' + s, b'[R]', b'|')
31518
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   361
    >>> s
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   362
    '[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: 31499
diff changeset
   363
    """
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   364
    parts = []
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   365
    for t in text.split(stop):
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   366
        if not t:
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   367
            continue
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   368
        parts.extend([start, t, stop])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   369
    return b''.join(parts)
31518
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   370
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   371
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
   372
def _render_effects(ui, text, effects):
43787
be8552f25cab cleanup: fix docstring formatting
Matt Harbison <matt_harbison@yahoo.com>
parents: 43506
diff changeset
   373
    """Wrap text in commands to turn on each effect."""
30973
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   374
    if not text:
e5363cb96233 color: move the '_render_effects' function to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30972
diff changeset
   375
        return text
31113
268caf97c38f color: move the dict with terminfo parameters on the ui object
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31112
diff changeset
   376
    if ui._terminfoparams:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   377
        start = b''.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   378
            _effect_str(ui, effect) for effect in [b'none'] + effects.split()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   379
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   380
        stop = _effect_str(ui, b'none')
31071
350d737e059d color: minor reversal of two conditional clause for clarity
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31067
diff changeset
   381
    else:
31689
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
   382
        activeeffects = _activeeffects(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   383
        start = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   384
            pycompat.bytestr(activeeffects[e])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   385
            for e in [b'none'] + effects.split()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   386
        ]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   387
        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
   388
        stop = b'\033[' + pycompat.bytestr(activeeffects[b'none']) + b'm'
31518
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
   389
    return _mergeeffects(text, start, stop)
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   390
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   391
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
   392
_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: 31518
diff changeset
   393
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   394
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
   395
def stripeffects(text):
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
   396
    """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
   397
    return _ansieffectre.sub(b'', text)
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
   398
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   399
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   400
def colorlabel(ui, msg, label):
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   401
    """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
   402
    if ui._colormode == b'debug':
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   403
        if label and msg:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   404
            if msg.endswith(b'\n'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   405
                msg = b"[%s|%s]\n" % (label, msg[:-1])
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   406
            else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   407
                msg = b"[%s|%s]" % (label, msg)
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   408
    elif ui._colormode is not None:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   409
        effects = []
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   410
        for l in label.split():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   411
            s = ui._styles.get(l, b'')
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   412
            if s:
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   413
                effects.append(s)
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
   414
            elif valideffect(ui, l):
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   415
                effects.append(l)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   416
        effects = b' '.join(effects)
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   417
        if effects:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   418
            msg = b'\n'.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   419
                [
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   420
                    _render_effects(ui, line, effects)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   421
                    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
   422
                ]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   423
            )
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   424
    return msg
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
   425
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   426
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   427
w32effects = None
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
   428
if pycompat.iswindows:
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   429
    import ctypes
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   430
43476
949b4d545c90 color: suppress pytype warning on a windows-only module
Augie Fackler <augie@google.com>
parents: 43089
diff changeset
   431
    _kernel32 = ctypes.windll.kernel32  # pytype: disable=module-attr
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   432
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   433
    _WORD = ctypes.c_ushort
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   434
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   435
    _INVALID_HANDLE_VALUE = -1
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   436
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   437
    class _COORD(ctypes.Structure):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   438
        _fields_ = [('X', ctypes.c_short), ('Y', ctypes.c_short)]
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   439
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   440
    class _SMALL_RECT(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   441
        _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   442
            ('Left', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   443
            ('Top', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   444
            ('Right', ctypes.c_short),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   445
            ('Bottom', ctypes.c_short),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   446
        ]
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   447
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   448
    class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   449
        _fields_ = [
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   450
            ('dwSize', _COORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   451
            ('dwCursorPosition', _COORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   452
            ('wAttributes', _WORD),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   453
            ('srWindow', _SMALL_RECT),
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   454
            ('dwMaximumWindowSize', _COORD),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   455
        ]
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   456
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   457
    _STD_OUTPUT_HANDLE = 0xFFFFFFF5  # (DWORD)-11
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   458
    _STD_ERROR_HANDLE = 0xFFFFFFF4  # (DWORD)-12
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   459
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   460
    _FOREGROUND_BLUE = 0x0001
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   461
    _FOREGROUND_GREEN = 0x0002
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   462
    _FOREGROUND_RED = 0x0004
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   463
    _FOREGROUND_INTENSITY = 0x0008
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   464
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   465
    _BACKGROUND_BLUE = 0x0010
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   466
    _BACKGROUND_GREEN = 0x0020
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   467
    _BACKGROUND_RED = 0x0040
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   468
    _BACKGROUND_INTENSITY = 0x0080
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   469
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   470
    _COMMON_LVB_REVERSE_VIDEO = 0x4000
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   471
    _COMMON_LVB_UNDERSCORE = 0x8000
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   472
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   473
    # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   474
    w32effects = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   475
        b'none': -1,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   476
        b'black': 0,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   477
        b'red': _FOREGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   478
        b'green': _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   479
        b'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   480
        b'blue': _FOREGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   481
        b'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   482
        b'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   483
        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
   484
        b'bold': _FOREGROUND_INTENSITY,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   485
        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
   486
        b'red_background': _BACKGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   487
        b'green_background': _BACKGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   488
        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
   489
        b'blue_background': _BACKGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   490
        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
   491
        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
   492
        b'white_background': (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   493
            _BACKGROUND_RED | _BACKGROUND_GREEN | _BACKGROUND_BLUE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   494
        ),
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   495
        b'bold_background': _BACKGROUND_INTENSITY,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   496
        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
   497
        b'inverse': _COMMON_LVB_REVERSE_VIDEO,  # double-byte charsets only
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   498
    }
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   499
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   500
    passthrough = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   501
        _FOREGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   502
        _BACKGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   503
        _COMMON_LVB_UNDERSCORE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   504
        _COMMON_LVB_REVERSE_VIDEO,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   505
    }
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   506
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   507
    stdout = _kernel32.GetStdHandle(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   508
        _STD_OUTPUT_HANDLE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   509
    )  # don't close the handle returned
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   510
    if stdout is None or stdout == _INVALID_HANDLE_VALUE:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   511
        w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   512
    else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   513
        csbi = _CONSOLE_SCREEN_BUFFER_INFO()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   514
        if not _kernel32.GetConsoleScreenBufferInfo(stdout, ctypes.byref(csbi)):
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   515
            # stdout may not support GetConsoleScreenBufferInfo()
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   516
            # when called from subprocess or redirected
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   517
            w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   518
        else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   519
            origattr = csbi.wAttributes
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   520
            ansire = re.compile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   521
                br'\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   522
            )
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   523
40522
51091816a355 ui: simply concatenate messages before applying color labels
Yuya Nishihara <yuya@tcha.org>
parents: 40521
diff changeset
   524
    def win32print(ui, writefunc, text, **opts):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43476
diff changeset
   525
        label = opts.get('label', b'')
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   526
        attr = origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   527
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   528
        def mapcolor(val, attr):
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   529
            if val == -1:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   530
                return origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   531
            elif val in passthrough:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   532
                return attr | val
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   533
            elif val > 0x0F:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   534
                return (val & 0x70) | (attr & 0x8F)
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   535
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
   536
                return (val & 0x07) | (attr & 0xF8)
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   537
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   538
        # determine console attributes based on labels
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   539
        for l in label.split():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   540
            style = ui._styles.get(l, b'')
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   541
            for effect in style.split():
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   542
                try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   543
                    attr = mapcolor(w32effects[effect], attr)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   544
                except KeyError:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   545
                    # w32effects could not have certain attributes so we skip
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   546
                    # them if not found
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   547
                    pass
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   548
        # hack to ensure regexp finds data
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39091
diff changeset
   549
        if not text.startswith(b'\033['):
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39091
diff changeset
   550
            text = b'\033[m' + text
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   551
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   552
        # Look for ANSI-like codes embedded in text
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   553
        m = re.match(ansire, text)
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   554
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   555
        try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   556
            while m:
39644
3b421154d2ca py3: fix str vs bytes in enough places to run `hg version` on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 39091
diff changeset
   557
                for sattr in m.group(1).split(b';'):
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   558
                    if sattr:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   559
                        attr = mapcolor(int(sattr), attr)
31499
31d2ddfd338c color: sync text attributes and buffered text output on Windows (issue5508)
Matt Harbison <matt_harbison@yahoo.com>
parents: 31123
diff changeset
   560
                ui.flush()
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   561
                _kernel32.SetConsoleTextAttribute(stdout, attr)
40521
49746e53ac92 ui: simplify interface of low-level write() functions
Yuya Nishihara <yuya@tcha.org>
parents: 40367
diff changeset
   562
                writefunc(m.group(2))
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   563
                m = re.match(ansire, m.group(3))
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   564
        finally:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   565
            # Explicitly reset original attributes
31499
31d2ddfd338c color: sync text attributes and buffered text output on Windows (issue5508)
Matt Harbison <matt_harbison@yahoo.com>
parents: 31123
diff changeset
   566
            ui.flush()
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
   567
            _kernel32.SetConsoleTextAttribute(stdout, origattr)