Skip to content

Commit 3fe8b03

Browse files
committed
Simplify truncate_words
1 parent 4669d34 commit 3fe8b03

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

cloudbot/util/formatting.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,17 @@ def truncate_words(content, length=10, suffix='...'):
143143
Truncates a string after a certain number of words.
144144
:rtype str
145145
"""
146-
msg = content.split(" ")
147-
out = None
148-
x = 0
149-
for i in msg:
150-
if x < length:
151-
if out:
152-
out = out + " " + msg[x]
153-
else:
154-
out = msg[x]
155-
x += 1
156-
if x <= length:
157-
return out
146+
split = content.split()
147+
if len(split) <= length:
148+
return " ".join(split[:length])
158149
else:
159-
return out + suffix
150+
return " ".join(split[:length]) + suffix
160151

161152

162153
def truncate(content, length=100, suffix='...'):
163154
"""
164155
Truncates a string after a certain number of characters.
156+
Function always truncates on a word boundary.
165157
:rtype str
166158
"""
167159
if len(content) <= length:

0 commit comments

Comments
 (0)