hgext/color.py
author Danek Duvall <danek.duvall@oracle.com>
Thu, 13 Oct 2016 12:01:41 -0700
changeset 30174 5d777fe4615d
parent 30173 f34a8cff51d9
child 30175 8915c68f3eba
permissions -rw-r--r--
color: ignore effects missing from terminfo If terminfo mode is in effect, and an effect is used which is missing from the terminfo database, simply silently ignore the request, leaving the output unaffected rather than causing a crash.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22771
d6274291fac1 color: update description of the extension
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22763
diff changeset
     1
# color.py color output for Mercurial commands
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     2
#
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     3
# Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com>
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     4
#
15771
425c1309718f color: Use the same GPL boilerplate as most hg files
Augie Fackler <durin42@gmail.com>
parents: 15048
diff changeset
     5
# This software may be used and distributed according to the terms of the
425c1309718f color: Use the same GPL boilerplate as most hg files
Augie Fackler <durin42@gmail.com>
parents: 15048
diff changeset
     6
# GNU General Public License version 2 or any later version.
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     7
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8866
diff changeset
     8
'''colorize output from some commands
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
     9
22771
d6274291fac1 color: update description of the extension
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22763
diff changeset
    10
The color extension colorizes output from several Mercurial commands.
d6274291fac1 color: update description of the extension
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22763
diff changeset
    11
For example, the diff command shows additions in green and deletions
d6274291fac1 color: update description of the extension
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22763
diff changeset
    12
in red, while the status command shows modified files in magenta. Many
d6274291fac1 color: update description of the extension
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22763
diff changeset
    13
other commands have analogous colors. It is possible to customize
d6274291fac1 color: update description of the extension
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22763
diff changeset
    14
these colors.
7457
a70fb83cbb9e diff colorization: finish highlighting trailing whitespace
Georg Brandl <georg@python.org>
parents: 7456
diff changeset
    15
22772
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    16
Effects
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    17
-------
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    18
7988
0447939b4b97 color: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7983
diff changeset
    19
Other effects in addition to color, like bold and underlined text, are
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    20
also available. By default, the terminfo database is used to find the
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    21
terminal codes used to change color and effect.  If terminfo is not
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
    22
available, then effects are rendered with the ECMA-48 SGR control
13635
c9ddc39c21b6 color: don't mention internal function in docstring
Martin Geisler <mg@aragost.com>
parents: 13361
diff changeset
    23
function (aka ANSI escape codes).
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    24
22772
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    25
The available effects in terminfo mode are 'blink', 'bold', 'dim',
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    26
'inverse', 'invisible', 'italic', 'standout', and 'underline'; in
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    27
ECMA-48 mode, the options are 'bold', 'inverse', 'italic', and
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    28
'underline'.  How each is rendered depends on the terminal emulator.
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    29
Some may not be available for a given terminal type, and will be
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    30
silently ignored.
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    31
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    32
Labels
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    33
------
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
    34
22462
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    35
Text receives color effects depending on the labels that it has. Many
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    36
default Mercurial commands emit labelled text. You can also define
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    37
your own labels in templates using the label function, see :hg:`help
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    38
templates`. A single portion of text may have more than one label. In
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    39
that case, effects given to the last label will override any other
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    40
effects. This includes the special "none" effect, which nullifies
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    41
other effects.
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    42
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
    43
Labels are normally invisible. In order to see these labels and their
22711
60ac52a21512 color: update docstring for debug option
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22710
diff changeset
    44
position in the text, use the global --color=debug option. The same
60ac52a21512 color: update docstring for debug option
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22710
diff changeset
    45
anchor text may be associated to multiple labels, e.g.
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
    46
22711
60ac52a21512 color: update docstring for debug option
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22710
diff changeset
    47
  [log.changeset changeset.secret|changeset:   22611:6f0a53c8f587]
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
    48
22462
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    49
The following are the default effects for some default labels. Default
fbd67cf34799 color: document that labels are used for colorizing text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21991
diff changeset
    50
effects may be overridden from your configuration file::
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    51
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    52
  [color]
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    53
  status.modified = blue bold underline red_background
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    54
  status.added = green bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    55
  status.removed = red bold blue_background
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    56
  status.deleted = cyan bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    57
  status.unknown = magenta bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    58
  status.ignored = black bold
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    59
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    60
  # 'none' turns off all effects
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    61
  status.clean = none
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    62
  status.copied = none
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
    63
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    64
  qseries.applied = blue bold underline
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    65
  qseries.unapplied = black bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    66
  qseries.missing = red bold
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
    67
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    68
  diff.diffline = bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    69
  diff.extended = cyan bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    70
  diff.file_a = red bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    71
  diff.file_b = green bold
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    72
  diff.hunk = magenta
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    73
  diff.deleted = red
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    74
  diff.inserted = green
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    75
  diff.changed = white
22710
b07fd3ac8882 color: document the possibility to colourise tabs in diffs
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22465
diff changeset
    76
  diff.tab =
9206
c1a1b49221e3 color: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9057
diff changeset
    77
  diff.trailingwhitespace = bold red_background
10046
0c23b0b3516b color: Add support for bookmarks
David Soria Parra <dsp@php.net>
parents: 10045
diff changeset
    78
22465
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
    79
  # Blank so it inherits the style of the surrounding label
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
    80
  changeset.public =
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
    81
  changeset.draft =
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
    82
  changeset.secret =
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
    83
10223
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    84
  resolve.unresolved = red bold
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    85
  resolve.resolved = green bold
51421ab573de color: colorize output of hg resolve -l
Georg Brandl <georg@python.org>
parents: 10222
diff changeset
    86
25347
ceec79b5657a bookmarks: name label for active bookmark correctly
Ryan McElroy <rmcelroy@fb.com>
parents: 25186
diff changeset
    87
  bookmarks.active = green
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
    88
11969
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    89
  branches.active = none
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    90
  branches.closed = black bold
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    91
  branches.current = green
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    92
  branches.inactive = none
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
    93
15048
2f0a3977c939 color: add styles for tags
Marc Simpson <marc@0branch.com>
parents: 14989
diff changeset
    94
  tags.normal = green
2f0a3977c939 color: add styles for tags
Marc Simpson <marc@0branch.com>
parents: 14989
diff changeset
    95
  tags.local = black bold
2f0a3977c939 color: add styles for tags
Marc Simpson <marc@0branch.com>
parents: 14989
diff changeset
    96
19214
0250047a365e summary: indicate if a rebase is underway
Bryan O'Sullivan <bryano@fb.com>
parents: 19073
diff changeset
    97
  rebase.rebased = blue
0250047a365e summary: indicate if a rebase is underway
Bryan O'Sullivan <bryano@fb.com>
parents: 19073
diff changeset
    98
  rebase.remaining = red bold
0250047a365e summary: indicate if a rebase is underway
Bryan O'Sullivan <bryano@fb.com>
parents: 19073
diff changeset
    99
19854
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   100
  shelve.age = cyan
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   101
  shelve.newest = green bold
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   102
  shelve.name = blue bold
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   103
19215
f184fe1e2ac5 summary: add a histedit hook
Bryan O'Sullivan <bryano@fb.com>
parents: 19214
diff changeset
   104
  histedit.remaining = red bold
f184fe1e2ac5 summary: add a histedit hook
Bryan O'Sullivan <bryano@fb.com>
parents: 19214
diff changeset
   105
22772
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   106
Custom colors
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   107
-------------
14769
9adce4b38ed1 color: for the sake of "less -R", default to ansi in auto mode (issue2792)
Brodie Rao <brodie@bitheap.org>
parents: 14768
diff changeset
   108
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   109
Because there are only eight standard colors, this module allows you
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   110
to define color names for other color slots which might be available
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   111
for your terminal type, assuming terminfo mode.  For instance::
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   112
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   113
  color.brightblue = 12
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   114
  color.pink = 207
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   115
  color.orange = 202
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   116
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   117
to set 'brightblue' to color slot 12 (useful for 16 color terminals
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   118
that have brighter colors defined in the upper eight) and, 'pink' and
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   119
'orange' to colors in 256-color xterm's default color cube.  These
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   120
defined colors may then be used as any of the pre-defined eight,
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   121
including appending '_background' to set the background to that color.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   122
22772
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   123
Modes
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   124
-----
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   125
14769
9adce4b38ed1 color: for the sake of "less -R", default to ansi in auto mode (issue2792)
Brodie Rao <brodie@bitheap.org>
parents: 14768
diff changeset
   126
By default, the color extension will use ANSI mode (or win32 mode on
9adce4b38ed1 color: for the sake of "less -R", default to ansi in auto mode (issue2792)
Brodie Rao <brodie@bitheap.org>
parents: 14768
diff changeset
   127
Windows) if it detects a terminal. To override auto mode (to enable
9adce4b38ed1 color: for the sake of "less -R", default to ansi in auto mode (issue2792)
Brodie Rao <brodie@bitheap.org>
parents: 14768
diff changeset
   128
terminfo mode, for example), set the following configuration option::
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   129
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   130
  [color]
14769
9adce4b38ed1 color: for the sake of "less -R", default to ansi in auto mode (issue2792)
Brodie Rao <brodie@bitheap.org>
parents: 14768
diff changeset
   131
  mode = terminfo
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   132
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   133
Any value other than 'ansi', 'win32', 'terminfo', or 'auto' will
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   134
disable color.
22772
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   135
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   136
Note that on some systems, terminfo mode may cause problems when using
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   137
color with the pager extension and less -R. less with the -R option
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   138
will only display ECMA-48 color codes, and terminfo mode may sometimes
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   139
emit codes that less doesn't understand. You can work around this by
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   140
either using ansi mode (or auto mode), or by using less -r (which will
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   141
pass through all terminal control codes, not just color control
b186b3ef4b54 color: reorganise and sectionify the help text
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22771
diff changeset
   142
codes).
24068
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   143
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   144
On some systems (such as MSYS in Windows), the terminal may support
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   145
a different color mode than the pager (activated via the "pager"
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   146
extension). It is possible to define separate modes depending on whether
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   147
the pager is active::
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   148
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   149
  [color]
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   150
  mode = auto
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   151
  pagermode = ansi
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   152
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   153
If ``pagermode`` is not defined, the ``mode`` will be used.
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   154
'''
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   155
28968
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   156
from __future__ import absolute_import
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   157
28968
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   158
import os
29205
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 28968
diff changeset
   159
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 28968
diff changeset
   160
