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