def abbreviate(s, maxlength=25):
    """Color-aware abbreviator"""
    assert maxlength >= 4
    skip = False
    abbrv = None
    i = 0
    for j, c in enumerate(s):
        if c == '\x1b':
            skip = True
        elif skip:
            if c == 'm':
                skip = False
        else:
            i += 1
        if i == maxlength - 1:
            abbrv = s[:j] + '\x1b[0m...'
        elif i > maxlength:
            break
    if i <= maxlength:
        return s
    else:
        return abbrv
