annotate mercurial/color.py @ 43077:687b865b95ad

formatting: byteify all mercurial/ and hgext/ string literals Done with python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py') black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**') # skip-blame mass-reformatting only Differential Revision: https://phab.mercurial-scm.org/D6972
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:48:39 -0400
parents 2372284d9457
children c59eb1560c44
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30652
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
1 # utility for color output for Mercurial commands
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
2 #
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
3 # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> and other
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
4 #
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
5 # This software may be used and distributed according to the terms of the
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
6 # GNU General Public License version 2 or any later version.
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
7
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
8 from __future__ import absolute_import
1ec42bdd7874 color: move hgext.color._styles to mercurial.color.style
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
9
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
10 import re
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
11
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
12 from .i18n import _
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 }
30968
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30967
diff changeset
46 except ImportError:
0d2a58a04080 color: move '_terminfo_params' into the core 'color' module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 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',
31109
53230c5bb273 color: reinvent dictionary
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31106
diff changeset
147 }
30653
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
148
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
149
30653
b2be4ccaff1d color: load 'colortable' from extension using an 'extraloader'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30652
diff changeset
150 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
151 _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
152
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
153
33680
f9f28ee41cac color: remove warnings if term is not formatted (==dumb or !ui.formatted())
Kyle Lippincott <spectral@google.com>
parents: 33522
diff changeset
154 def _terminfosetup(ui, mode, formatted):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
155 '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
156
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
157 # If we failed to load curses, we go ahead and return.
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
158 if curses is None:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
159 return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
160 # 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
161 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
162 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
163 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
164
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
165 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
166 if key.startswith(b'color.'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
167 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
168 ui._terminfoparams[key[6:]] = newval
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
169 elif key.startswith(b'terminfo.'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
170 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
171 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
172 try:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
173 curses.setupterm()
41365
876494fd967d cleanup: delete lots of unused local variables
Martin von Zweigbergk <martinvonz@google.com>
parents: 40970
diff changeset
174 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
175 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
176 return
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
177
37662
5340daa85c62 py3: iterate over a copy of dict while changing it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37084
diff changeset
178 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
179 if not b:
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
180 continue
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
181 if not c and not curses.tigetstr(pycompat.sysstr(e)):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
182 # Most terminals don't support dim, invis, etc, so don't be
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
183 # noisy and use ui.debug().
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
184 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
185 del ui._terminfoparams[key]
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
186 if not curses.tigetstr(r'setaf') or not curses.tigetstr(r'setab'):
31100
8903f67b9ca8 color: move 'terminfosetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31089
diff changeset
187 # 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
188 # 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
189 if mode == b"terminfo" and formatted:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
190 ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
191 _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
192 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
193 b"ECMA-48 color\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
194 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
195 )
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
196 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
197
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
198
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
199 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
200 """configure color on a ui
45be7590301d color: move triggering of the initialisation logic in core
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31103
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 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
203 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
204 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
205 ui._colormode = mode
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
206 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
207 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
208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
209
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
210 def _modesetup(ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
211 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
212 return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
213 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
214 if config == b'debug':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
215 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
216
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
217 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
218 always = False
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36502
diff changeset
219 if not auto and stringutil.parsebool(config):
32103
9a98023ac8db color: special case 'always' in 'ui.color'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32102
diff changeset
220 # We want the config to behave like a boolean, "on" is actually auto,
9a98023ac8db color: special case 'always' in 'ui.color'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 32102
diff changeset
221 # but "always" value is treated as a special case to reduce confusion.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
222 if (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
223 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
224 or config == b'always'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
225 ):
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
226 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
227 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
228 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
229
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
230 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
231 return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
232
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
233 formatted = always or (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
234 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
235 )
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
236
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
237 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
238
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
239 # 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
240 if getattr(ui, 'pageractive', False):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
241 mode = ui.config(b'color', b'pagermode', mode)
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
242
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
243 realmode = mode
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
244 if pycompat.iswindows:
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
245 from . import win32
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
246
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
247 term = encoding.environ.get(b'TERM')
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
248 # TERM won't be defined in a vanilla cmd.exe environment.
31101
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
249
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
250 # UNIX-like environments on Windows such as Cygwin and MSYS will
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
251 # set TERM. They appear to make a best effort attempt at setting it
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
252 # to something appropriate. However, not all environments with TERM
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
253 # defined support ANSI.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
254 ansienviron = term and b'xterm' in term
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
255
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
256 if mode == b'auto':
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
257 # 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
258 # 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
259 # 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
260 # 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
261 # it first avoids win32 calls in a state known to error out.
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
262 if ansienviron or not w32effects or win32.enablevtmode():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
263 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
264 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
265 realmode = b'win32'
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
266 # 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
267 # cannot enable VT mode.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
268 elif mode == b'ansi' and w32effects and not ansienviron:
32665
98c2b44bdf9a color: enable ANSI support on Windows 10
Matt Harbison <matt_harbison@yahoo.com>
parents: 32291
diff changeset
269 win32.enablevtmode()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
270 elif mode == b'auto':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
271 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
272
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
273 def modewarn():
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
274 # 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
275 # 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
276 if mode == realmode and formatted:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
277 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
278
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
279 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
280 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
281 if not w32effects:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
282 modewarn()
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
283 return None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
284 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
285 ui._terminfoparams.clear()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
286 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
287 _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
288 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
289 ## 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
290 modewarn()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
291 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
292 else:
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
293 return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
294
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
295 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
296 return realmode
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
297 return None
9021a94a7dbf color: move 'modesetup' into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31100
diff changeset
298
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
299
30971
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
300 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
301 ui._styles.update(_defaultstyles)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
302 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
303 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
304 continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
305 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
306 if cfgeffects:
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
307 good = []
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
308 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
309 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
310 good.append(e)
bb6385882cfa color: move configstyles into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30970
diff changeset
311 else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
312 ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
313 _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
314 b"ignoring unknown color/effect %s "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
315 b"(configured in color.%s)\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
316 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
317 % (stringutil.pprint(e), status)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
318 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
319 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
320
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
321
31689
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
322 def _activeeffects(ui):
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
323 '''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
324 if ui._colormode == b'win32':
31689
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
325 return w32effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
326 elif ui._colormode is not None:
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
327 return _effects
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
328 return {}
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
329
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
330
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
331 def valideffect(ui, effect):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
332 b'Determine if the effect is valid or not.'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
333 return (not ui._terminfoparams and effect in _activeeffects(ui)) or (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
334 effect in ui._terminfoparams or effect[:-11] in ui._terminfoparams
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
335 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
336
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
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
338 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
339 '''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
340
a3c7e42c7a1f color: move '_effect_str' function into the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30971
diff changeset
341 bg = False
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
342 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
343 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
344 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
345 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
346 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
347 except KeyError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
348 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
349 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
350 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
351 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
352 else:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
353 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
354 elif bg:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
355 return curses.tparm(curses.tigetstr(r'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
356 else:
37664
483cafc3762a py3: make sure curses.tigetstr() first argument is a str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 37662
diff changeset
357 return curses.tparm(curses.tigetstr(r'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
358
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
359
31518
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
360 def _mergeeffects(text, start, stop):
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
361 """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
362
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33680
diff changeset
363 >>> 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
364 >>> 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
365 >>> 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
366 >>> 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
367 >>> s
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
368 '[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
369 """
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
370 parts = []
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
371 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
372 if not t:
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
373 continue
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
374 parts.extend([start, t, stop])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
375 return b''.join(parts)
31518
43d6ef658874 color: insert color code after every "\e[0m" (issue5413)
Yuya Nishihara <yuya@tcha.org>
parents: 31499
diff changeset
376
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
377
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
378 def _render_effects(ui, text, effects):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
379 b'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
380 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
381 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
382 if ui._terminfoparams:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
383 start = b''.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
384 _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
385 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
386 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
387 else:
31689
57a22f699179 color: stop mutating the default effects map
Matt Harbison <matt_harbison@yahoo.com>
parents: 31521
diff changeset
388 activeeffects = _activeeffects(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
389 start = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
390 pycompat.bytestr(activeeffects[e])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
391 for e in [b'none'] + effects.split()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
392 ]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
393 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
394 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
395 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
396
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
397
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
398 _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
399
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
400
31521
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
401 def stripeffects(text):
44c591f63458 templater: make pad() strip color codes before computing width (issue5416)
Yuya Nishihara <yuya@tcha.org>
parents: 31518
diff changeset
402 """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
403 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
404
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
405
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 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
407 """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
408 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
409 if label and msg:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
410 if msg.endswith(b'\n'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
411 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
412 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
413 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
414 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
415 effects = []
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
416 for l in label.split():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
417 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
418 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
419 effects.append(s)
31112
7f056fdbe37e color: add ui to effect rendering
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31110
diff changeset
420 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
421 effects.append(l)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
422 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
423 if effects:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
424 msg = b'\n'.join(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
425 [
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
426 _render_effects(ui, line, effects)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
427 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
428 ]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
429 )
31086
e6082078c853 color: move the 'colorlabel' function in the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31071
diff changeset
430 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
431
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
432
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
433 w32effects = None
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 34486
diff changeset
434 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
435 import ctypes
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 _kernel32 = ctypes.windll.kernel32
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
438
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
439 _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
440
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
441 _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
442
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
443 class _COORD(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
444 _fields_ = [(r'X', ctypes.c_short), (r'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
445
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
446 class _SMALL_RECT(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
447 _fields_ = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
448 (r'Left', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
449 (r'Top', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
450 (r'Right', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
451 (r'Bottom', ctypes.c_short),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
452 ]
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
453
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
454 class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
455 _fields_ = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
456 (r'dwSize', _COORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
457 (r'dwCursorPosition', _COORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
458 (r'wAttributes', _WORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
459 (r'srWindow', _SMALL_RECT),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
460 (r'dwMaximumWindowSize', _COORD),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
461 ]
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
462
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
463 _STD_OUTPUT_HANDLE = 0xFFFFFFF5 # (DWORD)-11
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
464 _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
465
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
466 _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
467 _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
468 _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
469 _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
470
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
471 _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
472 _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
473 _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
474 _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
475
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
476 _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
477 _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
478
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
479 # 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
480 w32effects = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
481 b'none': -1,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
482 b'black': 0,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
483 b'red': _FOREGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
484 b'green': _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
485 b'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
486 b'blue': _FOREGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
487 b'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
488 b'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
489 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
490 b'bold': _FOREGROUND_INTENSITY,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
491 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
492 b'red_background': _BACKGROUND_RED,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
493 b'green_background': _BACKGROUND_GREEN,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
494 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
495 b'blue_background': _BACKGROUND_BLUE,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
496 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
497 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
498 b'white_background': (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
499 _BACKGROUND_RED | _BACKGROUND_GREEN | _BACKGROUND_BLUE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
500 ),
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
501 b'bold_background': _BACKGROUND_INTENSITY,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
502 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
503 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
504 }
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
505
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
506 passthrough = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
507 _FOREGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
508 _BACKGROUND_INTENSITY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
509 _COMMON_LVB_UNDERSCORE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
510 _COMMON_LVB_REVERSE_VIDEO,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
511 }
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
512
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
513 stdout = _kernel32.GetStdHandle(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
514 _STD_OUTPUT_HANDLE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
515 ) # 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
516 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
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 csbi = _CONSOLE_SCREEN_BUFFER_INFO()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
520 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
521 # 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
522 # 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
523 w32effects = None
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
524 else:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
525 origattr = csbi.wAttributes
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
526 ansire = re.compile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
527 br'\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
528 )
31067
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
529
40522
51091816a355 ui: simply concatenate messages before applying color labels
Yuya Nishihara <yuya@tcha.org>
parents: 40521
diff changeset
530 def win32print(ui, writefunc, text, **opts):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
531 label = opts.get(r'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
532 attr = origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
533
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
534 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
535 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
536 return origattr
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
537 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
538 return attr | val
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
539 elif val > 0x0F:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
540 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
541 else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41741
diff changeset
542 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
543
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
544 # 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
545 for l in label.split():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
546 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
547 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
548 try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
549 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
550 except KeyError:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
551 # 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
552 # 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
553 pass
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
554 # 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
555 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
556 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
557
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
558 # 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
559 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
560
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
561 try:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
562 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
563 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
564 if sattr:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
565 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
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, attr)
40521
49746e53ac92 ui: simplify interface of low-level write() functions
Yuya Nishihara <yuya@tcha.org>
parents: 40367
diff changeset
568 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
569 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
570 finally:
a0bde5ec3a46 color: move 'win32' declaration to the core module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 30973
diff changeset
571 # 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
572 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
573 _kernel32.SetConsoleTextAttribute(stdout, origattr)