Expand description
Building blocks for advanced wrapping functionality.
The functions and structs in this module can be used to implement
advanced wrapping functionality when wrap()
fill() don’t do what you want.
In general, you want to follow these steps when wrapping something:
-
Split your input into
Fragments. These are abstract blocks of text or content which can be wrapped into lines. SeeWordSeparatorfor how to do this for text. -
Potentially split your fragments into smaller pieces. This allows you to implement things like hyphenation. If you use the
Wordtype, you can useWordSplitterenum for this. -
Potentially break apart fragments that are still too large to fit on a single line. This is implemented in
break_words. -
Finally take your fragments and put them into lines. There are two algorithms for this in the
wrap_algorithmsmodule:wrap_optimal_fitandwrap_first_fit. The former produces better line breaks, the latter is faster. -
Iterate through the slices returned by the wrapping functions and construct your lines of output.
Please open an issue if the functionality here is not sufficient or if you have ideas for improving it. We would love to hear from you!
Structs§
- Word
- A piece of wrappable text, including any trailing whitespace.
Traits§
- Fragment
- A (text) fragment denotes the unit which we wrap into lines.
Functions§
- break_
words - Forcibly break words wider than
line_widthinto smaller words. - display_
width - Compute the display width of
textwhile skipping over ANSI escape sequences.