##// END OF EJS Templates
utils: coloransi: Escape Unicode U0001 and U0002 non-printable characters....
Maxim Cournoyer -
Show More
@@ -1,187 +1,187 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 :class:`TermColors` and
52 52 :class`InputTermColors`.
53 53 """
54 54 for name,value in color_templates:
55 55 setattr(in_class,name,in_class._base % value)
56 56
57 57 class TermColors:
58 58 """Color escape sequences.
59 59
60 60 This class defines the escape sequences for all the standard (ANSI?)
61 61 colors in terminals. Also defines a NoColor escape which is just the null
62 62 string, suitable for defining 'dummy' color schemes in terminals which get
63 63 confused by color escapes.
64 64
65 65 This class should be used as a mixin for building color schemes."""
66 66
67 67 NoColor = '' # for color schemes in color-less terminals.
68 68 Normal = '\033[0m' # Reset normal coloring
69 69 _base = '\033[%sm' # Template for all other colors
70 70
71 71 # Build the actual color table as a set of class attributes:
72 72 make_color_table(TermColors)
73 73
74 74 class InputTermColors:
75 75 """Color escape sequences for input prompts.
76 76
77 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
77 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 79 can wrap lines accordingly. Use this class for any colored text which
80 80 needs to be used in input prompts, such as in calls to raw_input().
81 81
82 82 This class defines the escape sequences for all the standard (ANSI?)
83 83 colors in terminals. Also defines a NoColor escape which is just the null
84 84 string, suitable for defining 'dummy' color schemes in terminals which get
85 85 confused by color escapes.
86 86
87 87 This class should be used as a mixin for building color schemes."""
88 88
89 89 NoColor = '' # for color schemes in color-less terminals.
90 90
91 91 if os.name == 'nt' and os.environ.get('TERM','dumb') == 'emacs':
92 92 # (X)emacs on W32 gets confused with \001 and \002 so we remove them
93 93 Normal = '\033[0m' # Reset normal coloring
94 94 _base = '\033[%sm' # Template for all other colors
95 95 else:
96 96 Normal = '\001\033[0m\002' # Reset normal coloring
97 97 _base = '\001\033[%sm\002' # Template for all other colors
98 98
99 99 # Build the actual color table as a set of class attributes:
100 100 make_color_table(InputTermColors)
101 101
102 102 class NoColors:
103 103 """This defines all the same names as the colour classes, but maps them to
104 104 empty strings, so it can easily be substituted to turn off colours."""
105 105 NoColor = ''
106 106 Normal = ''
107 107
108 108 for name, value in color_templates:
109 109 setattr(NoColors, name, '')
110 110
111 111 class ColorScheme:
112 112 """Generic color scheme class. Just a name and a Struct."""
113 113 def __init__(self,__scheme_name_,colordict=None,**colormap):
114 114 self.name = __scheme_name_
115 115 if colordict is None:
116 116 self.colors = Struct(**colormap)
117 117 else:
118 118 self.colors = Struct(colordict)
119 119
120 120 def copy(self,name=None):
121 121 """Return a full copy of the object, optionally renaming it."""
122 122 if name is None:
123 123 name = self.name
124 124 return ColorScheme(name, self.colors.dict())
125 125
126 126 class ColorSchemeTable(dict):
127 127 """General class to handle tables of color schemes.
128 128
129 129 It's basically a dict of color schemes with a couple of shorthand
130 130 attributes and some convenient methods.
131 131
132 132 active_scheme_name -> obvious
133 133 active_colors -> actual color table of the active scheme"""
134 134
135 135 def __init__(self, scheme_list=None, default_scheme=''):
136 136 """Create a table of color schemes.
137 137
138 138 The table can be created empty and manually filled or it can be
139 139 created with a list of valid color schemes AND the specification for
140 140 the default active scheme.
141 141 """
142 142
143 143 # create object attributes to be set later
144 144 self.active_scheme_name = ''
145 145 self.active_colors = None
146 146
147 147 if scheme_list:
148 148 if default_scheme == '':
149 149 raise ValueError('you must specify the default color scheme')
150 150 for scheme in scheme_list:
151 151 self.add_scheme(scheme)
152 152 self.set_active_scheme(default_scheme)
153 153
154 154 def copy(self):
155 155 """Return full copy of object"""
156 156 return ColorSchemeTable(self.values(),self.active_scheme_name)
157 157
158 158 def add_scheme(self,new_scheme):
159 159 """Add a new color scheme to the table."""
160 160 if not isinstance(new_scheme,ColorScheme):
161 161 raise ValueError('ColorSchemeTable only accepts ColorScheme instances')
162 162 self[new_scheme.name] = new_scheme
163 163
164 164 def set_active_scheme(self,scheme,case_sensitive=0):
165 165 """Set the currently active scheme.
166 166
167 167 Names are by default compared in a case-insensitive way, but this can
168 168 be changed by setting the parameter case_sensitive to true."""
169 169
170 170 scheme_names = list(self.keys())
171 171 if case_sensitive:
172 172 valid_schemes = scheme_names
173 173 scheme_test = scheme
174 174 else:
175 175 valid_schemes = [s.lower() for s in scheme_names]
176 176 scheme_test = scheme.lower()
177 177 try:
178 178 scheme_idx = valid_schemes.index(scheme_test)
179 179 except ValueError as e:
180 180 raise ValueError('Unrecognized color scheme: ' + scheme + \
181 181 '\nValid schemes: '+str(scheme_names).replace("'', ",'')) from e
182 182 else:
183 183 active = scheme_names[scheme_idx]
184 184 self.active_scheme_name = active
185 185 self.active_colors = self[active].colors
186 186 # Now allow using '' as an index for the current active scheme
187 187 self[''] = self[active]
General Comments 0
You need to be logged in to leave comments. Login now