'write text at current cursor position and interpret color escapes.
return the number of characters written.'

def write_color(self, text, attr=None):

    log(('write_color("%s",    %s)' % (text, attr)))
    chunks = self.terminal_escape.split(text)
    log(('chunks=%s' % repr(chunks)))
    junk = c_int(0)
    n = 0
    for chunk in chunks:
        m = self.escape_parts.match(chunk)
        if m:
            attr = self.escape_to_color[m.group(1)]
            continue
        n += len(chunk)
        log(('attr=%s' % attr))
        if (attr is None):
            attr = self.attr
        self.SetConsoleTextAttribute(self.hout, attr)
        self.WriteConsoleA(self.hout, ensure_text(chunk), len(chunk), byref(junk), None)
    return n
