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