from mercurial.i18n import _
28968
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   161
from mercurial import (
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   162
    cmdutil,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   163
    commands,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   164
    dispatch,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   165
    extensions,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   166
    subrepo,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   167
    ui as uimod,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   168
    util,
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   169
)
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   170
21249
2b8697e20978 color: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21227
diff changeset
   171
cmdtable = {}
2b8697e20978 color: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21227
diff changeset
   172
command = cmdutil.command(cmdtable)
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29205
diff changeset
   173
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24848
diff changeset
   174
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24848
diff changeset
   175
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24848
diff changeset
   176
# leave the attribute unspecified.
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29205
diff changeset
   177
testedwith = 'ships-with-hg-core'
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 15771
diff changeset
   178
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   179
# start and stop parameters for effects
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   180
_effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   181
            'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1,
23925
e563e0cfe08c color: add missing 'dim' in _effects
Sean Farley <sean.michael.farley@gmail.com>
parents: 23780
diff changeset
   182
            'italic': 3, 'underline': 4, 'inverse': 7, 'dim': 2,
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   183
            'black_background': 40, 'red_background': 41,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   184
            'green_background': 42, 'yellow_background': 43,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   185
            'blue_background': 44, 'purple_background': 45,
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   186
            'cyan_background': 46, 'white_background': 47}
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   187
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   188
def _terminfosetup(ui, mode):
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   189
    '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   190
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   191
    global _terminfo_params
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   192
    # If we failed to load curses, we go ahead and return.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   193
    if not _terminfo_params:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   194
        return
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   195
    # Otherwise, see what the config file says.
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   196
    if mode not in ('auto', 'terminfo'):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   197
        return
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   198
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   199
    _terminfo_params.update((key[6:], (False, int(val), ''))
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   200
        for key, val in ui.configitems('color')
13998
14c7526fed89 color: code simplification
Patrick Mezard <pmezard@gmail.com>
parents: 13987
diff changeset
   201
        if key.startswith('color.'))
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   202
    _terminfo_params.update((key[9:], (True, '', val.replace('\\E', '\x1b')))
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   203
        for key, val in ui.configitems('color')
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   204
        if key.startswith('terminfo.'))
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   205
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   206
    try:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   207
        curses.setupterm()
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25597
diff changeset
   208
    except curses.error as e:
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   209
        _terminfo_params = {}
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   210
        return
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   211
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   212
    for key, (b, e, c) in _terminfo_params.items():
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   213
        if not b:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   214
            continue
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   215
        if not c and not curses.tigetstr(e):
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   216
            # Most terminals don't support dim, invis, etc, so don't be
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   217
            # noisy and use ui.debug().
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   218
            ui.debug("no terminfo entry for %s\n" % e)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   219
            del _terminfo_params[key]
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   220
    if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
