We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c490339 commit 0159588Copy full SHA for 0159588
FluentPython/clip.py
@@ -0,0 +1,17 @@
1
+def clip(text, max_len=80):
2
+# print(len(text))
3
+ '''在max_len前面或者后面第一个出现的空格处截断文本'''
4
+ end = None
5
+ if len(text) > max_len:
6
+ space_before = text.rfind(' ', 0, max_len)
7
+# print(space_before)
8
+ if space_before >= 0:
9
+ end = space_before
10
+ else:
11
+ space_after = text.rfind(" ", max_len)
12
+
13
+ if space_after >= 0:
14
+ end = space_after
15
+ if end is None:
16
+ end = len(text)
17
+ return text[:end].rstrip()
0 commit comments