Crate textwrap [] [src]

textwrap provides functions for word wrapping and filling text.

This can be very useful in commandline programs where you want to format dynamic output nicely so it looks good in a terminal.

To wrap text, one must know the width of each word so can know when to break lines. This library measures the width of text using the displayed width, not the size in bytes.

This is important for non-ASCII text. ASCII characters such as a and ! are simple: the displayed with is the same as the number of bytes used in their UTF-8 encoding (one ASCII character takes up one byte in UTF-8). However, non-ASCII characters and symbols take up more than one byte: é is 0xc3 0xa9 and is 0xe2 0x9a 0x99 in UTF-8, respectively.

This is why we take care to use the displayed width instead of the byte count when computing line lengths. All functions in this library handle Unicode characters like this.

Structs

Wrapper

A Wrapper holds settings for wrapping and filling text.

Functions

dedent

Removes common leading whitespace from each line.

fill

Fill a line of text at width characters. Strings are wrapped based on their displayed width, not their size in bytes.

indent

Add prefix to each non-empty line.

wrap

Wrap a line of text at width characters. Strings are wrapped based on their displayed width, not their size in bytes.