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