pyqonsole

view pyqonsole/ca.py @ 188:f353f5aba78e

copyright update, remove __revision__
author "Sylvain <syt@logilab.fr>"
date Fri, 09 Mar 2007 14:05:04 +0100
parents 093a2d693ff9
children
line source
1 # Copyright (c) 2005-2007 LOGILAB S.A. (Paris, FRANCE).
2 # Copyright (c) 2005-2006 CEA Grenoble
3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr
4 #
5 # This program is free software; you can redistribute it and/or modify it under
6 # the terms of the CECILL license, available at
7 # http://www.inria.fr/valorisation/logiciels/Licence.CeCILL-V2.pdf
8 #
9 """Provide the Ca class and some other rendering utilities.
11 Based on the konsole code from Lars Doelle.
13 @author: Lars Doelle
14 @author: Benjamin Longuet
15 @author: Frederic Mantegazza
16 @author: Cyrille Boullier
17 @author: Sylvain Thenault
18 @copyright: 2003, 2005-2007
19 @organization: CEA-Grenoble
20 @organization: Logilab
21 @license: CECILL
22 """
24 BASE_COLORS = 2+8
25 _INTENSITIES = 2
26 TABLE_COLORS = _INTENSITIES * BASE_COLORS
28 DEFAULT_FORE_COLOR = -1
29 DEFAULT_BACK_COLOR = 0
31 DEFAULT_RENDITION = 0
32 RE_BOLD = 2**0
33 RE_BLINK = 2**1
34 RE_UNDERLINE = 2**2
35 RE_REVERSE = 2**3
36 RE_CURSOR = 2**4
39 class Ca(object):
40 """a character with background / foreground colors and rendition attributes
41 """
42 __slots__ = ('c', 'f', 'b', 'r')
44 def __init__(self, c=u' ', f=DEFAULT_FORE_COLOR,
45 b=DEFAULT_BACK_COLOR, r=DEFAULT_RENDITION):
46 self.c = c # character
47 self.f = f # foreground color
48 self.b = b # background color
49 self.r = r # rendition
51 def __eq__(self, other):
52 """implements the '==' operator"""
53 return (self.c == other.c and self.f == other.f and
54 self.b == other.b and self.r == other.r)
56 def __ne__(self, other):
57 """implements the '!=' operator"""
58 return (self.c != other.c or self.f != other.f or
59 self.b != other.b or self.r != other.r)
61 def __repr__(self):
62 """to help debugging"""
63 return '%r %s %s %r' % (self.c, self.f, self.b, self.r)
65 def isSpace(self):
66 """return true if this character can be considered as a space"""
67 return self.c.isspace()
69 def charClass(self, word_characters=u":@-./_~"):
70 """return a kind of category for this char
71 * space (' ')
72 * alpha numeric ('a')
73 * other (1)
74 """
75 char = self.c
76 if char.isspace():
77 return ' '
78 if char.isalnum() or char in word_characters:
79 return 'a'
80 # everything else is weird
81 return 1
83 ## # XXX for debugging
84 ## def setC(self, c):
85 ## assert c is None or isinstance(c, unicode)
86 ## self._c = c
87 ## def getC(self):
88 ## return self._c
89 ## c = property(getC, setC)
91 DCA = Ca() # the default character for optimization
94 class ColorEntry:
95 """a color with additional attribute (transparent / bold)
96 """
97 def __init__(self, c=None, tr=False, b=False):
98 self.color = c
99 self.transparent = tr # if used on bg
100 self.bold = b # if used on fg