Show More
@@ -0,0 +1,390 b'' | |||
|
1 | """ | |
|
2 | ``astyle`` provides classes for adding style (foreground and background color; | |
|
3 | bold; blink; etc.) to terminal and curses output. | |
|
4 | """ | |
|
5 | ||
|
6 | ||
|
7 | import os | |
|
8 | ||
|
9 | try: | |
|
10 | import curses | |
|
11 | except ImportError: | |
|
12 | curses = None | |
|
13 | ||
|
14 | ||
|
15 | COLOR_BLACK = 0 | |
|
16 | COLOR_RED = 1 | |
|
17 | COLOR_GREEN = 2 | |
|
18 | COLOR_YELLOW = 3 | |
|
19 | COLOR_BLUE = 4 | |
|
20 | COLOR_MAGENTA = 5 | |
|
21 | COLOR_CYAN = 6 | |
|
22 | COLOR_WHITE = 7 | |
|
23 | ||
|
24 | A_BLINK = 1<<0 # Blinking text | |
|
25 | A_BOLD = 1<<1 # Extra bright or bold text | |
|
26 | A_DIM = 1<<2 # Half bright text | |
|
27 | A_REVERSE = 1<<3 # Reverse-video text | |
|
28 | A_STANDOUT = 1<<4 # The best highlighting mode available | |
|
29 | A_UNDERLINE = 1<<5 # Underlined text | |
|
30 | ||
|
31 | ||
|
32 | class Style(object): | |
|
33 | """ | |
|
34 | Store foreground color, background color and attribute (bold, underlined | |
|
35 | etc.). | |
|
36 | """ | |
|
37 | __slots__ = ("fg", "bg", "attrs") | |
|
38 | ||
|
39 | COLORNAMES = { | |
|
40 | "black": COLOR_BLACK, | |
|
41 | "red": COLOR_RED, | |
|
42 | "green": COLOR_GREEN, | |
|
43 | "yellow": COLOR_YELLOW, | |
|
44 | "blue": COLOR_BLUE, | |
|
45 | "magenta": COLOR_MAGENTA, | |
|
46 | "cyan": COLOR_CYAN, | |
|
47 | "white": COLOR_WHITE, | |
|
48 | } | |
|
49 | ATTRNAMES = { | |
|
50 | "blink": A_BLINK, | |
|
51 | "bold": A_BOLD, | |
|
52 | "dim": A_DIM, | |
|
53 | "reverse": A_REVERSE, | |
|
54 | "standout": A_STANDOUT, | |
|
55 | "underline": A_UNDERLINE, | |
|
56 | } | |
|
57 | ||
|
58 | def __init__(self, fg, bg, attrs=0): | |
|
59 | """ | |
|
60 | Create a ``Style`` object with ``fg`` as the foreground color, | |
|
61 | ``bg`` as the background color and ``attrs`` as the attributes. | |
|
62 | ||
|
63 | Examples: | |
|
64 | ||
|
65 | >>> Style(COLOR_RED, COLOR_BLACK) | |
|
66 | >>> Style(COLOR_YELLOW, COLOR_BLUE, A_BOLD|A_UNDERLINE) | |
|
67 | """ | |
|
68 | self.fg = fg | |
|
69 | self.bg = bg | |
|
70 | self.attrs = attrs | |
|
71 | ||
|
72 | def __call__(self, *args): | |
|
73 | text = Text() | |
|
74 | for arg in args: | |
|
75 | if isinstance(arg, Text): | |
|
76 | text.extend(arg) | |
|
77 | else: | |
|
78 | text.append((self, arg)) | |
|
79 | return text | |
|
80 | ||
|
81 | def __eq__(self, other): | |
|
82 | return self.fg == other.fg and self.bg == other.bg and self.attrs == other.attrs | |
|
83 | ||
|
84 | def __neq__(self, other): | |
|
85 | return self.fg != other.fg or self.bg != other.bg or self.attrs != other.attrs | |
|
86 | ||
|
87 | def __repr__(self): | |
|
88 | color2name = ("black", "red", "green", "yellow", "blue", "magenta", "cyan", "white") | |
|
89 | attrs2name = ("blink", "bold", "dim", "reverse", "standout", "underline") | |
|
90 | ||
|
91 | return "<%s fg=%s bg=%s attrs=%s>" % ( | |
|
92 | self.__class__.__name__, color2name[self.fg], color2name[self.bg], | |
|
93 | "|".join([attrs2name[b] for b in xrange(6) if self.attrs&(1<<b)]) or 0) | |
|
94 | ||
|
95 | def fromstr(cls, value): | |
|
96 | """ | |
|
97 | Create a ``Style`` object from a string. The format looks like this: | |
|
98 | ``"red:black:bold|blink"``. | |
|
99 | """ | |
|
100 | # defaults | |
|
101 | fg = COLOR_WHITE | |
|
102 | bg = COLOR_BLACK | |
|
103 | attrs = 0 | |
|
104 | ||
|
105 | parts = value.split(":") | |
|
106 | if len(parts) > 0: | |
|
107 | fg = cls.COLORNAMES[parts[0].lower()] | |
|
108 | if len(parts) > 1: | |
|
109 | bg = cls.COLORNAMES[parts[1].lower()] | |
|
110 | if len(parts) > 2: | |
|
111 | for strattr in parts[2].split("|"): | |
|
112 | attrs |= cls.ATTRNAMES[strattr.lower()] | |
|
113 | return cls(fg, bg, attrs) | |
|
114 | fromstr = classmethod(fromstr) | |
|
115 | ||
|
116 | def fromenv(cls, name, default): | |
|
117 | """ | |
|
118 | Create a ``Style`` from an environment variable named ``name`` | |
|
119 | (using ``default`` if the environment variable doesn't exist). | |
|
120 | """ | |
|
121 | return cls.fromstr(os.environ.get(name, default)) | |
|
122 | fromenv = classmethod(fromenv) | |
|
123 | ||
|
124 | ||
|
125 | def switchstyle(s1, s2): | |
|
126 | """ | |
|
127 | Return the ANSI escape sequence needed to switch from style ``s1`` to | |
|
128 | style ``s2``. | |
|
129 | """ | |
|
130 | attrmask = (A_BLINK|A_BOLD|A_UNDERLINE|A_REVERSE) | |
|
131 | a1 = s1.attrs & attrmask | |
|
132 | a2 = s2.attrs & attrmask | |
|
133 | ||
|
134 | args = [] | |
|
135 | if s1 != s2: | |
|
136 | # do we have to get rid of the bold/underline/blink bit? | |
|
137 | # (can only be done by a reset) | |
|
138 | # use reset when our target color is the default color | |
|
139 | # (this is shorter than 37;40) | |
|
140 | if (a1 & ~a2 or s2==style_default): | |
|
141 | args.append("0") | |
|
142 | s1 = style_default | |
|
143 | a1 = 0 | |
|
144 | ||
|
145 | # now we know that old and new color have the same boldness, | |
|
146 | # or the new color is bold and the old isn't, | |
|
147 | # i.e. we only might have to switch bold on, not off | |
|
148 | if not (a1 & A_BOLD) and (a2 & A_BOLD): | |
|
149 | args.append("1") | |
|
150 | ||
|
151 | # Fix underline | |
|
152 | if not (a1 & A_UNDERLINE) and (a2 & A_UNDERLINE): | |
|
153 | args.append("4") | |
|
154 | ||
|
155 | # Fix blink | |
|
156 | if not (a1 & A_BLINK) and (a2 & A_BLINK): | |
|
157 | args.append("5") | |
|
158 | ||
|
159 | # Fix reverse | |
|
160 | if not (a1 & A_REVERSE) and (a2 & A_REVERSE): | |
|
161 | args.append("7") | |
|
162 | ||
|
163 | # Fix foreground color | |
|
164 | if s1.fg != s2.fg: | |
|
165 | args.append("3%d" % s2.fg) | |
|
166 | ||
|
167 | # Finally fix the background color | |
|
168 | if s1.bg != s2.bg: | |
|
169 | args.append("4%d" % s2.bg) | |
|
170 | ||
|
171 | if args: | |
|
172 | return "\033[%sm" % ";".join(args) | |
|
173 | return "" | |
|
174 | ||
|
175 | ||
|
176 | class Text(list): | |
|
177 | """ | |
|
178 | A colored string. A ``Text`` object is a sequence, the sequence | |
|
179 | items will be ``(style, string)`` tuples. | |
|
180 | """ | |
|
181 | ||
|
182 | def __init__(self, *args): | |
|
183 | list.__init__(self) | |
|
184 | self.append(*args) | |
|
185 | ||
|
186 | def __repr__(self): | |
|
187 | return "%s.%s(%s)" % ( | |
|
188 | self.__class__.__module__, self.__class__.__name__, | |
|
189 | list.__repr__(self)[1:-1]) | |
|
190 | ||
|
191 | def append(self, *args): | |
|
192 | for arg in args: | |
|
193 | if isinstance(arg, Text): | |
|
194 | self.extend(arg) | |
|
195 | elif isinstance(arg, tuple): # must be (style, string) | |
|
196 | list.append(self, arg) | |
|
197 | elif isinstance(arg, unicode): | |
|
198 | list.append(self, (style_default, arg)) | |
|
199 | else: | |
|
200 | list.append(self, (style_default, str(arg))) | |
|
201 | ||
|
202 | def insert(self, index, *args): | |
|
203 | self[index:index] = Text(*args) | |
|
204 | ||
|
205 | def __add__(self, other): | |
|
206 | new = Text() | |
|
207 | new.append(self) | |
|
208 | new.append(other) | |
|
209 | return new | |
|
210 | ||
|
211 | def __iadd__(self, other): | |
|
212 | self.append(other) | |
|
213 | return self | |
|
214 | ||
|
215 | def format(self, styled=True): | |
|
216 | """ | |
|
217 | This generator yields the strings that will make up the final | |
|
218 | colorized string. | |
|
219 | """ | |
|
220 | if styled: | |
|
221 | oldstyle = style_default | |
|
222 | for (style, string) in self: | |
|
223 | if not isinstance(style, (int, long)): | |
|
224 | switch = switchstyle(oldstyle, style) | |
|
225 | if switch: | |
|
226 | yield switch | |
|
227 | if string: | |
|
228 | yield string | |
|
229 | oldstyle = style | |
|
230 | switch = switchstyle(oldstyle, style_default) | |
|
231 | if switch: | |
|
232 | yield switch | |
|
233 | else: | |
|
234 | for (style, string) in self: | |
|
235 | if not isinstance(style, (int, long)): | |
|
236 | yield string | |
|
237 | ||
|
238 | def string(self, styled=True): | |
|
239 | """ | |
|
240 | Return the resulting string (with escape sequences, if ``styled`` | |
|
241 | is true). | |
|
242 | """ | |
|
243 | return "".join(self.format(styled)) | |
|
244 | ||
|
245 | def __str__(self): | |
|
246 | """ | |
|
247 | Return ``self`` as a string (without ANSI escape sequences). | |
|
248 | """ | |
|
249 | return self.string(False) | |
|
250 | ||
|
251 | def write(self, stream, styled=True): | |
|
252 | """ | |
|
253 | Write ``self`` to the output stream ``stream`` (with escape sequences, | |
|
254 | if ``styled`` is true). | |
|
255 | """ | |
|
256 | for part in self.format(styled): | |
|
257 | stream.write(part) | |
|
258 | ||
|
259 | def __xrepr__(self, mode="default"): | |
|
260 | yield (-1, True) | |
|
261 | for info in self: | |
|
262 | yield info | |
|
263 | ||
|
264 | ||
|
265 | def streamstyle(stream, styled=None): | |
|
266 | """ | |
|
267 | If ``styled`` is ``None``, return whether ``stream`` refers to a terminal. | |
|
268 | If this can't be determined (either because ``stream`` doesn't refer to a | |
|
269 | real OS file, or because you're on Windows) return ``False``. If ``styled`` | |
|
270 | is not ``None`` ``styled`` will be returned unchanged. | |
|
271 | """ | |
|
272 | if styled is None: | |
|
273 | try: | |
|
274 | styled = os.isatty(stream.fileno()) | |
|
275 | except (KeyboardInterrupt, SystemExit): | |
|
276 | raise | |
|
277 | except Exception: | |
|
278 | styled = False | |
|
279 | return styled | |
|
280 | ||
|
281 | ||
|
282 | def write(stream, styled, *texts): | |
|
283 | """ | |
|
284 | Write ``texts`` to ``stream``. | |
|
285 | """ | |
|
286 | text = Text(*texts) | |
|
287 | text.write(stream, streamstyle(stream, styled)) | |
|
288 | ||
|
289 | ||
|
290 | def writeln(stream, styled, *texts): | |
|
291 | """ | |
|
292 | Write ``texts`` to ``stream`` and finish with a line feed. | |
|
293 | """ | |
|
294 | write(stream, styled, *texts) | |
|
295 | stream.write("\n") | |
|
296 | ||
|
297 | ||
|
298 | class Stream(object): | |
|
299 | """ | |
|
300 | Stream wrapper that adds color output. | |
|
301 | """ | |
|
302 | def __init__(self, stream, styled=None): | |
|
303 | self.stream = stream | |
|
304 | self.styled = streamstyle(stream, styled) | |
|
305 | ||
|
306 | def write(self, *texts): | |
|
307 | write(self.stream, self.styled, *texts) | |
|
308 | ||
|
309 | def writeln(self, *texts): | |
|
310 | writeln(self.stream, self.styled, *texts) | |
|
311 | ||
|
312 | def __getattr__(self, name): | |
|
313 | return getattr(self.stream, name) | |
|
314 | ||
|
315 | ||
|
316 | class stdout(object): | |
|
317 | """ | |
|
318 | Stream wrapper for ``sys.stdout`` that adds color output. | |
|
319 | """ | |
|
320 | def write(self, *texts): | |
|
321 | write(sys.stdout, None, *texts) | |
|
322 | ||
|
323 | def writeln(self, *texts): | |
|
324 | writeln(sys.stdout, None, *texts) | |
|
325 | ||
|
326 | def __getattr__(self, name): | |
|
327 | return getattr(sys.stdout, name) | |
|
328 | stdout = stdout() | |
|
329 | ||
|
330 | ||
|
331 | class stderr(object): | |
|
332 | """ | |
|
333 | Stream wrapper for ``sys.stderr`` that adds color output. | |
|
334 | """ | |
|
335 | def write(self, *texts): | |
|
336 | write(sys.stderr, None, *texts) | |
|
337 | ||
|
338 | def writeln(self, *texts): | |
|
339 | writeln(sys.stderr, None, *texts) | |
|
340 | ||
|
341 | def __getattr__(self, name): | |
|
342 | return getattr(sys.stdout, name) | |
|
343 | stderr = stderr() | |
|
344 | ||
|
345 | ||
|
346 | if curses is not None: | |
|
347 | # This is probably just range(8) | |
|
348 | COLOR2CURSES = [ | |
|
349 | COLOR_BLACK, | |
|
350 | COLOR_RED, | |
|
351 | COLOR_GREEN, | |
|
352 | COLOR_YELLOW, | |
|
353 | COLOR_BLUE, | |
|
354 | COLOR_MAGENTA, | |
|
355 | COLOR_CYAN, | |
|
356 | COLOR_WHITE, | |
|
357 | ] | |
|
358 | ||
|
359 | A2CURSES = { | |
|
360 | A_BLINK: curses.A_BLINK, | |
|
361 | A_BOLD: curses.A_BOLD, | |
|
362 | A_DIM: curses.A_DIM, | |
|
363 | A_REVERSE: curses.A_REVERSE, | |
|
364 | A_STANDOUT: curses.A_STANDOUT, | |
|
365 | A_UNDERLINE: curses.A_UNDERLINE, | |
|
366 | } | |
|
367 | ||
|
368 | ||
|
369 | # default style | |
|
370 | style_default = Style.fromstr("white:black") | |
|
371 | ||
|
372 | # Styles for datatypes | |
|
373 | style_type_none = Style.fromstr("magenta:black") | |
|
374 | style_type_bool = Style.fromstr("magenta:black") | |
|
375 | style_type_number = Style.fromstr("yellow:black") | |
|
376 | style_type_datetime = Style.fromstr("magenta:black") | |
|
377 | ||
|
378 | # Style for URLs and file/directory names | |
|
379 | style_url = Style.fromstr("green:black") | |
|
380 | style_dir = Style.fromstr("cyan:black") | |
|
381 | style_file = Style.fromstr("green:black") | |
|
382 | ||
|
383 | # Style for ellipsis (when an output has been shortened | |
|
384 | style_ellisis = Style.fromstr("red:black") | |
|
385 | ||
|
386 | # Style for displaying exceptions | |
|
387 | style_error = Style.fromstr("red:black") | |
|
388 | ||
|
389 | # Style for displaying non-existing attributes | |
|
390 | style_nodata = Style.fromstr("red:black") |
This diff has been collapsed as it changes many lines, (562 lines changed) Show them Hide them | |||
@@ -57,11 +57,11 b' three extensions points (all of them optional):' | |||
|
57 | 57 | line would use ``True``, but for a large data structure (i.e. a nested list, |
|
58 | 58 | tuple or dictionary) ``False`` would be used). The other output ``__xrepr__()`` |
|
59 | 59 | may produce is tuples of ``Style```objects and text (which contain the text |
|
60 |
representation of the object). If ``__xrepr__()`` |
|
|
61 |
structure the function ``xrepr(object, mode)`` can |
|
|
62 |
must be passed as the mode in these calls. This in |
|
|
63 |
``__xrepr__()`` method on ``object`` (or uses ``repr(object)`` |
|
|
64 | representation if ``__xrepr__()`` doesn't exist. | |
|
60 | representation of the object; see the ``astyle`` module). If ``__xrepr__()`` | |
|
61 | recursively outputs a data structure the function ``xrepr(object, mode)`` can | |
|
62 | be used and ``"default"`` must be passed as the mode in these calls. This in | |
|
63 | turn calls the ``__xrepr__()`` method on ``object`` (or uses ``repr(object)`` | |
|
64 | as the string representation if ``__xrepr__()`` doesn't exist). | |
|
65 | 65 | |
|
66 | 66 | * Objects that can be iterated by ``Pipe``s must implement the method |
|
67 | 67 | ``__xiter__(self, mode)``. ``mode`` can take the following values: |
@@ -163,6 +163,8 b' try:' | |||
|
163 | 163 | except ImportError: |
|
164 | 164 | pass |
|
165 | 165 | |
|
166 | import astyle | |
|
167 | ||
|
166 | 168 | |
|
167 | 169 | __all__ = [ |
|
168 | 170 | "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp", |
@@ -379,299 +381,6 b' def _attrname(name):' | |||
|
379 | 381 | return str(name) |
|
380 | 382 | |
|
381 | 383 | |
|
382 | COLOR_BLACK = 0 | |
|
383 | COLOR_RED = 1 | |
|
384 | COLOR_GREEN = 2 | |
|
385 | COLOR_YELLOW = 3 | |
|
386 | COLOR_BLUE = 4 | |
|
387 | COLOR_MAGENTA = 5 | |
|
388 | COLOR_CYAN = 6 | |
|
389 | COLOR_WHITE = 7 | |
|
390 | ||
|
391 | A_BLINK = 1<<0 # Blinking text | |
|
392 | A_BOLD = 1<<1 # Extra bright or bold text | |
|
393 | A_DIM = 1<<2 # Half bright text | |
|
394 | A_REVERSE = 1<<3 # Reverse-video text | |
|
395 | A_STANDOUT = 1<<4 # The best highlighting mode available | |
|
396 | A_UNDERLINE = 1<<5 # Underlined text | |
|
397 | ||
|
398 | ||
|
399 | class Style(object): | |
|
400 | """ | |
|
401 | Store foreground color, background color and attribute (bold, underlined | |
|
402 | etc.). | |
|
403 | """ | |
|
404 | __slots__ = ("fg", "bg", "attrs") | |
|
405 | ||
|
406 | COLORNAMES = { | |
|
407 | "black": COLOR_BLACK, | |
|
408 | "red": COLOR_RED, | |
|
409 | "green": COLOR_GREEN, | |
|
410 | "yellow": COLOR_YELLOW, | |
|
411 | "blue": COLOR_BLUE, | |
|
412 | "magenta": COLOR_MAGENTA, | |
|
413 | "cyan": COLOR_CYAN, | |
|
414 | "white": COLOR_WHITE, | |
|
415 | } | |
|
416 | ATTRNAMES = { | |
|
417 | "blink": A_BLINK, | |
|
418 | "bold": A_BOLD, | |
|
419 | "dim": A_DIM, | |
|
420 | "reverse": A_REVERSE, | |
|
421 | "standout": A_STANDOUT, | |
|
422 | "underline": A_UNDERLINE, | |
|
423 | } | |
|
424 | ||
|
425 | def __init__(self, fg, bg, attrs=0): | |
|
426 | """ | |
|
427 | Create a ``Style`` object with ``fg`` as the foreground color, | |
|
428 | ``bg`` as the background color and ``attrs`` as the attributes. | |
|
429 | ||
|
430 | Examples: | |
|
431 | ||
|
432 | >>> Style(COLOR_RED, COLOR_BLACK) | |
|
433 | >>> Style(COLOR_YELLOW, COLOR_BLUE, A_BOLD|A_UNDERLINE) | |
|
434 | """ | |
|
435 | self.fg = fg | |
|
436 | self.bg = bg | |
|
437 | self.attrs = attrs | |
|
438 | ||
|
439 | def __call__(self, *args): | |
|
440 | text = Text() | |
|
441 | for arg in args: | |
|
442 | if isinstance(arg, Text): | |
|
443 | text.extend(arg) | |
|
444 | else: | |
|
445 | text.append((self, arg)) | |
|
446 | return text | |
|
447 | ||
|
448 | def __eq__(self, other): | |
|
449 | return self.fg == other.fg and self.bg == other.bg and self.attrs == other.attrs | |
|
450 | ||
|
451 | def __neq__(self, other): | |
|
452 | return self.fg != other.fg or self.bg != other.bg or self.attrs != other.attrs | |
|
453 | ||
|
454 | def __repr__(self): | |
|
455 | color2name = ("black", "red", "green", "yellow", "blue", "magenta", "cyan", "white") | |
|
456 | attrs2name = ("blink", "bold", "dim", "reverse", "standout", "underline") | |
|
457 | ||
|
458 | return "<%s fg=%s bg=%s attrs=%s>" % ( | |
|
459 | self.__class__.__name__, color2name[self.fg], color2name[self.bg], | |
|
460 | "|".join([attrs2name[b] for b in xrange(6) if self.attrs&(1<<b)]) or 0) | |
|
461 | ||
|
462 | def fromstr(cls, value): | |
|
463 | """ | |
|
464 | Create a ``Style`` object from a string. The format looks like this: | |
|
465 | ``"red:black:bold|blink"``. | |
|
466 | """ | |
|
467 | # defaults | |
|
468 | fg = COLOR_WHITE | |
|
469 | bg = COLOR_BLACK | |
|
470 | attrs = 0 | |
|
471 | ||
|
472 | parts = value.split(":") | |
|
473 | if len(parts) > 0: | |
|
474 | fg = cls.COLORNAMES[parts[0].lower()] | |
|
475 | if len(parts) > 1: | |
|
476 | bg = cls.COLORNAMES[parts[1].lower()] | |
|
477 | if len(parts) > 2: | |
|
478 | for strattr in parts[2].split("|"): | |
|
479 | attrs |= cls.ATTRNAMES[strattr.lower()] | |
|
480 | return cls(fg, bg, attrs) | |
|
481 | fromstr = classmethod(fromstr) | |
|
482 | ||
|
483 | def fromenv(cls, name, default): | |
|
484 | """ | |
|
485 | Create a ``Style`` from an environment variable named ``name`` | |
|
486 | (using ``default`` if the environment variable doesn't exist). | |
|
487 | """ | |
|
488 | return cls.fromstr(os.environ.get(name, default)) | |
|
489 | fromenv = classmethod(fromenv) | |
|
490 | ||
|
491 | ||
|
492 | def switchstyle(s1, s2): | |
|
493 | """ | |
|
494 | Return the ANSI escape sequence needed to switch from style ``s1`` to | |
|
495 | style ``s2``. | |
|
496 | """ | |
|
497 | attrmask = (A_BLINK|A_BOLD|A_UNDERLINE|A_REVERSE) | |
|
498 | a1 = s1.attrs & attrmask | |
|
499 | a2 = s2.attrs & attrmask | |
|
500 | ||
|
501 | args = [] | |
|
502 | if s1 != s2: | |
|
503 | # do we have to get rid of the bold/underline/blink bit? | |
|
504 | # (can only be done by a reset) | |
|
505 | # use reset when our target color is the default color | |
|
506 | # (this is shorter than 37;40) | |
|
507 | if (a1 & ~a2 or s2==style_default): | |
|
508 | args.append("0") | |
|
509 | s1 = style_default | |
|
510 | a1 = 0 | |
|
511 | ||
|
512 | # now we know that old and new color have the same boldness, | |
|
513 | # or the new color is bold and the old isn't, | |
|
514 | # i.e. we only might have to switch bold on, not off | |
|
515 | if not (a1 & A_BOLD) and (a2 & A_BOLD): | |
|
516 | args.append("1") | |
|
517 | ||
|
518 | # Fix underline | |
|
519 | if not (a1 & A_UNDERLINE) and (a2 & A_UNDERLINE): | |
|
520 | args.append("4") | |
|
521 | ||
|
522 | # Fix blink | |
|
523 | if not (a1 & A_BLINK) and (a2 & A_BLINK): | |
|
524 | args.append("5") | |
|
525 | ||
|
526 | # Fix reverse | |
|
527 | if not (a1 & A_REVERSE) and (a2 & A_REVERSE): | |
|
528 | args.append("7") | |
|
529 | ||
|
530 | # Fix foreground color | |
|
531 | if s1.fg != s2.fg: | |
|
532 | args.append("3%d" % s2.fg) | |
|
533 | ||
|
534 | # Finally fix the background color | |
|
535 | if s1.bg != s2.bg: | |
|
536 | args.append("4%d" % s2.bg) | |
|
537 | ||
|
538 | if args: | |
|
539 | return "\033[%sm" % ";".join(args) | |
|
540 | return "" | |
|
541 | ||
|
542 | ||
|
543 | class Text(list): | |
|
544 | """ | |
|
545 | A colored string. A ``Text`` object is a sequence, the sequence | |
|
546 | items will be ``(style, string)`` tuples. | |
|
547 | """ | |
|
548 | ||
|
549 | def __init__(self, *args): | |
|
550 | list.__init__(self) | |
|
551 | self.append(*args) | |
|
552 | ||
|
553 | def __repr__(self): | |
|
554 | return "%s.%s(%s)" % ( | |
|
555 | self.__class__.__module__, self.__class__.__name__, | |
|
556 | list.__repr__(self)[1:-1]) | |
|
557 | ||
|
558 | def append(self, *args): | |
|
559 | for arg in args: | |
|
560 | if isinstance(arg, Text): | |
|
561 | self.extend(arg) | |
|
562 | elif isinstance(arg, tuple): # must be (style, string) | |
|
563 | list.append(self, arg) | |
|
564 | elif isinstance(arg, unicode): | |
|
565 | list.append(self, (style_default, arg)) | |
|
566 | else: | |
|
567 | list.append(self, (style_default, str(arg))) | |
|
568 | ||
|
569 | def insert(self, index, *args): | |
|
570 | self[index:index] = Text(*args) | |
|
571 | ||
|
572 | def __add__(self, other): | |
|
573 | new = Text() | |
|
574 | new.append(self) | |
|
575 | new.append(other) | |
|
576 | return new | |
|
577 | ||
|
578 | def __iadd__(self, other): | |
|
579 | self.append(other) | |
|
580 | return self | |
|
581 | ||
|
582 | def format(self, styled=True): | |
|
583 | """ | |
|
584 | This generator yields the strings that will make up the final | |
|
585 | colorized string. | |
|
586 | """ | |
|
587 | if styled: | |
|
588 | oldstyle = style_default | |
|
589 | for (style, string) in self: | |
|
590 | if not isinstance(style, (int, long)): | |
|
591 | switch = switchstyle(oldstyle, style) | |
|
592 | if switch: | |
|
593 | yield switch | |
|
594 | if string: | |
|
595 | yield string | |
|
596 | oldstyle = style | |
|
597 | switch = switchstyle(oldstyle, style_default) | |
|
598 | if switch: | |
|
599 | yield switch | |
|
600 | else: | |
|
601 | for (style, string) in self: | |
|
602 | if not isinstance(style, (int, long)): | |
|
603 | yield string | |
|
604 | ||
|
605 | def string(self, styled=True): | |
|
606 | """ | |
|
607 | Return the resulting string (with escape sequences, if ``styled`` | |
|
608 | is true). | |
|
609 | """ | |
|
610 | return "".join(self.format(styled)) | |
|
611 | ||
|
612 | def __str__(self): | |
|
613 | """ | |
|
614 | Return the resulting string with ANSI escape sequences. | |
|
615 | """ | |
|
616 | return self.string(False) | |
|
617 | ||
|
618 | def write(self, stream, styled=True): | |
|
619 | for part in self.format(styled): | |
|
620 | stream.write(part) | |
|
621 | ||
|
622 | def __xrepr__(self, mode="default"): | |
|
623 | yield (-1, True) | |
|
624 | for info in self: | |
|
625 | yield info | |
|
626 | ||
|
627 | ||
|
628 | if curses is not None: | |
|
629 | # This is probably just range(8) | |
|
630 | COLOR2CURSES = [ | |
|
631 | COLOR_BLACK, | |
|
632 | COLOR_RED, | |
|
633 | COLOR_GREEN, | |
|
634 | COLOR_YELLOW, | |
|
635 | COLOR_BLUE, | |
|
636 | COLOR_MAGENTA, | |
|
637 | COLOR_CYAN, | |
|
638 | COLOR_WHITE, | |
|
639 | ] | |
|
640 | ||
|
641 | A2CURSES = { | |
|
642 | A_BLINK: curses.A_BLINK, | |
|
643 | A_BOLD: curses.A_BOLD, | |
|
644 | A_DIM: curses.A_DIM, | |
|
645 | A_REVERSE: curses.A_REVERSE, | |
|
646 | A_STANDOUT: curses.A_STANDOUT, | |
|
647 | A_UNDERLINE: curses.A_UNDERLINE, | |
|
648 | } | |
|
649 | ||
|
650 | ||
|
651 | # default style | |
|
652 | style_default = Style(COLOR_WHITE, COLOR_BLACK) | |
|
653 | ||
|
654 | # Styles for datatypes | |
|
655 | style_type_none = Style(COLOR_MAGENTA, COLOR_BLACK) | |
|
656 | style_type_bool = Style(COLOR_MAGENTA, COLOR_BLACK) | |
|
657 | style_type_number = Style(COLOR_YELLOW, COLOR_BLACK) | |
|
658 | style_type_datetime = Style(COLOR_MAGENTA, COLOR_BLACK) | |
|
659 | ||
|
660 | # Style for URLs and file/directory names | |
|
661 | style_url = Style(COLOR_GREEN, COLOR_BLACK) | |
|
662 | style_dir = Style(COLOR_CYAN, COLOR_BLACK) | |
|
663 | style_file = Style(COLOR_GREEN, COLOR_BLACK) | |
|
664 | ||
|
665 | # Style for ellipsis (when an output has been shortened | |
|
666 | style_ellisis = Style(COLOR_RED, COLOR_BLACK) | |
|
667 | ||
|
668 | # Style for displaying exceptions | |
|
669 | style_error = Style(COLOR_RED, COLOR_BLACK) | |
|
670 | ||
|
671 | # Style for displaying non-existing attributes | |
|
672 | style_nodata = Style(COLOR_RED, COLOR_BLACK) | |
|
673 | ||
|
674 | ||
|
675 | 384 | def xrepr(item, mode): |
|
676 | 385 | try: |
|
677 | 386 | func = item.__xrepr__ |
@@ -685,62 +394,62 b' def xrepr(item, mode):' | |||
|
685 | 394 | raise |
|
686 | 395 | except Exception: |
|
687 | 396 | yield (-1, True) |
|
688 | yield (style_default, repr(item)) | |
|
397 | yield (astyle.style_default, repr(item)) | |
|
689 | 398 | return |
|
690 | 399 | if item is None: |
|
691 | 400 | yield (-1, True) |
|
692 | yield (style_type_none, repr(item)) | |
|
401 | yield (astyle.style_type_none, repr(item)) | |
|
693 | 402 | elif isinstance(item, bool): |
|
694 | 403 | yield (-1, True) |
|
695 | yield (style_type_bool, repr(item)) | |
|
404 | yield (astyle.style_type_bool, repr(item)) | |
|
696 | 405 | elif isinstance(item, str): |
|
697 | 406 | yield (-1, True) |
|
698 | 407 | if mode == "cell": |
|
699 | yield (style_default, repr(item.expandtabs(tab))[1:-1]) | |
|
408 | yield (astyle.style_default, repr(item.expandtabs(tab))[1:-1]) | |
|
700 | 409 | else: |
|
701 | yield (style_default, repr(item)) | |
|
410 | yield (astyle.style_default, repr(item)) | |
|
702 | 411 | elif isinstance(item, unicode): |
|
703 | 412 | yield (-1, True) |
|
704 | 413 | if mode == "cell": |
|
705 | yield (style_default, repr(item.expandtabs(tab))[2:-1]) | |
|
414 | yield (astyle.style_default, repr(item.expandtabs(tab))[2:-1]) | |
|
706 | 415 | else: |
|
707 | yield (style_default, repr(item)) | |
|
416 | yield (astyle.style_default, repr(item)) | |
|
708 | 417 | elif isinstance(item, (int, long, float)): |
|
709 | 418 | yield (1, True) |
|
710 | yield (style_type_number, repr(item)) | |
|
419 | yield (astyle.style_type_number, repr(item)) | |
|
711 | 420 | elif isinstance(item, complex): |
|
712 | 421 | yield (-1, True) |
|
713 | yield (style_type_number, repr(item)) | |
|
422 | yield (astyle.style_type_number, repr(item)) | |
|
714 | 423 | elif isinstance(item, datetime.datetime): |
|
715 | 424 | yield (-1, True) |
|
716 | 425 | if mode == "cell": |
|
717 | 426 | # Don't use strftime() here, as this requires year >= 1900 |
|
718 | yield (style_type_datetime, | |
|
427 | yield (astyle.style_type_datetime, | |
|
719 | 428 | "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \ |
|
720 | 429 | (item.year, item.month, item.day, |
|
721 | 430 | item.hour, item.minute, item.second, |
|
722 | 431 | item.microsecond), |
|
723 | 432 | ) |
|
724 | 433 | else: |
|
725 | yield (style_type_datetime, repr(item)) | |
|
434 | yield (astyle.style_type_datetime, repr(item)) | |
|
726 | 435 | elif isinstance(item, datetime.date): |
|
727 | 436 | yield (-1, True) |
|
728 | 437 | if mode == "cell": |
|
729 | yield (style_type_datetime, | |
|
438 | yield (astyle.style_type_datetime, | |
|
730 | 439 | "%04d-%02d-%02d" % (item.year, item.month, item.day)) |
|
731 | 440 | else: |
|
732 | yield (style_type_datetime, repr(item)) | |
|
441 | yield (astyle.style_type_datetime, repr(item)) | |
|
733 | 442 | elif isinstance(item, datetime.time): |
|
734 | 443 | yield (-1, True) |
|
735 | 444 | if mode == "cell": |
|
736 | yield (style_type_datetime, | |
|
445 | yield (astyle.style_type_datetime, | |
|
737 | 446 | "%02d:%02d:%02d.%06d" % \ |
|
738 | 447 | (item.hour, item.minute, item.second, item.microsecond)) |
|
739 | 448 | else: |
|
740 | yield (style_type_datetime, repr(item)) | |
|
449 | yield (astyle.style_type_datetime, repr(item)) | |
|
741 | 450 | elif isinstance(item, datetime.timedelta): |
|
742 | 451 | yield (-1, True) |
|
743 | yield (style_type_datetime, repr(item)) | |
|
452 | yield (astyle.style_type_datetime, repr(item)) | |
|
744 | 453 | elif isinstance(item, Exception): |
|
745 | 454 | yield (-1, True) |
|
746 | 455 | if item.__class__.__module__ == "exceptions": |
@@ -749,9 +458,9 b' def xrepr(item, mode):' | |||
|
749 | 458 | classname = "%s.%s" % \ |
|
750 | 459 | (item.__class__.__module__, item.__class__.__name__) |
|
751 | 460 | if mode == "header" or mode == "footer": |
|
752 | yield (style_error, "%s: %s" % (classname, item)) | |
|
461 | yield (astyle.style_error, "%s: %s" % (classname, item)) | |
|
753 | 462 | else: |
|
754 | yield (style_error, classname) | |
|
463 | yield (astyle.style_error, classname) | |
|
755 | 464 | elif isinstance(item, (list, tuple)): |
|
756 | 465 | yield (-1, False) |
|
757 | 466 | if mode == "header" or mode == "footer": |
@@ -760,22 +469,22 b' def xrepr(item, mode):' | |||
|
760 | 469 | else: |
|
761 | 470 | classname = "%s.%s" % \ |
|
762 | 471 | (item.__class__.__module__,item.__class__.__name__) |
|
763 | yield (style_default, | |
|
472 | yield (astyle.style_default, | |
|
764 | 473 | "<%s object with %d items at 0x%x>" % \ |
|
765 | 474 | (classname, len(item), id(item))) |
|
766 | 475 | else: |
|
767 | 476 | if isinstance(item, list): |
|
768 | yield (style_default, "[") | |
|
477 | yield (astyle.style_default, "[") | |
|
769 | 478 | end = "]" |
|
770 | 479 | else: |
|
771 | yield (style_default, "(") | |
|
480 | yield (astyle.style_default, "(") | |
|
772 | 481 | end = ")" |
|
773 | 482 | for (i, subitem) in enumerate(item): |
|
774 | 483 | if i: |
|
775 | yield (style_default, ", ") | |
|
484 | yield (astyle.style_default, ", ") | |
|
776 | 485 | for part in xrepr(subitem, "default"): |
|
777 | 486 | yield part |
|
778 | yield (style_default, end) | |
|
487 | yield (astyle.style_default, end) | |
|
779 | 488 | elif isinstance(item, (dict, types.DictProxyType)): |
|
780 | 489 | yield (-1, False) |
|
781 | 490 | if mode == "header" or mode == "footer": |
@@ -784,28 +493,28 b' def xrepr(item, mode):' | |||
|
784 | 493 | else: |
|
785 | 494 | classname = "%s.%s" % \ |
|
786 | 495 | (item.__class__.__module__,item.__class__.__name__) |
|
787 | yield (style_default, | |
|
496 | yield (astyle.style_default, | |
|
788 | 497 | "<%s object with %d items at 0x%x>" % \ |
|
789 | 498 | (classname, len(item), id(item))) |
|
790 | 499 | else: |
|
791 | 500 | if isinstance(item, dict): |
|
792 | yield (style_default, "{") | |
|
501 | yield (astyle.style_default, "{") | |
|
793 | 502 | end = "}" |
|
794 | 503 | else: |
|
795 | yield (style_default, "dictproxy((") | |
|
504 | yield (astyle.style_default, "dictproxy((") | |
|
796 | 505 | end = "})" |
|
797 | 506 | for (i, (key, value)) in enumerate(item.iteritems()): |
|
798 | 507 | if i: |
|
799 | yield (style_default, ", ") | |
|
508 | yield (astyle.style_default, ", ") | |
|
800 | 509 | for part in xrepr(key, "default"): |
|
801 | 510 | yield part |
|
802 | yield (style_default, ": ") | |
|
511 | yield (astyle.style_default, ": ") | |
|
803 | 512 | for part in xrepr(value, "default"): |
|
804 | 513 | yield part |
|
805 | yield (style_default, end) | |
|
514 | yield (astyle.style_default, end) | |
|
806 | 515 | else: |
|
807 | 516 | yield (-1, True) |
|
808 | yield (style_default, repr(item)) | |
|
517 | yield (astyle.style_default, repr(item)) | |
|
809 | 518 | |
|
810 | 519 | |
|
811 | 520 | def xattrs(item, mode): |
@@ -874,15 +583,15 b' class ichain(Pipe):' | |||
|
874 | 583 | if mode == "header" or mode == "footer": |
|
875 | 584 | for (i, item) in enumerate(self.iters): |
|
876 | 585 | if i: |
|
877 | yield (style_default, "+") | |
|
586 | yield (astyle.style_default, "+") | |
|
878 | 587 | if isinstance(item, Pipe): |
|
879 | yield (style_default, "(") | |
|
588 | yield (astyle.style_default, "(") | |
|
880 | 589 | for part in xrepr(item, mode): |
|
881 | 590 | yield part |
|
882 | 591 | if isinstance(item, Pipe): |
|
883 | yield (style_default, ")") | |
|
592 | yield (astyle.style_default, ")") | |
|
884 | 593 | else: |
|
885 | yield (style_default, repr(self)) | |
|
594 | yield (astyle.style_default, repr(self)) | |
|
886 | 595 | |
|
887 | 596 | def __repr__(self): |
|
888 | 597 | args = ", ".join([repr(it) for it in self.iters]) |
@@ -1124,13 +833,13 b' class ifile(path.path):' | |||
|
1124 | 833 | try: |
|
1125 | 834 | if self.isdir(): |
|
1126 | 835 | name = "idir" |
|
1127 | style = style_dir | |
|
836 | style = astyle.style_dir | |
|
1128 | 837 | else: |
|
1129 | 838 | name = "ifile" |
|
1130 | style = style_file | |
|
839 | style = astyle.style_file | |
|
1131 | 840 | except IOError: |
|
1132 | 841 | name = "ifile" |
|
1133 | style = style_default | |
|
842 | style = astyle.style_default | |
|
1134 | 843 | if mode == "cell" or mode in "header" or mode == "footer": |
|
1135 | 844 | abspath = repr(path._base(self.normpath())) |
|
1136 | 845 | if abspath.startswith("u"): |
@@ -1160,7 +869,7 b' class iparentdir(ifile):' | |||
|
1160 | 869 | def __xrepr__(self, mode): |
|
1161 | 870 | yield (-1, True) |
|
1162 | 871 | if mode == "cell": |
|
1163 | yield (style_dir, os.pardir) | |
|
872 | yield (astyle.style_dir, os.pardir) | |
|
1164 | 873 | else: |
|
1165 | 874 | for part in ifile.__xrepr__(self, mode): |
|
1166 | 875 | yield part |
@@ -1209,9 +918,10 b' class iglob(Table):' | |||
|
1209 | 918 | def __xrepr__(self, mode): |
|
1210 | 919 | yield (-1, True) |
|
1211 | 920 | if mode == "header" or mode == "footer" or mode == "cell": |
|
1212 | yield (style_default, "%s(%r)" % (self.__class__.__name__, self.glob)) | |
|
921 | yield (astyle.style_default, | |
|
922 | "%s(%r)" % (self.__class__.__name__, self.glob)) | |
|
1213 | 923 | else: |
|
1214 | yield (style_default, repr(self)) | |
|
924 | yield (astyle.style_default, repr(self)) | |
|
1215 | 925 | |
|
1216 | 926 | def __repr__(self): |
|
1217 | 927 | return "%s.%s(%r)" % \ |
@@ -1243,9 +953,10 b' class iwalk(Table):' | |||
|
1243 | 953 | def __xrepr__(self, mode): |
|
1244 | 954 | yield (-1, True) |
|
1245 | 955 | if mode == "header" or mode == "footer" or mode == "cell": |
|
1246 | yield (style_default, "%s(%r)" % (self.__class__.__name__, self.base)) | |
|
956 | yield (astyle.style_default, | |
|
957 | "%s(%r)" % (self.__class__.__name__, self.base)) | |
|
1247 | 958 | else: |
|
1248 | yield (style_default, repr(self)) | |
|
959 | yield (astyle.style_default, repr(self)) | |
|
1249 | 960 | |
|
1250 | 961 | def __repr__(self): |
|
1251 | 962 | return "%s.%s(%r)" % \ |
@@ -1330,9 +1041,9 b' class ipwd(Table):' | |||
|
1330 | 1041 | def __xrepr__(self, mode): |
|
1331 | 1042 | yield (-1, True) |
|
1332 | 1043 | if mode == "header" or mode == "footer" or mode == "cell": |
|
1333 | yield (style_default, "%s()" % self.__class__.__name__) | |
|
1044 | yield (astyle.style_default, "%s()" % self.__class__.__name__) | |
|
1334 | 1045 | else: |
|
1335 | yield (style_default, repr(self)) | |
|
1046 | yield (astyle.style_default, repr(self)) | |
|
1336 | 1047 | |
|
1337 | 1048 | |
|
1338 | 1049 | class igrpentry(object): |
@@ -1379,16 +1090,16 b' class igrpentry(object):' | |||
|
1379 | 1090 | def __xrepr__(self, mode): |
|
1380 | 1091 | yield (-1, True) |
|
1381 | 1092 | if mode == "header" or mode == "footer" or mode == "cell": |
|
1382 | yield (style_default, "group ") | |
|
1093 | yield (astyle.style_default, "group ") | |
|
1383 | 1094 | try: |
|
1384 | yield (style_default, self.name) | |
|
1095 | yield (astyle.style_default, self.name) | |
|
1385 | 1096 | except KeyError: |
|
1386 | 1097 | if isinstance(self._id, basestring): |
|
1387 | yield (style_default, self.name_id) | |
|
1098 | yield (astyle.style_default, self.name_id) | |
|
1388 | 1099 | else: |
|
1389 | yield (style_type_number, str(self._id)) | |
|
1100 | yield (astyle.style_type_number, str(self._id)) | |
|
1390 | 1101 | else: |
|
1391 | yield (style_default, repr(self)) | |
|
1102 | yield (astyle.style_default, repr(self)) | |
|
1392 | 1103 | |
|
1393 | 1104 | def __xiter__(self, mode): |
|
1394 | 1105 | for member in self.mem: |
@@ -1410,9 +1121,9 b' class igrp(Table):' | |||
|
1410 | 1121 | def __xrepr__(self, mode): |
|
1411 | 1122 | yield (-1, False) |
|
1412 | 1123 | if mode == "header" or mode == "footer": |
|
1413 | yield (style_default, "%s()" % self.__class__.__name__) | |
|
1124 | yield (astyle.style_default, "%s()" % self.__class__.__name__) | |
|
1414 | 1125 | else: |
|
1415 | yield (style_default, repr(self)) | |
|
1126 | yield (astyle.style_default, repr(self)) | |
|
1416 | 1127 | |
|
1417 | 1128 | |
|
1418 | 1129 | class Fields(object): |
@@ -1427,26 +1138,26 b' class Fields(object):' | |||
|
1427 | 1138 | def __xrepr__(self, mode): |
|
1428 | 1139 | yield (-1, False) |
|
1429 | 1140 | if mode == "header" or mode == "cell": |
|
1430 | yield (style_default, self.__class__.__name__) | |
|
1431 | yield (style_default, "(") | |
|
1141 | yield (astyle.style_default, self.__class__.__name__) | |
|
1142 | yield (astyle.style_default, "(") | |
|
1432 | 1143 | for (i, f) in enumerate(self.__fieldnames): |
|
1433 | 1144 | if i: |
|
1434 | yield (style_default, ", ") | |
|
1435 | yield (style_default, f) | |
|
1436 | yield (style_default, "=") | |
|
1145 | yield (astyle.style_default, ", ") | |
|
1146 | yield (astyle.style_default, f) | |
|
1147 | yield (astyle.style_default, "=") | |
|
1437 | 1148 | for part in xrepr(getattr(self, f), "default"): |
|
1438 | 1149 | yield part |
|
1439 | yield (style_default, ")") | |
|
1150 | yield (astyle.style_default, ")") | |
|
1440 | 1151 | elif mode == "footer": |
|
1441 | yield (style_default, self.__class__.__name__) | |
|
1442 | yield (style_default, "(") | |
|
1152 | yield (astyle.style_default, self.__class__.__name__) | |
|
1153 | yield (astyle.style_default, "(") | |
|
1443 | 1154 | for (i, f) in enumerate(self.__fieldnames): |
|
1444 | 1155 | if i: |
|
1445 | yield (style_default, ", ") | |
|
1446 | yield (style_default, f) | |
|
1447 | yield (style_default, ")") | |
|
1156 | yield (astyle.style_default, ", ") | |
|
1157 | yield (astyle.style_default, f) | |
|
1158 | yield (astyle.style_default, ")") | |
|
1448 | 1159 | else: |
|
1449 | yield (style_default, repr(self)) | |
|
1160 | yield (astyle.style_default, repr(self)) | |
|
1450 | 1161 | |
|
1451 | 1162 | |
|
1452 | 1163 | class FieldTable(Table, list): |
@@ -1464,15 +1175,15 b' class FieldTable(Table, list):' | |||
|
1464 | 1175 | def __xrepr__(self, mode): |
|
1465 | 1176 | yield (-1, False) |
|
1466 | 1177 | if mode == "header" or mode == "footer": |
|
1467 | yield (style_default, self.__class__.__name__) | |
|
1468 | yield (style_default, "(") | |
|
1178 | yield (astyle.style_default, self.__class__.__name__) | |
|
1179 | yield (astyle.style_default, "(") | |
|
1469 | 1180 | for (i, f) in enumerate(self.__fieldnames): |
|
1470 | 1181 | if i: |
|
1471 | yield (style_default, ", ") | |
|
1472 | yield (style_default, f) | |
|
1473 | yield (style_default, ")") | |
|
1182 | yield (astyle.style_default, ", ") | |
|
1183 | yield (astyle.style_default, f) | |
|
1184 | yield (astyle.style_default, ")") | |
|
1474 | 1185 | else: |
|
1475 | yield (style_default, repr(self)) | |
|
1186 | yield (astyle.style_default, repr(self)) | |
|
1476 | 1187 | |
|
1477 | 1188 | def __repr__(self): |
|
1478 | 1189 | return "<%s.%s object with fields=%r at 0x%x>" % \ |
@@ -1487,16 +1198,16 b' class List(list):' | |||
|
1487 | 1198 | def __xrepr__(self, mode): |
|
1488 | 1199 | yield (-1, False) |
|
1489 | 1200 | if mode == "header" or mode == "cell" or mode == "footer" or mode == "default": |
|
1490 | yield (style_default, self.__class__.__name__) | |
|
1491 | yield (style_default, "(") | |
|
1201 | yield (astyle.style_default, self.__class__.__name__) | |
|
1202 | yield (astyle.style_default, "(") | |
|
1492 | 1203 | for (i, item) in enumerate(self): |
|
1493 | 1204 | if i: |
|
1494 | yield (style_default, ", ") | |
|
1205 | yield (astyle.style_default, ", ") | |
|
1495 | 1206 | for part in xrepr(item, "default"): |
|
1496 | 1207 | yield part |
|
1497 | yield (style_default, ")") | |
|
1208 | yield (astyle.style_default, ")") | |
|
1498 | 1209 | else: |
|
1499 | yield (style_default, repr(self)) | |
|
1210 | yield (astyle.style_default, repr(self)) | |
|
1500 | 1211 | |
|
1501 | 1212 | |
|
1502 | 1213 | class ienv(Table): |
@@ -1516,9 +1227,9 b' class ienv(Table):' | |||
|
1516 | 1227 | def __xrepr__(self, mode): |
|
1517 | 1228 | yield (-1, True) |
|
1518 | 1229 | if mode == "header" or mode == "cell": |
|
1519 | yield (style_default, "%s()" % self.__class__.__name__) | |
|
1230 | yield (astyle.style_default, "%s()" % self.__class__.__name__) | |
|
1520 | 1231 | else: |
|
1521 | yield (style_default, repr(self)) | |
|
1232 | yield (astyle.style_default, repr(self)) | |
|
1522 | 1233 | |
|
1523 | 1234 | |
|
1524 | 1235 | class icsv(Pipe): |
@@ -1548,18 +1259,18 b' class icsv(Pipe):' | |||
|
1548 | 1259 | if input is not None: |
|
1549 | 1260 | for part in xrepr(input, mode): |
|
1550 | 1261 | yield part |
|
1551 | yield (style_default, " | ") | |
|
1552 | yield (style_default, "%s(" % self.__class__.__name__) | |
|
1262 | yield (astyle.style_default, " | ") | |
|
1263 | yield (astyle.style_default, "%s(" % self.__class__.__name__) | |
|
1553 | 1264 | for (i, (name, value)) in enumerate(self.csvargs.iteritems()): |
|
1554 | 1265 | if i: |
|
1555 | yield (style_default, ", ") | |
|
1556 | yield (style_default, name) | |
|
1557 | yield (style_default, "=") | |
|
1266 | yield (astyle.style_default, ", ") | |
|
1267 | yield (astyle.style_default, name) | |
|
1268 | yield (astyle.style_default, "=") | |
|
1558 | 1269 | for part in xrepr(value, "default"): |
|
1559 | 1270 | yield part |
|
1560 | yield (style_default, ")") | |
|
1271 | yield (astyle.style_default, ")") | |
|
1561 | 1272 | else: |
|
1562 | yield (style_default, repr(self)) | |
|
1273 | yield (astyle.style_default, repr(self)) | |
|
1563 | 1274 | |
|
1564 | 1275 | def __repr__(self): |
|
1565 | 1276 | args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()]) |
@@ -1596,9 +1307,10 b' class ix(Table):' | |||
|
1596 | 1307 | def __xrepr__(self, mode): |
|
1597 | 1308 | yield (-1, True) |
|
1598 | 1309 | if mode == "header" or mode == "footer": |
|
1599 | yield (style_default, "%s(%r)" % (self.__class__.__name__, self.cmd)) | |
|
1310 | yield (astyle.style_default, | |
|
1311 | "%s(%r)" % (self.__class__.__name__, self.cmd)) | |
|
1600 | 1312 | else: |
|
1601 | yield (style_default, repr(self)) | |
|
1313 | yield (astyle.style_default, repr(self)) | |
|
1602 | 1314 | |
|
1603 | 1315 | def __repr__(self): |
|
1604 | 1316 | return "%s.%s(%r)" % \ |
@@ -1676,13 +1388,13 b' class ifilter(Pipe):' | |||
|
1676 | 1388 | if input is not None: |
|
1677 | 1389 | for part in xrepr(input, mode): |
|
1678 | 1390 | yield part |
|
1679 | yield (style_default, " | ") | |
|
1680 | yield (style_default, "%s(" % self.__class__.__name__) | |
|
1391 | yield (astyle.style_default, " | ") | |
|
1392 | yield (astyle.style_default, "%s(" % self.__class__.__name__) | |
|
1681 | 1393 | for part in xrepr(self.expr, "default"): |
|
1682 | 1394 | yield part |
|
1683 | yield (style_default, ")") | |
|
1395 | yield (astyle.style_default, ")") | |
|
1684 | 1396 | else: |
|
1685 | yield (style_default, repr(self)) | |
|
1397 | yield (astyle.style_default, repr(self)) | |
|
1686 | 1398 | |
|
1687 | 1399 | def __repr__(self): |
|
1688 | 1400 | return "<%s.%s expr=%r at 0x%x>" % \ |
@@ -1745,13 +1457,13 b' class ieval(Pipe):' | |||
|
1745 | 1457 | if input is not None: |
|
1746 | 1458 | for part in xrepr(input, mode): |
|
1747 | 1459 | yield part |
|
1748 | yield (style_default, " | ") | |
|
1749 | yield (style_default, "%s(" % self.__class__.__name__) | |
|
1460 | yield (astyle.style_default, " | ") | |
|
1461 | yield (astyle.style_default, "%s(" % self.__class__.__name__) | |
|
1750 | 1462 | for part in xrepr(self.expr, "default"): |
|
1751 | 1463 | yield part |
|
1752 | yield (style_default, ")") | |
|
1464 | yield (astyle.style_default, ")") | |
|
1753 | 1465 | else: |
|
1754 | yield (style_default, repr(self)) | |
|
1466 | yield (astyle.style_default, repr(self)) | |
|
1755 | 1467 | |
|
1756 | 1468 | def __repr__(self): |
|
1757 | 1469 | return "<%s.%s expr=%r at 0x%x>" % \ |
@@ -1818,17 +1530,17 b' class isort(Pipe):' | |||
|
1818 | 1530 | if input is not None: |
|
1819 | 1531 | for part in xrepr(input, mode): |
|
1820 | 1532 | yield part |
|
1821 | yield (style_default, " | ") | |
|
1822 | yield (style_default, "%s(" % self.__class__.__name__) | |
|
1533 | yield (astyle.style_default, " | ") | |
|
1534 | yield (astyle.style_default, "%s(" % self.__class__.__name__) | |
|
1823 | 1535 | for part in xrepr(self.key, "default"): |
|
1824 | 1536 | yield part |
|
1825 | 1537 | if self.reverse: |
|
1826 | yield (style_default, ", ") | |
|
1538 | yield (astyle.style_default, ", ") | |
|
1827 | 1539 | for part in xrepr(True, "default"): |
|
1828 | 1540 | yield part |
|
1829 | yield (style_default, ")") | |
|
1541 | yield (astyle.style_default, ")") | |
|
1830 | 1542 | else: |
|
1831 | yield (style_default, repr(self)) | |
|
1543 | yield (astyle.style_default, repr(self)) | |
|
1832 | 1544 | |
|
1833 | 1545 | def __repr__(self): |
|
1834 | 1546 | return "<%s.%s key=%r reverse=%r at 0x%x>" % \ |
@@ -1890,7 +1602,7 b' def xformat(value, mode, maxlength):' | |||
|
1890 | 1602 | align = None |
|
1891 | 1603 | full = False |
|
1892 | 1604 | width = 0 |
|
1893 | text = Text() | |
|
1605 | text = astyle.Text() | |
|
1894 | 1606 | for part in xrepr(value, mode): |
|
1895 | 1607 | # part is (alignment, stop) |
|
1896 | 1608 | if isinstance(part[0], int): |
@@ -1903,7 +1615,7 b' def xformat(value, mode, maxlength):' | |||
|
1903 | 1615 | text.append(part) |
|
1904 | 1616 | width += len(part[1]) |
|
1905 | 1617 | if width >= maxlength and not full: |
|
1906 | text.append((style_ellisis, "...")) | |
|
1618 | text.append((astyle.style_ellisis, "...")) | |
|
1907 | 1619 | width += 3 |
|
1908 | 1620 | break |
|
1909 | 1621 | if align is None: # default to left alignment |
@@ -1916,7 +1628,7 b' class idump(Display):' | |||
|
1916 | 1628 | maxattrlength = 200 |
|
1917 | 1629 | |
|
1918 | 1630 | # Style for column names |
|
1919 | style_header = Style(COLOR_WHITE, COLOR_BLACK, A_BOLD) | |
|
1631 | style_header = astyle.Style.fromstr("white:black:bold") | |
|
1920 | 1632 | |
|
1921 | 1633 | def __init__(self, *attrs): |
|
1922 | 1634 | self.attrs = attrs |
@@ -2010,9 +1722,9 b' class XMode(object):' | |||
|
2010 | 1722 | def __xrepr__(self, mode): |
|
2011 | 1723 | yield (-1, True) |
|
2012 | 1724 | if mode == "header" or mode == "footer": |
|
2013 | yield (style_default, self.title) | |
|
1725 | yield (astyle.style_default, self.title) | |
|
2014 | 1726 | else: |
|
2015 | yield (style_default, repr(self)) | |
|
1727 | yield (astyle.style_default, repr(self)) | |
|
2016 | 1728 | |
|
2017 | 1729 | def __xattrs__(self, mode): |
|
2018 | 1730 | if mode == "detail": |
@@ -2205,7 +1917,7 b' if curses is not None:' | |||
|
2205 | 1917 | |
|
2206 | 1918 | |
|
2207 | 1919 | class _BrowserHelp(object): |
|
2208 | style_header = Style(COLOR_RED, COLOR_BLACK) | |
|
1920 | style_header = astyle.Style.fromstr("red:blacK") | |
|
2209 | 1921 | # This is used internally by ``ibrowse`` for displaying the help screen. |
|
2210 | 1922 | def __init__(self, browser): |
|
2211 | 1923 | self.browser = browser |
@@ -2213,9 +1925,9 b' if curses is not None:' | |||
|
2213 | 1925 | def __xrepr__(self, mode): |
|
2214 | 1926 | yield (-1, True) |
|
2215 | 1927 | if mode == "header" or mode == "footer": |
|
2216 | yield (style_default, "ibrowse help screen") | |
|
1928 | yield (astyle.style_default, "ibrowse help screen") | |
|
2217 | 1929 | else: |
|
2218 | yield (style_default, repr(self)) | |
|
1930 | yield (astyle.style_default, repr(self)) | |
|
2219 | 1931 | |
|
2220 | 1932 | def __xiter__(self, mode): |
|
2221 | 1933 | # Get reverse key mapping |
@@ -2233,7 +1945,7 b' if curses is not None:' | |||
|
2233 | 1945 | keys = allkeys.get(name, []) |
|
2234 | 1946 | lines = textwrap.wrap(description, 60) |
|
2235 | 1947 | |
|
2236 | yield Fields(fields, description=Text((self.style_header, name))) | |
|
1948 | yield Fields(fields, description=astyle.Text((self.style_header, name))) | |
|
2237 | 1949 | for i in xrange(max(len(keys), len(lines))): |
|
2238 | 1950 | try: |
|
2239 | 1951 | key = self.browser.keylabel(keys[i]) |
@@ -2371,7 +2083,7 b' if curses is not None:' | |||
|
2371 | 2083 | parts.append(part) |
|
2372 | 2084 | totallength += len(part[1]) |
|
2373 | 2085 | if totallength >= self.browser.maxattrlength and not full: |
|
2374 | parts.append((style_ellisis, "...")) | |
|
2086 | parts.append((astyle.style_ellisis, "...")) | |
|
2375 | 2087 | totallength += 3 |
|
2376 | 2088 | break |
|
2377 | 2089 | # remember alignment, length and colored parts |
@@ -2566,19 +2278,19 b' if curses is not None:' | |||
|
2566 | 2278 | maxattrlength = 200 |
|
2567 | 2279 | |
|
2568 | 2280 | # Styles for various parts of the GUI |
|
2569 | style_objheadertext = Style(COLOR_WHITE, COLOR_BLACK, A_BOLD|A_REVERSE) | |
|
2570 | style_objheadernumber = Style(COLOR_WHITE, COLOR_BLUE, A_BOLD|A_REVERSE) | |
|
2571 | style_objheaderobject = Style(COLOR_WHITE, COLOR_BLACK, A_REVERSE) | |
|
2572 | style_colheader = Style(COLOR_BLUE, COLOR_WHITE, A_REVERSE) | |
|
2573 | style_colheaderhere = Style(COLOR_GREEN, COLOR_BLACK, A_BOLD|A_REVERSE) | |
|
2574 | style_colheadersep = Style(COLOR_BLUE, COLOR_BLACK, A_REVERSE) | |
|
2575 | style_number = Style(COLOR_BLUE, COLOR_WHITE, A_REVERSE) | |
|
2576 | style_numberhere = Style(COLOR_GREEN, COLOR_BLACK, A_BOLD|A_REVERSE) | |
|
2577 | style_sep = Style(COLOR_BLUE, COLOR_BLACK) | |
|
2578 | style_data = Style(COLOR_WHITE, COLOR_BLACK) | |
|
2579 | style_datapad = Style(COLOR_BLUE, COLOR_BLACK, A_BOLD) | |
|
2580 | style_footer = Style(COLOR_BLACK, COLOR_WHITE) | |
|
2581 | style_report = Style(COLOR_WHITE, COLOR_BLACK) | |
|
2281 | style_objheadertext = astyle.Style.fromstr("white:black:bold|reverse") | |
|
2282 | style_objheadernumber = astyle.Style.fromstr("white:blue:bold|reverse") | |
|
2283 | style_objheaderobject = astyle.Style.fromstr("white:black:reverse") | |
|
2284 | style_colheader = astyle.Style.fromstr("blue:white:reverse") | |
|
2285 | style_colheaderhere = astyle.Style.fromstr("green:black:bold|reverse") | |
|
2286 | style_colheadersep = astyle.Style.fromstr("blue:black:reverse") | |
|
2287 | style_number = astyle.Style.fromstr("blue:white:reverse") | |
|
2288 | style_numberhere = astyle.Style.fromstr("green:black:bold|reverse") | |
|
2289 | style_sep = astyle.Style.fromstr("blue:black") | |
|
2290 | style_data = astyle.Style.fromstr("white:black") | |
|
2291 | style_datapad = astyle.Style.fromstr("blue:black:bold") | |
|
2292 | style_footer = astyle.Style.fromstr("black:white") | |
|
2293 | style_report = astyle.Style.fromstr("white:black") | |
|
2582 | 2294 | |
|
2583 | 2295 | # Column separator in header |
|
2584 | 2296 | headersepchar = "|" |
@@ -2710,16 +2422,16 b' if curses is not None:' | |||
|
2710 | 2422 | return self._styles[style.fg, style.bg, style.attrs] |
|
2711 | 2423 | except KeyError: |
|
2712 | 2424 | attrs = 0 |
|
2713 | for b in A2CURSES: | |
|
2425 | for b in astyle.A2CURSES: | |
|
2714 | 2426 | if style.attrs & b: |
|
2715 | attrs |= A2CURSES[b] | |
|
2427 | attrs |= astyle.A2CURSES[b] | |
|
2716 | 2428 | try: |
|
2717 | 2429 | color = self._colors[style.fg, style.bg] |
|
2718 | 2430 | except KeyError: |
|
2719 | 2431 | curses.init_pair( |
|
2720 | 2432 | self._maxcolor, |
|
2721 | COLOR2CURSES[style.fg], | |
|
2722 | COLOR2CURSES[style.bg] | |
|
2433 | astyle.COLOR2CURSES[style.fg], | |
|
2434 | astyle.COLOR2CURSES[style.bg] | |
|
2723 | 2435 | ) |
|
2724 | 2436 | color = curses.color_pair(self._maxcolor) |
|
2725 | 2437 | self._colors[style.fg, style.bg] = color |
@@ -2758,7 +2470,7 b' if curses is not None:' | |||
|
2758 | 2470 | Return a style for displaying the original style ``style`` |
|
2759 | 2471 | in the row the cursor is on. |
|
2760 | 2472 | """ |
|
2761 | return Style(style.fg, style.bg, style.attrs | A_BOLD) | |
|
2473 | return astyle.Style(style.fg, style.bg, style.attrs | astyle.A_BOLD) | |
|
2762 | 2474 | |
|
2763 | 2475 | def report(self, msg): |
|
2764 | 2476 | """ |
@@ -3337,7 +3049,7 b' if curses is not None:' | |||
|
3337 | 3049 | if posx >= endx: |
|
3338 | 3050 | break |
|
3339 | 3051 | |
|
3340 | attrstyle = [(style_default, "no attribute")] | |
|
3052 | attrstyle = [(astyle.style_default, "no attribute")] | |
|
3341 | 3053 | attrname = level.displayattr[1] |
|
3342 | 3054 | if attrname is not _default and attrname is not None: |
|
3343 | 3055 | posx += self.addstr(posy, posx, 0, endx, " | ", self.style_footer) |
General Comments 0
You need to be logged in to leave comments.
Login now