tuikit/driver/driver.py
author Radek Brich <radek.brich@devl.cz>
Sun, 22 Feb 2015 09:53:13 +0100
changeset 119 dd91747504dd
parent 118 8c7970520632
permissions -rw-r--r--
Redraw widgets on request. Add scrollbar demo.

# -*- coding: utf-8 -*-

from tuikit.common import Size, ClipStack


class Driver:

    """Driver base class.

    Defines common interface.

    """

    def __init__(self):
        #: Screen size.
        self.size = Size()
        #: Clipping region stack.
        self.clipstack = ClipStack()

    ## initialization, finalization ##

    def init(self):
        """Initialize the driver and screen."""
        pass

    def close(self):
        """Clean up the screen etc."""
        pass

    ## drawing ##

    def erase(self):
        pass

    def putch(self, x, y, c):
        pass

    def draw(self, buffer, x=0, y=0):
        for bufy in range(buffer.size.h):
            for bufx in range(buffer.size.w):
                print(buffer.get(bufx, bufy)[0], end='')
            print()

    def flush(self):
        pass

    ## colors, attributes ##

    def setattr(self, attr_desc):
        """Set attribute to be used for subsequent draw operations."""
        pass

    ## cursor ##

    def showcursor(self, x, y):
        pass

    def hidecursor(self):
        pass

    ## input, events ##

    def getevents(self, timeout=None):
        """Process input, return list of events.

        timeout -- float, in seconds (None=infinite)

        Returns:
            [('event', param1, ...), ...]

        """
        return []

    ## convenience implementations ##

    def _parse_attr_desc(self, attr_desc):
        """Convenience implementation of attribute parsing. Not part of API."""
        parts = attr_desc.split(',')
        fgbg = parts[0].split(' on ', 1)
        fg = fgbg[0].strip().lower()
        bg = fgbg[1:] and fgbg[1].strip().lower() or 'default'
        attrs = (part.strip().lower() for part in parts[1:])
        return fg, bg, attrs

    ## with statement support ##

    def __enter__(self):
        self.init()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()