1
2
3
4
5
6
7
8
9
10
11
12 """Module containing classes to help with creating colour text.
13
14 Classes for text markup:
15
16 ANSIMarkup - apply ANSI codes to the text
17 NoMarkup - ingore colour codes and leave the text unchanged
18
19 ANSI code classes defined are:
20
21 Foreground - create object carrying ANSI codes for
22 changing text foreground colour;
23
24 Background - create object carrying ANSI codes for
25 changing text background colour;
26
27 Effects - create object carrying ANSI codes for
28 changing text effects.
29
30 Example usage is as follows:
31
32 fg = Foreground()
33 bg = Background()
34 fx = Effects()
35
36 if coloring_enabled:
37 # text will be blue by default and colour codes are applied
38 m = ANSIMarkup(fg.blue)
39 else:
40 # colour codes are ignored
41 m = NoMarkup()
42
43 # Text will be coloured in red if coloring_enabled,
44 # otherwise text will be unchanged.
45 print m('Text in red',code=fg.red)
46
47 It is a better practice to use markup objects to apply colours
48 because it is easier to enable/disable the markup if desired.
49
50 However inserting the codes directly also works:
51
52 # Print text in specified colour.
53 print fg.some_colour + 'Text in some_colour' + fx.normal
54
55 # Print text with colour of background changed as specified.
56 print bg.some_colour + 'Text with background in some_colour' + fx.normal
57
58 # Print text with specified effect applied.
59 print fx.some_effect + 'Text with some_effect applied' + fx.normal
60
61 Note that each ANSI code overrides the previous one, and their effect
62 isn't cumulative. Terminating a string with fx.normal in the above
63 examples ensures that subsequent text is output using the terminal
64 defaults.
65
66 For details of the colours and effects available, see help for
67 individual classes.
68 """
69
70 __author__ = "K.Harrison <Harrison@hep.phy.cam.ac.uk>"
71 __date__ = "24 August 2005"
72 __version__ = "1.0"
73
75 """ Apply ANSI colouring codes.
76 """
78 if default_code is None:
79 default_code = Effects().normal
80 self.default_code = default_code
81
83 if code is None:
84 code = self.default_code
85 return code+text+self.default_code
86
88 """ Leave text unchanged.
89 """
92
95
97
98 """Class for creating objects carrying ANSI codes for changing
99 text background colour. The defined colours are:
100
101 black, blue, cyan, green, orange, magenta, red, white.
102
103 In all cases, the text foregreound colour is the terminal
104 default (usually black or white)."""
105
107 """Set ANSI codes for defined colours"""
108
109 _base = '\033[%sm'
110 self.black = _base % "0;40"
111 self.red = _base % "0;41"
112 self.green = _base % "0;42"
113 self.orange = _base % "0;43"
114 self.blue = _base % "0;44"
115 self.magenta = _base % "0;45"
116 self.cyan = _base % "0;46"
117 self.white = _base % "0;47"
118
120
121 """Class for creating objects carrying ANSI codes for text
122 effects. The defined effects are:
123
124 normal,
125
126 bold, reverse, underline,
127
128 nobold, noreverse, nounderline.
129
130 All effects imply terminal defaults for the colours."""
131
133 """Set ANSI codes for defined effects"""
134 _base = '\033[%sm'
135 self.normal = _base % "0;0"
136 self.bold = _base % "0;1"
137 self.underline = _base % "0;4"
138 self.reverse = _base % "0;7"
139 self.nobold = _base % "0;21"
140 self.nounderline = _base % "0;24"
141 self.noreverse = _base % "0;27"
142
144 """Class for creating objects carrying ANSI codes for changing
145 text foreground colour. The defined colours are:
146
147 black, blue, cyan, green, orange, magenta, red, white,
148
149 boldblue, boldcyan, boldgreen, boldgreen, boldgrey,
150 boldmagenta, boldred, boldwhite, boldyellow.
151
152 For good visibility, the bold colours are better."""
153
155 """Set ANSI codes for defined colours"""
156
157 _base = '\033[%sm'
158 self.black = _base % "0;30"
159 self.red = _base % "0;31"
160 self.green = _base % "0;32"
161 self.orange = _base % "0;33"
162 self.blue = _base % "0;34"
163 self.magenta = _base % "0;35"
164 self.cyan = _base % "0;36"
165 self.white = _base % "0;37"
166 self.boldgrey = _base % "1;30"
167 self.boldred = _base % "1;31"
168 self.boldgreen = _base % "1;32"
169 self.boldyellow = _base % "1;33"
170 self.boldblue = _base % "1;34"
171 self.boldmagenta = _base % "1;35"
172 self.boldcyan = _base % "1;36"
173 self.boldwhite = _base % "1;37"
174
175
176 colour_objects = { 'fg' : Foreground(), 'bg' : Background(), 'fx' : Effects() }
177
179 """ Get a colour code from the symbolic name: fg = Foreground(), bg = Background(), fx = Effects()
180 The name examples fg.red, fx.normal, bg.white
181 Raise ValueError if name undefined or malformed.
182 """
183 x,y = name.split('.')
184 return getattr(colour_objects[x],y)
185
186 try:
187 x,y = name.split('.')
188 except Exception:
189 raise ValueError('unknown colour code %s'%str(name))
190
191 try:
192 return getattr(colour_objects[x],y)
193 except Exception:
194 raise ValueError('unknown colour code %s'%str(name))
195