1. Steve Losh
  2. snippy

Commits

Steve Losh  committed 342980a

First really useful commit.

  • Participants
  • Parent commits d05013e
  • Branches default

Comments (0)

Files changed (1)

File snippy.py

View file
+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]