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