Wrap, fill, indent, dedent, and shorten text for Ecko, written in Ecko - handy for building prompts and formatting terminal output.
ecko get github.com/ecko-lang/textwrapimport textwrap
textwrap.wrap("a long sentence here", 12) # ["a long", "sentence", "here"]
textwrap.fill("a long sentence here", 12) # "a long\nsentence\nhere"
textwrap.indent("a\nb", "> ") # "> a\n> b"
textwrap.dedent(" a\n b") # "a\nb"
textwrap.shorten("a long sentence here", 12) # "a long ..."
| Function | Description |
|---|---|
wrap(text, width) |
Word-wrap into a list of lines, each ≤ width |
fill(text, width) |
wrap joined with newlines |
indent(text, prefix) |
Prefix every non-blank line |
dedent(text) |
Remove the common leading whitespace from all lines |
shorten(text, width) |
Collapse whitespace and truncate with an "..." ellipsis at a word boundary |
- Runs of whitespace (spaces, tabs, newlines) are collapsed to single spaces.
- A word longer than
widthis kept whole on its own line rather than broken.
ecko test tests/MIT - see LICENSE.