Debian Codegraph @

osrf_pycommon/terminal_color/__init__.py

raw
1 # Copyright 2014 Open Source Robotics Foundation, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 This module provides tools for colorizing terminal output.
17
18 This module defines the ansi escape sequences used for colorizing the
19 output from terminal programs in Linux.
20 You can access the ansi escape sequences using the :py:func:`ansi` function:
21
22 .. code-block:: python
23
24 >>> from osrf_pycommon.terminal_color import ansi
25 >>> print(["This is ", ansi('red'), "red", ansi('reset'), "."])
26 ['This is ', '\\x1b[31m', 'red', '\\x1b[0m', '.']
27
28 You can also use :py:func:`format_color` to do in-line substitution of keys
29 wrapped in ``@{}`` markers for their ansi escape sequences:
30
31 .. code-block:: python
32
33 >>> from osrf_pycommon.terminal_color import format_color
34 >>> print(format_color("This is @{bf}blue@{reset}.").split())
35 ['This', 'is', '\\x1b[34mblue\\x1b[0m.']
36
37 This is a list of all of the available substitutions:
38
39 +-------------------+--------------+--------------+
40 | Long Form | Shorter | Value |
41 +===================+==============+==============+
42 | ``@{blackf}`` | ``@{kf}`` | ``\\033[30m`` |
43 +-------------------+--------------+--------------+
44 | ``@{redf}`` | ``@{rf}`` | ``\\033[31m`` |
45 +-------------------+--------------+--------------+
46 | ``@{greenf}`` | ``@{gf}`` | ``\\033[32m`` |
47 +-------------------+--------------+--------------+
48 | ``@{yellowf}`` | ``@{yf}`` | ``\\033[33m`` |
49 +-------------------+--------------+--------------+
50 | ``@{bluef}`` | ``@{bf}`` | ``\\033[34m`` |
51 +-------------------+--------------+--------------+
52 | ``@{purplef}`` | ``@{pf}`` | ``\\033[35m`` |
53 +-------------------+--------------+--------------+
54 | ``@{cyanf}`` | ``@{cf}`` | ``\\033[36m`` |
55 +-------------------+--------------+--------------+
56 | ``@{whitef}`` | ``@{wf}`` | ``\\033[37m`` |
57 +-------------------+--------------+--------------+
58 | ``@{blackb}`` | ``@{kb}`` | ``\\033[40m`` |
59 +-------------------+--------------+--------------+
60 | ``@{redb}`` | ``@{rb}`` | ``\\033[41m`` |
61 +-------------------+--------------+--------------+
62 | ``@{greenb}`` | ``@{gb}`` | ``\\033[42m`` |
63 +-------------------+--------------+--------------+
64 | ``@{yellowb}`` | ``@{yb}`` | ``\\033[43m`` |
65 +-------------------+--------------+--------------+
66 | ``@{blueb}`` | ``@{bb}`` | ``\\033[44m`` |
67 +-------------------+--------------+--------------+
68 | ``@{purpleb}`` | ``@{pb}`` | ``\\033[45m`` |
69 +-------------------+--------------+--------------+
70 | ``@{cyanb}`` | ``@{cb}`` | ``\\033[46m`` |
71 +-------------------+--------------+--------------+
72 | ``@{whiteb}`` | ``@{wb}`` | ``\\033[47m`` |
73 +-------------------+--------------+--------------+
74 | ``@{escape}`` | | ``\\033`` |
75 +-------------------+--------------+--------------+
76 | ``@{reset}`` | ``@|`` | ``\\033[0m`` |
77 +-------------------+--------------+--------------+
78 | ``@{boldon}`` | ``@!`` | ``\\033[1m`` |
79 +-------------------+--------------+--------------+
80 | ``@{italicson}`` | ``@/`` | ``\\033[3m`` |
81 +-------------------+--------------+--------------+
82 | ``@{ulon}`` | ``@_`` | ``\\033[4m`` |
83 +-------------------+--------------+--------------+
84 | ``@{invon}`` | | ``\\033[7m`` |
85 +-------------------+--------------+--------------+
86 | ``@{boldoff}`` | | ``\\033[22m`` |
87 +-------------------+--------------+--------------+
88 | ``@{italicsoff}`` | | ``\\033[23m`` |
89 +-------------------+--------------+--------------+
90 | ``@{uloff}`` | | ``\\033[24m`` |
91 +-------------------+--------------+--------------+
92 | ``@{invoff}`` | | ``\\033[27m`` |
93 +-------------------+--------------+--------------+
94
95 These substitution's values come from the ANSI color escape sequences,
96 see: http://en.wikipedia.org/wiki/ANSI_escape_code
97
98 Also for any of the keys which have a trailing ``f``, you can safely drop the
99 trailing ``f`` and get the same thing.
100
101 For example, ``format_color("@{redf}")`` and ``format_color("@{red}")`` are
102 functionally equivalent.
103
104 Also, many of the substitutions have shorten forms for convenience, such that
105 ``@{redf}``, ``@{rf}``, ``@{red}``, and ``@{r}`` are all the same.
106
107 Note that a trailing ``b`` is always required when specifying a background.
108
109 Some of the most common non-color sequences have ``{}``'less versions.
110
111 For example, ``@{boldon}``'s shorter form is ``@!``.
112
113 By default, the substitutions (and calls to :py:func:`ansi`) resolve to escape
114 sequences, but if you call :py:func:`disable_ansi_color_substitution_globally`
115 then they will resolve to empty strings.
116
117 This allows you to always use the substitution strings and disable them
118 globally when desired.
119
120 On Windows the substitutions are always resolved to empty strings as the
121 ansi escape sequences do not work on Windows.
122 Instead strings annotated with ``@{}`` style substitutions or raw ``\\x1b[xxm``
123 style ansi escape sequences must be passed to :py:func:`print_color` in order
124 for colors to be displayed on windows.
125 Also the :py:func:`print_ansi_color_win32` function can be used on strings
126 which only contain ansi escape sequences.
127
128 .. note::
129
130 There are existing Python modules like
131 `colorama <https://pypi.python.org/pypi/colorama>`_ which provide ansi
132 colorization on multiple platforms, so a valid question is:
133 "why write this module?".
134 The reason for writing this module is to provide the color annotation of
135 strings and functions for removing or replacing ansi escape sequences which
136 are not provided by modules like colorama.
137 This module could have depended on colorama for colorization on Windows,
138 but colorama works by replacing the built-in ``sys.stdout`` and
139 ``sys.stderr``, which we did not want and it has extra functionality that
140 we do not need.
141 So, instead of depending on colorama, the Windows color printing code
142 was used as the inspiration for the Windows color printing in the
143 ``windows.py`` module in this ``terminal_color`` package.
144 The colorama license was placed in the header of that file and the colorama
145 license is compatible with this package's license.
146
147 """
148
149 from .ansi_re import remove_ansi_escape_senquences
150 from .ansi_re import remove_ansi_escape_sequences
151 from .ansi_re import split_by_ansi_escape_sequence
152
153 from .impl import ansi
154 from .impl import disable_ansi_color_substitution_globally
155 from .impl import enable_ansi_color_substitution_globally
156 from .impl import format_color
157 from .impl import get_ansi_dict
158 from .impl import print_ansi_color_win32
159 from .impl import print_color
160 from .impl import sanitize
161 from .impl import test_colors
162
163 __all__ = [
164 'ansi',
165 'disable_ansi_color_substitution_globally',
166 'enable_ansi_color_substitution_globally',
167 'format_color',
168 'get_ansi_dict',
169 'print_ansi_color_win32',
170 'print_color',
171 'remove_ansi_escape_senquences',
172 'remove_ansi_escape_sequences',
173 'sanitize',
174 'split_by_ansi_escape_sequence',
175 'test_colors',
176 ]