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