def col256(text, fg=None, bg=None, bold=False):

    def _get_color(col):
        return '8;5;{0:d}'.format(_to_color(col))

    def _to_color(num):
        if isinstance(num, int):
            return num
        if (isinstance(num, str) and (len(num) <= 3)):
            return (16 + int(num, 6))
        raise ValueError('Invalid color: {0!r}'.format(num))
    if (not isinstance(text, str)):
        text = repr(text)
    buf = io.StringIO()
    if bold:
        buf.write('\x1b[1m')
    if (fg is not None):
        buf.write('\x1b[3{0}m'.format(_get_color(fg)))
    if (bg is not None):
        buf.write('\x1b[4{0}m'.format(_get_color(bg)))
    buf.write(text)
    buf.write('\x1b[0m')
    return buf.getvalue()
def col256(text, fg=None, bg=None, bold=False):

    def _get_color(col):
        return '8;5;{0:d}'.format(_to_color(col))

    def _to_color(num):
        if isinstance(num, int):
            return num
        if (isinstance(num, str) and (len(num) <= 3)):
            return (16 + int(num, 6))
        raise ValueError('Invalid color: {0!r}'.format(num))
    if (not isinstance(text, str)):
        text = repr(text)
    buf = io.StringIO()
    if bold:
        buf.write('\x1b[1m')
    if (fg is not None):
        buf.write('\x1b[3{0}m'.format(_get_color(fg)))
    if (bg is not None):
        buf.write('\x1b[4{0}m'.format(_get_color(bg)))
    buf.write(text)
    buf.write('\x1b[0m')
    return buf.getvalue()