'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)))
    bg = self.savebg
    n = 0
    if (attr is None):
        attr = self.attr
    try:
        fg = self.trtable[(15 & attr)]
        bg = self.trtable[((240 & attr) >> 4)]
    except TypeError:
        fg = attr
    for chunk in chunks:
        m = self.escape_parts.match(chunk)
        if m:
            log(m.group(1))
            attr = ansicolor.get(m.group(1), self.attr)
        n += len(chunk)
        System.Console.ForegroundColor = fg
        System.Console.BackgroundColor = bg
        System.Console.Write(chunk)
    return n