14758
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   221
        # Only warn about missing terminfo entries if we explicitly asked for
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   222
        # terminfo mode.
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   223
        if mode == "terminfo":
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   224
            ui.warn(_("no terminfo entry for setab/setaf: reverting to "
1e6661e09818 color: be silent when falling back to ECMA-48 mode if "auto" mode was desired
Danek Duvall <duvall@comfychair.org>
parents: 14516
diff changeset
   225
              "ECMA-48 color\n"))
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   226
        _terminfo_params = {}
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   227
19297
8ddc3cd962ba color: only provide the required opt to _modesetup
Simon Heimberg <simohe@besonet.ch>
parents: 19215
diff changeset
   228
def _modesetup(ui, coloropt):
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   229
    global _terminfo_params
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   230
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   231
    if coloropt == 'debug':
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   232
        return 'debug'
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   233
21723
35eb333d61d5 color: add parenthesis to help readability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21249
diff changeset
   234
    auto = (coloropt == 'auto')
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   235
    always = not auto and util.parsebool(coloropt)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   236
    if not always and not auto:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   237
        return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   238
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   239
    formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted())
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   240
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   241
    mode = ui.config('color', 'mode', 'auto')
24068
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   242
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   243
    # If pager is active, color.pagermode overrides color.mode.
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   244
    if getattr(ui, 'pageractive', False):
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   245
        mode = ui.config('color', 'pagermode', mode)
4e02418b4236 color: support a different color mode when the pager is active
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24028
diff changeset
   246
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   247
    realmode = mode
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   248
    if mode == 'auto':
24028
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   249
        if os.name == 'nt':
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   250
            term = os.environ.get('TERM')
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   251
            # TERM won't be defined in a vanilla cmd.exe environment.
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   252
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   253
            # UNIX-like environments on Windows such as Cygwin and MSYS will
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   254
            # set TERM. They appear to make a best effort attempt at setting it
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   255
            # to something appropriate. However, not all environments with TERM
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   256
            # defined support ANSI. Since "ansi" could result in terminal
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   257
            # gibberish, we error on the side of selecting "win32". However, if
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   258
            # w32effects is not defined, we almost certainly don't support
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   259
            # "win32", so don't even try.
24535
1818d4dca75c color: fix crash in cmd.exe
Matt Harbison <matt_harbison@yahoo.com>
parents: 24266
diff changeset
   260
            if (term and 'xterm' in term) or not w32effects:
24028
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   261
                realmode = 'ansi'
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   262
            else:
