Collapse and truncate the given text to fit in the given width. The text first has its whitespace collapsed. If it then fits in the *width*, it is returned as is. Otherwise, as many words as possible are joined and then the placeholder is appended:: >>> textwrap.shorten("Hell
(text, width, **kwargs)
| 396 | return w.fill(text) |
| 397 | |
| 398 | def shorten(text, width, **kwargs): |
| 399 | """Collapse and truncate the given text to fit in the given width. |
| 400 | |
| 401 | The text first has its whitespace collapsed. If it then fits in |
| 402 | the *width*, it is returned as is. Otherwise, as many words |
| 403 | as possible are joined and then the placeholder is appended:: |
| 404 | |
| 405 | >>> textwrap.shorten("Hello world!", width=12) |
| 406 | 'Hello world!' |
| 407 | >>> textwrap.shorten("Hello world!", width=11) |
| 408 | 'Hello [...]' |
| 409 | """ |
| 410 | w = TextWrapper(width=width, max_lines=1, **kwargs) |
| 411 | return w.fill(' '.join(text.strip().split())) |
| 412 | |
| 413 | |
| 414 | # -- Loosely related functionality ------------------------------------- |
searching dependent graphs…