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