a78888d98606 color: be more conservative about setting ANSI mode on Windows (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23925
diff changeset
   263
                realmode = 'win32'
14769
9adce4b38ed1 color: for the sake of "less -R", default to ansi in auto mode (issue2792)
Brodie Rao <brodie@bitheap.org>
parents: 14768
diff changeset
   264
        else:
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   265
            realmode = 'ansi'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   266
24266
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   267
    def modewarn():
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   268
        # only warn if color.mode was explicitly set and we're in
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   269
        # an interactive terminal
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   270
        if mode == realmode and ui.interactive():
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   271
            ui.warn(_('warning: failed to set color mode to %s\n') % mode)
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   272
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   273
    if realmode == 'win32':
14989
9e9d4a762586 color.py - clear _terminfo_params in win32 mode
Andrei Vermel <avermel@mail.ru>
parents: 14769
diff changeset
   274
        _terminfo_params = {}
14768
55db12e54450 color: fix TypeError with auto mode on win32 when colors aren't available (issue2871)
Brodie Rao <brodie@bitheap.org>
parents: 14758
diff changeset
   275
        if not w32effects:
24266
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   276
            modewarn()
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   277
            return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   278
        _effects.update(w32effects)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   279
    elif realmode == 'ansi':
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   280
        _terminfo_params = {}
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   281
    elif realmode == 'terminfo':
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   282
        _terminfosetup(ui, mode)
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   283
        if not _terminfo_params:
24266
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   284
            ## FIXME Shouldn't we return None in this case too?
c14b1d349af3 color: consolidate cut-and-paste code
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24249
diff changeset
   285
            modewarn()
14495
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   286
            realmode = 'ansi'
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   287
    else:
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   288
        return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   289
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   290
    if always or (auto and formatted):
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   291
        return realmode
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   292
    return None
ad6ad51cc0dd color: fix --color=always when output is piped
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14145
diff changeset
   293
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   294
try:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   295
    import curses
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   296
    # Mapping from effect name to terminfo attribute name (or raw code) or
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   297
    # color number.  This will also force-load the curses module.
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   298
    _terminfo_params = {'none': (True, 'sgr0', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   299
                        'standout': (True, 'smso', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   300
                        'underline': (True, 'smul', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   301
                        'reverse': (True, 'rev', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   302
                        'inverse': (True, 'rev', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   303
                        'blink': (True, 'blink', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   304
                        'dim': (True, 'dim', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   305
                        'bold': (True, 'bold', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   306
                        'invisible': (True, 'invis', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   307
                        'italic': (True, 'sitm', ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   308
                        'black': (False, curses.COLOR_BLACK, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   309
                        'red': (False, curses.COLOR_RED, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   310
                        'green': (False, curses.COLOR_GREEN, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   311
                        'yellow': (False, curses.COLOR_YELLOW, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   312
                        'blue': (False, curses.COLOR_BLUE, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   313
                        'magenta': (False, curses.COLOR_MAGENTA, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   314
                        'cyan': (False, curses.COLOR_CYAN, ''),
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   315
                        'white': (False, curses.COLOR_WHITE, '')}
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   316
except ImportError:
21227
75aaae8ad660 color: don't fail on error messages when no curses (issue4237)
Mads Kiilerich <madski@unity3d.com>
parents: 21190
diff changeset
   317
    _terminfo_params = {}
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   318
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   319
_styles = {'grep.match': 'red bold',
17806
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   320
           'grep.linenumber': 'green',
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   321
           'grep.rev': 'green',
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   322
           'grep.change': 'green',
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   323
           'grep.sep': 'cyan',
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   324
           'grep.filename': 'magenta',
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   325
           'grep.user': 'magenta',
dc7010ed0101 grep: colorize all fields
Idan Kamara <idankk86@gmail.com>
parents: 17424
diff changeset
   326
           'grep.date': 'magenta',
25347
ceec79b5657a bookmarks: name label for active bookmark correctly
Ryan McElroy <rmcelroy@fb.com>
parents: 25186
diff changeset
   327
           'bookmarks.active': 'green',
11969
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   328
           'branches.active': 'none',
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   329
           'branches.closed': 'black bold',
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   330
           'branches.current': 'green',
52ec5c813723 color: enable branches support
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 11750
diff changeset
   331
           'branches.inactive': 'none',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   332
           'diff.changed': 'white',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   333
           'diff.deleted': 'red',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   334
           'diff.diffline': 'bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   335
           'diff.extended': 'cyan bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   336
           'diff.file_a': 'red bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   337
           'diff.file_b': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   338
           'diff.hunk': 'magenta',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   339
           'diff.inserted': 'green',
22710
b07fd3ac8882 color: document the possibility to colourise tabs in diffs
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22465
diff changeset
   340
           'diff.tab': '',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   341
           'diff.trailingwhitespace': 'bold red_background',
22465
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
   342
           'changeset.public' : '',
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
   343
           'changeset.draft' : '',
f8e2aebbb24c color: document that changeset phases have labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22464
diff changeset
   344
           'changeset.secret' : '',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   345
           'diffstat.deleted': 'red',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   346
           'diffstat.inserted': 'green',
19215
f184fe1e2ac5 summary: add a histedit hook
Bryan O'Sullivan <bryano@fb.com>
parents: 19214
diff changeset
   347
           'histedit.remaining': 'red bold',
13774
1ce0e80799c0 ui: label prompts, default to yellow prompts
Martin Geisler <mg@lazybytes.net>
parents: 13648
diff changeset
   348
           'ui.prompt': 'yellow',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   349
           'log.changeset': 'yellow',
23173
122f5c3f3308 patchbomb: add label and color to the confirm output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22772
diff changeset
   350
           'patchbomb.finalsummary': '',
122f5c3f3308 patchbomb: add label and color to the confirm output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22772
diff changeset
   351
           'patchbomb.from': 'magenta',
122f5c3f3308 patchbomb: add label and color to the confirm output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22772
diff changeset
   352
           'patchbomb.to': 'cyan',
122f5c3f3308 patchbomb: add label and color to the confirm output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22772
diff changeset
   353
           'patchbomb.subject': 'green',
122f5c3f3308 patchbomb: add label and color to the confirm output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22772
diff changeset
   354
           'patchbomb.diffstats': '',
19214
0250047a365e summary: indicate if a rebase is underway
Bryan O'Sullivan <bryano@fb.com>
parents: 19073
diff changeset
   355
           'rebase.rebased': 'blue',
0250047a365e summary: indicate if a rebase is underway
Bryan O'Sullivan <bryano@fb.com>
parents: 19073
diff changeset
   356
           'rebase.remaining': 'red bold',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   357
           'resolve.resolved': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   358
           'resolve.unresolved': 'red bold',
19854
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   359
           'shelve.age': 'cyan',
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   360
           'shelve.newest': 'green bold',
49d4919d21c2 shelve: add a shelve extension to save/restore working changes
David Soria Parra <dsp@experimentalworks.net>
parents: 19298
diff changeset
   361
           'shelve.name': 'blue bold',
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   362
           'status.added': 'green bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   363
           'status.clean': 'none',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   364
           'status.copied': 'none',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   365
           'status.deleted': 'cyan bold underline',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   366
           'status.ignored': 'black bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   367
           'status.modified': 'blue bold',
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   368
           'status.removed': 'red bold',
15048
2f0a3977c939 color: add styles for tags
Marc Simpson <marc@0branch.com>
parents: 14989
diff changeset
   369
           'status.unknown': 'magenta bold underline',
2f0a3977c939 color: add styles for tags
Marc Simpson <marc@0branch.com>
parents: 14989
diff changeset
   370
           'tags.normal': 'green',
2f0a3977c939 color: add styles for tags
Marc Simpson <marc@0branch.com>
parents: 14989
diff changeset
   371
           'tags.local': 'black bold'}
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   372
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   373
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   374
def _effect_str(effect):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   375
    '''Helper function for render_effects().'''
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   376
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   377
    bg = False
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   378
    if effect.endswith('_background'):
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   379
        bg = True
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   380
        effect = effect[:-11]
30174
5d777fe4615d color: ignore effects missing from terminfo
Danek Duvall <danek.duvall@oracle.com>
parents: 30173
diff changeset
   381
    try:
5d777fe4615d color: ignore effects missing from terminfo
Danek Duvall <danek.duvall@oracle.com>
parents: 30173
diff changeset
   382
        attr, val, termcode = _terminfo_params[effect]
5d777fe4615d color: ignore effects missing from terminfo
Danek Duvall <danek.duvall@oracle.com>
parents: 30173
diff changeset
   383
    except KeyError:
5d777fe4615d color: ignore effects missing from terminfo
Danek Duvall <danek.duvall@oracle.com>
parents: 30173
diff changeset
   384
        return ''
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   385
    if attr:
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   386
        if termcode:
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   387
            return termcode
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   388
        else:
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   389
            return curses.tigetstr(val)
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   390
    elif bg:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   391
        return curses.tparm(curses.tigetstr('setab'), val)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   392
    else:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   393
        return curses.tparm(curses.tigetstr('setaf'), val)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   394
8622
0a4f6e1b78dc color: use lists instead of tuples for effects
Martin Geisler <mg@lazybytes.net>
parents: 8278
diff changeset
   395
def render_effects(text, effects):
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   396
    'Wrap text in commands to turn on each effect.'
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   397
    if not text:
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   398
        return text
13987
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   399
    if not _terminfo_params:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   400
        start = [str(_effects[e]) for e in ['none'] + effects.split()]
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   401
        start = '\033[' + ';'.join(start) + 'm'
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   402
        stop = '\033[' + str(_effects['none']) + 'm'
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   403
    else:
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   404
        start = ''.join(_effect_str(effect)
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   405
                        for effect in ['none'] + effects.split())
e0f07847f8de color: add support for terminfo-based attributes and color
Danek Duvall <duvall@comfychair.org>
parents: 13919
diff changeset
   406
        stop = _effect_str('none')
7459
3fb5c142a9f0 color: replace effect-specific reset control codes with general purpose one
Brodie Rao <me+hg@dackz.net>
parents: 7457
diff changeset
   407
    return ''.join([start, text, stop])
5787
b7b22a2ade2e Add colored output to status and qseries commands
Kevin Christen <kevin.christen@gmail.com>
parents:
diff changeset
   408
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   409
def extstyles():
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   410
    for name, ext in extensions.extensions():
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   411
        _styles.update(getattr(ext, 'colortable', {}))
7456
79eb16db5e4a color: diff colorization
Brodie Rao <me+hg@dackz.net>
parents: 7455
diff changeset
   412
20992
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   413
def valideffect(effect):
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   414
    'Determine if the effect is valid or not.'
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   415
    good = False
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   416
    if not _terminfo_params and effect in _effects:
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   417
        good = True
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   418
    elif effect in _terminfo_params or effect[:-11] in _terminfo_params:
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   419
        good = True
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   420
    return good
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   421
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   422
def configstyles(ui):
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   423
    for status, cfgeffects in ui.configitems('color'):
30173
f34a8cff51d9 color: allow for user-configurable terminfo codes for effects
Danek Duvall <danek.duvall@oracle.com>
parents: 29890
diff changeset
   424
        if '.' not in status or status.startswith(('color.', 'terminfo.')):
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   425
            continue
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   426
        cfgeffects = ui.configlist('color', status)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   427
        if cfgeffects:
8945
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   428
            good = []
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   429
            for e in cfgeffects:
20992
05086b56b564 color: extract method for determining a valid effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20670
diff changeset
   430
                if valideffect(e):
8945
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   431
                    good.append(e)
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   432
                else:
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   433
                    ui.warn(_("ignoring unknown color/effect %r "
7b3d837ca60e color: don't blow up if configured with unknown color (just warn).
Greg Ward <greg-hg@gerg.ca>
parents: 8894
diff changeset
   434
                              "(configured in color.%s)\n")
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   435
                            % (e, status))
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   436
            _styles[status] = ' '.join(good)
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   437
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   438
class colorui(uimod.ui):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   439
    _colormode = 'ansi'
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   440
    def write(self, *args, **opts):
19072
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   441
        if self._colormode is None:
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   442
            return super(colorui, self).write(*args, **opts)
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   443
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   444
        label = opts.get('label', '')
28633
e35d7f131483 ui: add prompt argument to write (issue5154) (API)
timeless <timeless@mozdev.org>
parents: 28374
diff changeset
   445
        if self._buffers and not opts.get('prompt', False):
27108
717b75ae5bb0 color: evaluate labels at write time
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   446
            if self._bufferapplylabels:
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   447
                self._buffers[-1].extend(self.label(a, label) for a in args)
27108
717b75ae5bb0 color: evaluate labels at write time
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   448
            else:
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   449
                self._buffers[-1].extend(args)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   450
        elif self._colormode == 'win32':
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   451
            for a in args:
11727
c34a1ab80550 color: pass write/write_err to win32print correctly (issue2312)
Brodie Rao <brodie@bitheap.org>
parents: 11555
diff changeset
   452
                win32print(a, super(colorui, self).write, **opts)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   453
        else:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   454
            return super(colorui, self).write(
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   455
                *[self.label(a, label) for a in args], **opts)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   456
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   457
    def write_err(self, *args, **opts):
19072
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   458
        if self._colormode is None:
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   459
            return super(colorui, self).write_err(*args, **opts)
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   460
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   461
        label = opts.get('label', '')
24848
2f88821856eb ui: allow capture of subprocess output
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24535
diff changeset
   462
        if self._bufferstates and self._bufferstates[-1][0]:
21190
2606e7f227f6 color: add missing handling of stderr capture introduced by 350dc24a553d
Yuya Nishihara <yuya@tcha.org>
parents: 21037
diff changeset
   463
            return self.write(*args, **opts)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   464
        if self._colormode == 'win32':
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   465
            for a in args:
11727
c34a1ab80550 color: pass write/write_err to win32print correctly (issue2312)
Brodie Rao <brodie@bitheap.org>
parents: 11555
diff changeset
   466
                win32print(a, super(colorui, self).write_err, **opts)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   467
        else:
11732
386e56ecfb78 color: call correct superclass method in write_err
Brodie Rao <brodie@bitheap.org>
parents: 11727
diff changeset
   468
            return super(colorui, self).write_err(
27110
f04bd381e8c0 ui: avoid needless casting to a str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27109
diff changeset
   469
                *[self.label(a, label) for a in args], **opts)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   470
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   471
    def showlabel(self, msg, label):
22763
7ebf91426f6a color: omit debug label output on empty strings
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22711
diff changeset
   472
        if label and msg:
7ebf91426f6a color: omit debug label output on empty strings
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22711
diff changeset
   473
            if msg[-1] == '\n':
22464
964dd1c491ca color: change the debug output format
Matt Mackall <mpm@selenic.com>
parents: 22463
diff changeset
   474
                return "[%s|%s]\n" % (label, msg[:-1])
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   475
            else:
22464
964dd1c491ca color: change the debug output format
Matt Mackall <mpm@selenic.com>
parents: 22463
diff changeset
   476
                return "[%s|%s]" % (label, msg)
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   477
        else:
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   478
            return msg
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   479
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   480
    def label(self, msg, label):
19072
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   481
        if self._colormode is None:
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   482
            return super(colorui, self).label(msg, label)
b7b50a54bec9 color: turn colorui functions into forwards when color is None
Siddharth Agarwal <sid0@fb.com>
parents: 19063
diff changeset
   483
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   484
        if self._colormode == 'debug':
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   485
            return self.showlabel(msg, label)
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   486
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   487
        effects = []
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   488
        for l in label.split():
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   489
            s = _styles.get(l, '')
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   490
            if s:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   491
                effects.append(s)
20993
a8db48e997b5 color: fallback and test label as an effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20992
diff changeset
   492
            elif valideffect(l):
a8db48e997b5 color: fallback and test label as an effect
Sean Farley <sean.michael.farley@gmail.com>
parents: 20992
diff changeset
   493
                effects.append(l)
14145
4b7e4b9db8fb color: fix using multiple effects
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14139
diff changeset
   494
        effects = ' '.join(effects)
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   495
        if effects:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   496
            return '\n'.join([render_effects(s, effects)
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   497
                              for s in msg.split('\n')])
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   498
        return msg
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   499
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   500
def uisetup(ui):
10871
3f30190781a3 color: respect HGPLAIN
Steve Borho <steve@borho.org>
parents: 10870
diff changeset
   501
    if ui.plain():
3f30190781a3 color: respect HGPLAIN
Steve Borho <steve@borho.org>
parents: 10870
diff changeset
   502
        return
19298
6cd8403e070f color: use pythons isinstance method instead of reprogramming it
Simon Heimberg <simohe@besonet.ch>
parents: 19297
diff changeset
   503
    if not isinstance(ui, colorui):
19063
7ae12ce87594 color: set the ui class in reposetup, before a repo is created
Simon Heimberg <simohe@besonet.ch>
parents: 18921
diff changeset
   504
        colorui.__bases__ = (ui.__class__,)
7ae12ce87594 color: set the ui class in reposetup, before a repo is created
Simon Heimberg <simohe@besonet.ch>
parents: 18921
diff changeset
   505
        ui.__class__ = colorui
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   506
    def colorcmd(orig, ui_, opts, cmd, cmdfunc):
19297
8ddc3cd962ba color: only provide the required opt to _modesetup
Simon Heimberg <simohe@besonet.ch>
parents: 19215
diff changeset
   507
        mode = _modesetup(ui_, opts['color'])
19073
d78d0256198e color: set _colormode to None when mode is unset (issue3895)
Siddharth Agarwal <sid0@fb.com>
parents: 19072
diff changeset
   508
        colorui._colormode = mode
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   509
        if mode and mode != 'debug':
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   510
            extstyles()
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11326
diff changeset
   511
            configstyles(ui_)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   512
        return orig(ui_, opts, cmd, cmdfunc)
23780
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   513
    def colorgit(orig, gitsub, commands, env=None, stream=False, cwd=None):
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   514
        if gitsub.ui._colormode and len(commands) and commands[0] == "diff":
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   515
                # insert the argument in the front,
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   516
                # the end of git diff arguments is used for paths
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   517
                commands.insert(1, '--color')
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   518
        return orig(gitsub, commands, env, stream, cwd)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   519
    extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
23780
a857755144dc color: add support for colorizing git subrepo diffs
Mathias De Maré <mathias.demare@gmail.com>
parents: 23173
diff changeset
   520
    extensions.wrapfunction(subrepo.gitsubrepo, '_gitnodir', colorgit)
10826
717c35d55fb3 color: colorize based on output labels instead of parsing output
Brodie Rao <brodie@bitheap.org>
parents: 10477
diff changeset
   521
12693
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   522
def extsetup(ui):
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   523
    commands.globalopts.append(
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   524
        ('', 'color', 'auto',
22463
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   525
         # i18n: 'always', 'auto', 'never', and 'debug' are keywords
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   526
         # and should not be translated
1c4ae0f6a30f color: enable debug option to show labels
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22462
diff changeset
   527
         _("when to colorize (boolean, always, auto, never, or debug)"),
12693
33f0682ba8b1 color: add global option in extsetup() instead of globally
Brodie Rao <brodie@bitheap.org>
parents: 12278
diff changeset
   528
         _('TYPE')))
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   529
21249
2b8697e20978 color: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21227
diff changeset
   530
@command('debugcolor', [], 'hg debugcolor')
20455
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   531
def debugcolor(ui, repo, **opts):
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   532
    global _styles
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   533
    _styles = {}
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   534
    for effect in _effects.keys():
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   535
        _styles[effect] = effect
21024
7731a2281cf0 spelling: fixes from spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 20993
diff changeset
   536
    ui.write(('color mode: %s\n') % ui._colormode)
20455
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   537
    ui.write(_('available colors:\n'))
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   538
    for label, colors in _styles.items():
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   539
        ui.write(('%s\n') % colors, label=label)
a2c08adbb86e color: add debugcolor command (issue4094)
Olle Lundberg <geek@nerd.sh>
parents: 20067
diff changeset
   540
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   541
if os.name != 'nt':
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   542
    w32effects = None
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   543
else:
28968
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   544
    import ctypes
a5d449a7bc8f py3: make color use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28633
diff changeset
   545
    import re
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   546
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   547
    _kernel32 = ctypes.windll.kernel32
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   548
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   549
    _WORD = ctypes.c_ushort
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   550
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   551
    _INVALID_HANDLE_VALUE = -1
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   552
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   553
    class _COORD(ctypes.Structure):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   554
        _fields_ = [('X', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   555
                    ('Y', ctypes.c_short)]
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   556
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   557
    class _SMALL_RECT(ctypes.Structure):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   558
        _fields_ = [('Left', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   559
                    ('Top', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   560
                    ('Right', ctypes.c_short),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   561
                    ('Bottom', ctypes.c_short)]
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   562
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   563
    class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   564
        _fields_ = [('dwSize', _COORD),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   565
                    ('dwCursorPosition', _COORD),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   566
                    ('wAttributes', _WORD),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   567
                    ('srWindow', _SMALL_RECT),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   568
                    ('dwMaximumWindowSize', _COORD)]
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   569
29890
31a6d5e14508 py3: remove use of *L syntax
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29841
diff changeset
   570
    _STD_OUTPUT_HANDLE = 0xfffffff5 # (DWORD)-11
31a6d5e14508 py3: remove use of *L syntax
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29841
diff changeset
   571
    _STD_ERROR_HANDLE = 0xfffffff4  # (DWORD)-12
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   572
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   573
    _FOREGROUND_BLUE = 0x0001
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   574
    _FOREGROUND_GREEN = 0x0002
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   575
    _FOREGROUND_RED = 0x0004
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   576
    _FOREGROUND_INTENSITY = 0x0008
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   577
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   578
    _BACKGROUND_BLUE = 0x0010
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   579
    _BACKGROUND_GREEN = 0x0020
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   580
    _BACKGROUND_RED = 0x0040
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   581
    _BACKGROUND_INTENSITY = 0x0080
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   582
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   583
    _COMMON_LVB_REVERSE_VIDEO = 0x4000
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   584
    _COMMON_LVB_UNDERSCORE = 0x8000
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   585
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   586
    # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   587
    w32effects = {
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   588
        'none': -1,
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   589
        'black': 0,
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   590
        'red': _FOREGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   591
        'green': _FOREGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   592
        'yellow': _FOREGROUND_RED | _FOREGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   593
        'blue': _FOREGROUND_BLUE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   594
        'magenta': _FOREGROUND_BLUE | _FOREGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   595
        'cyan': _FOREGROUND_BLUE | _FOREGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   596
        'white': _FOREGROUND_RED | _FOREGROUND_GREEN | _FOREGROUND_BLUE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   597
        'bold': _FOREGROUND_INTENSITY,
12278
c4c2ba553401 merge with stable
Sune Foldager <cryo@cyanite.org>
parents: 12089 12277
diff changeset
   598
        'black_background': 0x100,                  # unused value > 0x0f
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   599
        'red_background': _BACKGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   600
        'green_background': _BACKGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   601
        'yellow_background': _BACKGROUND_RED | _BACKGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   602
        'blue_background': _BACKGROUND_BLUE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   603
        'purple_background': _BACKGROUND_BLUE | _BACKGROUND_RED,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   604
        'cyan_background': _BACKGROUND_BLUE | _BACKGROUND_GREEN,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   605
        'white_background': (_BACKGROUND_RED | _BACKGROUND_GREEN |
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   606
                             _BACKGROUND_BLUE),
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   607
        'bold_background': _BACKGROUND_INTENSITY,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   608
        'underline': _COMMON_LVB_UNDERSCORE,  # double-byte charsets only
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   609
        'inverse': _COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   610
    }
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   611
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   612
    passthrough = set([_FOREGROUND_INTENSITY,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   613
                       _BACKGROUND_INTENSITY,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   614
                       _COMMON_LVB_UNDERSCORE,
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   615
                       _COMMON_LVB_REVERSE_VIDEO])
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   616
13641
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   617
    stdout = _kernel32.GetStdHandle(
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   618
                  _STD_OUTPUT_HANDLE)  # don't close the handle returned
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   619
    if stdout is None or stdout == _INVALID_HANDLE_VALUE:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   620
        w32effects = None
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   621
    else:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   622
        csbi = _CONSOLE_SCREEN_BUFFER_INFO()
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   623
        if not _kernel32.GetConsoleScreenBufferInfo(
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   624
                    stdout, ctypes.byref(csbi)):
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   625
            # stdout may not support GetConsoleScreenBufferInfo()
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   626
            # when called from subprocess or redirected
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   627
            w32effects = None
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   628
        else:
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   629
            origattr = csbi.wAttributes
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   630
            ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)',
2420cb1ea1d6 color: port to using ctypes (issue2687)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13635
diff changeset
   631
                                re.MULTILINE | re.DOTALL)
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   632
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   633
    def win32print(text, orig, **opts):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   634
        label = opts.get('label', '')
12277
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   635
        attr = origattr
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   636
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   637
        def mapcolor(val, attr):
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   638
            if val == -1:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   639
                return origattr
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   640
            elif val in passthrough:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   641
                return attr | val
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   642
            elif val > 0x0f:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   643
                return (val & 0x70) | (attr & 0x8f)
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   644
            else:
a7d3147bd4b3 color: add win32 support for non-black background
Sune Foldager <cryo@cyanite.org>
parents: 12083
diff changeset
   645
                return (val & 0x07) | (attr & 0xf8)
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   646
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   647
        # determine console attributes based on labels
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   648
        for l in label.split():
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   649
            style = _styles.get(l, '')
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   650
            for effect in style.split():
21991
aca137619a45 color: pass on key error for win32 (issue4298)
Sean Farley <sean.michael.farley@gmail.com>
parents: 21723
diff changeset
   651
                try:
aca137619a45 color: pass on key error for win32 (issue4298)
Sean Farley <sean.michael.farley@gmail.com>
parents: 21723
diff changeset
   652
                    attr = mapcolor(w32effects[effect], attr)
aca137619a45 color: pass on key error for win32 (issue4298)
Sean Farley <sean.michael.farley@gmail.com>
parents: 21723
diff changeset
   653
                except KeyError:
aca137619a45 color: pass on key error for win32 (issue4298)
Sean Farley <sean.michael.farley@gmail.com>
parents: 21723
diff changeset
   654
                    # w32effects could not have certain attributes so we skip
aca137619a45 color: pass on key error for win32 (issue4298)
Sean Farley <sean.michael.farley@gmail.com>
parents: 21723
diff changeset
   655
                    # them if not found
aca137619a45 color: pass on key error for win32 (issue4298)
Sean Farley <sean.michael.farley@gmail.com>
parents: 21723
diff changeset
   656
                    pass
10870
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   657
        # hack to ensure regexp finds data
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   658
        if not text.startswith('\033['):
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   659
            text = '\033[m' + text
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   660
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   661
        # Look for ANSI-like codes embedded in text
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   662
        m = re.match(ansire, text)
a4944b430417 color: add support for Windows consoles
Steve Borho <steve@borho.org>
parents: 10869
diff changeset
   663
13919
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   664
        try:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   665
            while m:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   666
                for sattr in m.group(1).split(';'):
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   667
                    if sattr:
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   668
                        attr = mapcolor(int(sattr), attr)
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   669
                _kernel32.SetConsoleTextAttribute(stdout, attr)
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   670
                orig(m.group(2), **opts)
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   671
                m = re.match(ansire, m.group(3))
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   672
        finally:
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 16743
diff changeset
   673
            # Explicitly reset original attributes
13919
67f20625703f color: reset win32 console color in a finally block
Idan Kamara <idankk86@gmail.com>
parents: 13774
diff changeset
   674
            _kernel32.SetConsoleTextAttribute(stdout, origattr)