def truncpad(srcline, length, align=u'l', ellipsis=True):
    """Return srcline truncated and padded to length, aligned as requested."""
    # truncate
    if len(srcline) > length:
        if ellipsis and length > 4:
            ret = srcline[0:(length-1)] + u'\u2026' # Ellipsis
        else:
            ret = srcline[0:length]
    else:
        # pad
        if len(srcline) < length:
            if align == u'l':
                ret = srcline.ljust(length)
            elif align == u'r':
                ret = srcline.rjust(length)
            else:
                ret = srcline.center(length)
        else:
            ret = srcline
    return ret