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