Source

snippy / snippy.py

Full commit
def snip(text, length=80, postfix='...'):
    """Snip a string to a length, while keeping it pretty.
    
    Snip requires the 'text' parameter, which is the string to be snipped.
    
    Snip accepts a 'length' parameter.  The resulting string is guaranteed
    to fit within that many characters.  The default is 80.
    
    Snip also accepts a 'postfix' parameter.  This is the strip that will be
    appended if the string needs to be shortened.  The default is '...'."""
    
    if len(text) < length:
        return text
    elif len(postfix) >= length:
        return postfix[:length]
    else:
        return (text[:length-len(postfix)].strip() + postfix)[:length]