How can annotation previews truncate at word boundaries?

shorten collapses whitespace and removes trailing words until the text and placeholder fit within the width.

Python example and interpretation

A short preview of blue sky over lake can retain blue sky... . This uses whitespace boundaries; continuous Chinese text or code needs a suitable segmentation rule.

from textwrap import shorten
shorten('blue sky over lake',width=12,placeholder='...')

Result 'blue sky...'

中文

如何让长标注的列表预览尽量在词边界截断?

shorten 先折叠输入空白,再删除末尾整词,直到文本连同占位符能放进指定宽度。

Python 算例与适用条件

预览 blue sky over lake 只留 blue sky...。它依靠空白分词,连续中文或代码应采用符合该内容的切分方式。

from textwrap import shorten
shorten('blue sky over lake',width=12,placeholder='...')

结果 'blue sky...'

References / 参考来源

Keep reading / 继续阅读