tuikit/driver_sdl.py
changeset 50 c5b8b9d2da95
parent 48 1f00e90fd72a
child 53 c4263588b716
--- a/tuikit/driver_sdl.py	Sat Jan 05 16:47:30 2013 +0100
+++ b/tuikit/driver_sdl.py	Sat Jan 05 18:44:56 2013 +0100
@@ -19,11 +19,40 @@
 
     '''SDL driver class'''
 
+    color_map = {
+        'black'         : 0,
+        'blue'          : 1,
+        'green'         : 2,
+        'cyan'          : 3,
+        'red'           : 4,
+        'magenta'       : 5,
+        'brown'         : 6,
+        'lightgray'     : 7,
+        'gray'          : 8,
+        'lightblue'     : 9,
+        'lightgreen'    : 10,
+        'lightcyan'     : 11,
+        'lightred'      : 12,
+        'lightmagenta'  : 13,
+        'yellow'        : 14,
+        'white'         : 15,
+    }
+
+    style_map = {
+        'bold'      : 1 << 0,  # bold font
+        'underline' : 1 << 1,  # underline text
+        'standout'  : 1 << 2,  # inverse bg/fg
+        'blink'     : 1 << 3,  # blinking text
+        'dim'       : 0,
+    }
+
     def __init__(self):
         '''Initialize instance attributes'''
         Driver.__init__(self)
         self.log = logging.getLogger('tuikit')
         self.sdlterm = SDLTerminal()
+        self.colors = {}     # maps names to attributes
+        self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
 
     def start(self, mainfunc):
         '''Start driver and run mainfunc.'''
@@ -67,15 +96,43 @@
         desc - color description - foreground, background, attributes (e.g. 'black on white, bold')
 
         '''
-        self.log.info('DummyDriver.setcolor(name=%r, desc=%r)', name, desc)
+        parts = desc.split(',')
+        fg, bg = parts[0].split(' on ')
+        style = parts[1:]
+        fg = self._color_by_name(fg)
+        bg = self._color_by_name(bg)
+        style = self._style_by_names(style)
+        self.colors[name] = self.sdlterm.prepare_attr(fg, bg, style);
 
     def pushcolor(self, name):
         '''Add color on top of stack and use this color for following output.'''
-        self.log.info('DummyDriver.pushcolor(name=%r)', name)
+        if len(self.colorprefix):
+            prefixname = self.colorprefix[-1] + name
+            if prefixname in self.colors:
+                name = prefixname
+        attr = self.colors[name]
+        self.sdlterm.set_attr(attr)
+        self.colorstack.append(attr)
 
     def popcolor(self):
         '''Remove color from top of stack and use new top color for following output.'''
-        self.log.info('DummyDriver.popcolor()')
+        self.colorstack.pop()
+        if len(self.colorstack):
+            attr = self.colorstack[-1]
+        else:
+            attr = 0
+        self.sdlterm.set_attr(attr)
+
+    def _color_by_name(self, name):
+        name = name.lower().strip()
+        return self.color_map[name]
+
+    def _style_by_names(self, names):
+        style = 0
+        for name in names:
+            name = name.lower().strip()
+            style |= self.style_map[name]
+        return style
 
 
     ## cursor ##