##// END OF EJS Templates
Update IPython/utils/coloransi.py
M Bussonnier -
Show More
@@ -1,202 +1,202 b''
1 1 """Tools for coloring text in ANSI terminals.
2 2 """
3 3
4 4 #*****************************************************************************
5 5 # Copyright (C) 2002-2006 Fernando Perez. <fperez@colorado.edu>
6 6 #
7 7 # Distributed under the terms of the BSD License. The full license is in
8 8 # the file COPYING, distributed as part of this software.
9 9 #*****************************************************************************
10 10
11 11
12 12 import os
13 13 import warnings
14 14
15 15 from IPython.utils.ipstruct import Struct
16 16
17 17 __all__ = ["TermColors", "InputTermColors", "ColorScheme", "ColorSchemeTable"]
18 18
19 19 color_templates = (
20 20 # Dark colors
21 21 ("Black" , "0;30"),
22 22 ("Red" , "0;31"),
23 23 ("Green" , "0;32"),
24 24 ("Brown" , "0;33"),
25 25 ("Blue" , "0;34"),
26 26 ("Purple" , "0;35"),
27 27 ("Cyan" , "0;36"),
28 28 ("LightGray" , "0;37"),
29 29 # Light colors
30 30 ("DarkGray" , "1;30"),
31 31 ("LightRed" , "1;31"),
32 32 ("LightGreen" , "1;32"),
33 33 ("Yellow" , "1;33"),
34 34 ("LightBlue" , "1;34"),
35 35 ("LightPurple" , "1;35"),
36 36 ("LightCyan" , "1;36"),
37 37 ("White" , "1;37"),
38 38 # Blinking colors. Probably should not be used in anything serious.
39 39 ("BlinkBlack" , "5;30"),
40 40 ("BlinkRed" , "5;31"),
41 41 ("BlinkGreen" , "5;32"),
42 42 ("BlinkYellow" , "5;33"),
43 43 ("BlinkBlue" , "5;34"),
44 44 ("BlinkPurple" , "5;35"),
45 45 ("BlinkCyan" , "5;36"),
46 46 ("BlinkLightGray", "5;37"),
47 47 )
48 48
49 49 def make_color_table(in_class):
50 50 """Build a set of color attributes in a class.
51 51
52 52 Helper function for building the :class:`TermColors` and
53 53 :class`InputTermColors`.
54 54 """
55 55 for name,value in color_templates:
56 56 setattr(in_class,name,in_class._base % value)
57 57
58 58 class TermColors:
59 59 """Color escape sequences.
60 60
61 61 This class defines the escape sequences for all the standard (ANSI?)
62 62 colors in terminals. Also defines a NoColor escape which is just the null
63 63 string, suitable for defining 'dummy' color schemes in terminals which get
64 64 confused by color escapes.
65 65
66 66 This class should be used as a mixin for building color schemes."""
67 67
68 68 NoColor = '' # for color schemes in color-less terminals.
69 69 Normal = '\033[0m' # Reset normal coloring
70 70 _base = '\033[%sm' # Template for all other colors
71 71
72 72 # Build the actual color table as a set of class attributes:
73 73 make_color_table(TermColors)
74 74
75 75 class InputTermColors:
76 76 """Color escape sequences for input prompts.
77 77
78 78 This class is similar to TermColors, but the escapes are wrapped in \\001
79 79 and \\002 so that readline can properly know the length of each line and
80 80 can wrap lines accordingly. Use this class for any colored text which
81 81 needs to be used in input prompts, such as in calls to raw_input().
82 82
83 83 This class defines the escape sequences for all the standard (ANSI?)
84 84 colors in terminals. Also defines a NoColor escape which is just the null
85 85 string, suitable for defining 'dummy' color schemes in terminals which get
86 86 confused by color escapes.
87 87
88 88 This class should be used as a mixin for building color schemes."""
89 89
90 90 NoColor = '' # for color schemes in color-less terminals.
91 91
92 92 if os.name == 'nt' and os.environ.get('TERM','dumb') == 'emacs':
93 93 # (X)emacs on W32 gets confused with \001 and \002 so we remove them
94 94 Normal = '\033[0m' # Reset normal coloring
95 95 _base = '\033[%sm' # Template for all other colors
96 96 else:
97 97 Normal = '\001\033[0m\002' # Reset normal coloring
98 98 _base = '\001\033[%sm\002' # Template for all other colors
99 99
100 100 # Build the actual color table as a set of class attributes:
101 101 make_color_table(InputTermColors)
102 102
103 103 class NoColors:
104 104 """This defines all the same names as the colour classes, but maps them to
105 105 empty strings, so it can easily be substituted to turn off colours."""
106 106 NoColor = ''
107 107 Normal = ''
108 108
109 109 for name, value in color_templates:
110 110 setattr(NoColors, name, '')
111 111
112 112 class ColorScheme:
113 113 """Generic color scheme class. Just a name and a Struct."""
114 114
115 115 name: str
116 116 colors: Struct
117 117
118 118 def __init__(self, __scheme_name_, colordict=None, **colormap):
119 119 self.name = __scheme_name_
120 120 if colormap:
121 121 warnings.warn(
122 122 "Passing each colors as a kwarg to ColorScheme is "
123 123 "considered for deprecation. Please pass a "
124 "dict as single dict as second parameter. If you are using this"
125 "Feature, please comment an subscribe to issue "
124 "a single dict as second parameter. If you are using this"
125 "feature, please comment an subscribe to issue "
126 126 "https://github.com/ipython/ipython/issues/14304",
127 127 PendingDeprecationWarning,
128 128 stacklevel=2,
129 129 )
130 130 if colordict is None:
131 131 self.colors = Struct(**colormap)
132 132 else:
133 133 self.colors = Struct(colordict)
134 134
135 135 def copy(self,name=None):
136 136 """Return a full copy of the object, optionally renaming it."""
137 137 if name is None:
138 138 name = self.name
139 139 return ColorScheme(name, self.colors.dict())
140 140
141 141 class ColorSchemeTable(dict):
142 142 """General class to handle tables of color schemes.
143 143
144 144 It's basically a dict of color schemes with a couple of shorthand
145 145 attributes and some convenient methods.
146 146
147 147 active_scheme_name -> obvious
148 148 active_colors -> actual color table of the active scheme"""
149 149
150 150 def __init__(self, scheme_list=None, default_scheme=''):
151 151 """Create a table of color schemes.
152 152
153 153 The table can be created empty and manually filled or it can be
154 154 created with a list of valid color schemes AND the specification for
155 155 the default active scheme.
156 156 """
157 157
158 158 # create object attributes to be set later
159 159 self.active_scheme_name = ''
160 160 self.active_colors = None
161 161
162 162 if scheme_list:
163 163 if default_scheme == '':
164 164 raise ValueError('you must specify the default color scheme')
165 165 for scheme in scheme_list:
166 166 self.add_scheme(scheme)
167 167 self.set_active_scheme(default_scheme)
168 168
169 169 def copy(self):
170 170 """Return full copy of object"""
171 171 return ColorSchemeTable(self.values(),self.active_scheme_name)
172 172
173 173 def add_scheme(self,new_scheme):
174 174 """Add a new color scheme to the table."""
175 175 if not isinstance(new_scheme,ColorScheme):
176 176 raise ValueError('ColorSchemeTable only accepts ColorScheme instances')
177 177 self[new_scheme.name] = new_scheme
178 178
179 179 def set_active_scheme(self,scheme,case_sensitive=0):
180 180 """Set the currently active scheme.
181 181
182 182 Names are by default compared in a case-insensitive way, but this can
183 183 be changed by setting the parameter case_sensitive to true."""
184 184
185 185 scheme_names = list(self.keys())
186 186 if case_sensitive:
187 187 valid_schemes = scheme_names
188 188 scheme_test = scheme
189 189 else:
190 190 valid_schemes = [s.lower() for s in scheme_names]
191 191 scheme_test = scheme.lower()
192 192 try:
193 193 scheme_idx = valid_schemes.index(scheme_test)
194 194 except ValueError as e:
195 195 raise ValueError('Unrecognized color scheme: ' + scheme + \
196 196 '\nValid schemes: '+str(scheme_names).replace("'', ",'')) from e
197 197 else:
198 198 active = scheme_names[scheme_idx]
199 199 self.active_scheme_name = active
200 200 self.active_colors = self[active].colors
201 201 # Now allow using '' as an index for the current active scheme
202 202 self[''] = self[active]
General Comments 0
You need to be logged in to leave comments. Login now