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