indentation.rs |
Functions related to adding and removing indentation from lines of
text.
The functions here can be used to uniformly indent or dedent
(unindent) word wrapped lines of text. |
7486 |
lib.rs |
`textwrap` provides functions for word wrapping and filling text.
Wrapping text can be very useful in commandline programs where you
want to format dynamic output nicely so it looks good in a
terminal. A quick example:
```no_run
extern crate textwrap;
use textwrap::fill;
fn main() {
let text = "textwrap: a small library for wrapping text.";
println!("{}", fill(text, 18));
}
```
This will display the following output:
```text
textwrap: a small
library for
wrapping text.
```
# Displayed Width vs Byte Size
To word wrap text, one must know the width of each word so one can
know when to break lines. This library measures the width of text
using the [displayed width][unicode-width], not the size in bytes.
This is important for non-ASCII text. ASCII characters such as `a`
and `!` are simple and take up one column each. This means that
the displayed width is equal to the string length in bytes.
However, non-ASCII characters and symbols take up more than one
byte when UTF-8 encoded: `é` is `0xc3 0xa9` (two bytes) and `⚙` is
`0xe2 0x9a 0x99` (three bytes) 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.
[unicode-width]: https://docs.rs/unicode-width/ |
33203 |
splitting.rs |
Word splitting functionality.
To wrap text into lines, long words sometimes need to be split
across lines. The [`WordSplitter`] trait defines this
functionality. [`HyphenSplitter`] is the default implementation of
this treat: it will simply split words on existing hyphens. |
5106 |