| 1 |
# Textwrap |
| 2 |
|
| 3 |
[][build-status] |
| 4 |
[][codecov] |
| 5 |
[][crates-io] |
| 6 |
[][api-docs] |
| 7 |
|
| 8 |
Textwrap is a library for wrapping and indenting text. It is most often used by |
| 9 |
command-line programs to format dynamic output nicely so it looks good in a |
| 10 |
terminal. You can also use Textwrap to wrap text set in a proportional font—such |
| 11 |
as text used to generate PDF files, or drawn on an |
| 12 |
[HTML5 canvas using WebAssembly][wasm-demo]. |
| 13 |
|
| 14 |
## Usage |
| 15 |
|
| 16 |
To use the textwrap crate, add this to your `Cargo.toml` file: |
| 17 |
|
| 18 |
```toml |
| 19 |
[dependencies] |
| 20 |
textwrap = "0.16" |
| 21 |
``` |
| 22 |
|
| 23 |
By default, this enables word wrapping with support for Unicode strings. Extra |
| 24 |
features can be enabled with Cargo features—and the Unicode support can be |
| 25 |
disabled if needed. This allows you to slim down the library, so you only pay |
| 26 |
for the features you actually use. |
| 27 |
|
| 28 |
Textwrap supports `no_std` environments with an allocator. You can enable |
| 29 |
`unicode-width` for accurate character widths on embedded targets. |
| 30 |
|
| 31 |
Please see the [_Cargo Features_ in the crate documentation][cargo-features] for |
| 32 |
a full list of the available features as well as their impact on the size of |
| 33 |
your binary. |
| 34 |
|
| 35 |
## Documentation |
| 36 |
|
| 37 |
**[API documentation][api-docs]** |
| 38 |
|
| 39 |
## Getting Started |
| 40 |
|
| 41 |
Word wrapping is easy using the `wrap` and `fill` functions: |
| 42 |
|
| 43 |
```rust |
| 44 |
#[cfg(feature = "smawk")] { |
| 45 |
let text = "textwrap: an efficient and powerful library for wrapping text."; |
| 46 |
assert_eq!( |
| 47 |
textwrap::wrap(text, 28), |
| 48 |
vec![ |
| 49 |
"textwrap: an efficient", |
| 50 |
"and powerful library for", |
| 51 |
"wrapping text.", |
| 52 |
] |
| 53 |
); |
| 54 |
} |
| 55 |
``` |
| 56 |
|
| 57 |
Sharp-eyed readers will notice that the first line is 22 columns wide. So why is |
| 58 |
the word “and” put in the second line when there is space for it in the first |
| 59 |
line? |
| 60 |
|
| 61 |
The explanation is that textwrap does not just wrap text one line at a time. |
| 62 |
Instead, it uses an optimal-fit algorithm which looks ahead and chooses line |
| 63 |
breaks that minimize the gaps left at the ends of lines. This is controlled with |
| 64 |
the `smawk` Cargo feature, which is why the example is wrapped in the |
| 65 |
`cfg`-block. |
| 66 |
|
| 67 |
Without look-ahead, the first line would be longer and the text would look like |
| 68 |
this: |
| 69 |
|
| 70 |
```rust |
| 71 |
#[cfg(not(feature = "smawk"))] { |
| 72 |
let text = "textwrap: an efficient and powerful library for wrapping text."; |
| 73 |
assert_eq!( |
| 74 |
textwrap::wrap(text, 28), |
| 75 |
vec![ |
| 76 |
"textwrap: an efficient and", |
| 77 |
"powerful library for", |
| 78 |
"wrapping text.", |
| 79 |
] |
| 80 |
); |
| 81 |
} |
| 82 |
``` |
| 83 |
|
| 84 |
The second line is now shorter and the text is more ragged. The wrapping |
| 85 |
algorithm can be configured via `Options::wrap_algorithm`. |
| 86 |
|
| 87 |
If you enable the `hyphenation` Cargo feature, you get support for automatic |
| 88 |
hyphenation for [about 70 languages][patterns] via high-quality TeX hyphenation |
| 89 |
patterns. |
| 90 |
|
| 91 |
Your program must load the hyphenation patterns and configure |
| 92 |
`Options::word_splitter` to use it: |
| 93 |
|
| 94 |
```rust |
| 95 |
#[cfg(feature = "hyphenation")] { |
| 96 |
use hyphenation::{Language, Load, Standard}; |
| 97 |
use textwrap::{fill, Options, WordSplitter}; |
| 98 |
|
| 99 |
let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap(); |
| 100 |
let options = textwrap::Options::new(28).word_splitter(WordSplitter::Hyphenation(dictionary)); |
| 101 |
let text = "textwrap: an efficient and powerful library for wrapping text."; |
| 102 |
|
| 103 |
assert_eq!( |
| 104 |
textwrap::wrap(text, &options), |
| 105 |
vec![ |
| 106 |
"textwrap: an efficient and", |
| 107 |
"powerful library for wrap-", |
| 108 |
"ping text." |
| 109 |
] |
| 110 |
); |
| 111 |
} |
| 112 |
``` |
| 113 |
|
| 114 |
The US-English hyphenation patterns are embedded when you enable the |
| 115 |
`hyphenation` feature. They are licensed under a |
| 116 |
[permissive license][en-us license] and take up about 88 KB in your binary. If |
| 117 |
you need hyphenation for other languages, you need to download a |
| 118 |
[precompiled `.bincode` file][bincode] and load it yourself. Please see the |
| 119 |
[`hyphenation` documentation] for details. |
| 120 |
|
| 121 |
## Wrapping Strings at Compile Time |
| 122 |
|
| 123 |
If your strings are known at compile time, please take a look at the procedural |
| 124 |
macros from the [`textwrap-macros` crate]. |
| 125 |
|
| 126 |
## Examples |
| 127 |
|
| 128 |
The library comes with [a collection][examples] of small example programs that |
| 129 |
show various features. |
| 130 |
|
| 131 |
If you want to see Textwrap in action right away, then take a look at |
| 132 |
[`examples/wasm/`], which shows how to wrap sans-serif, serif, and monospace |
| 133 |
text. It uses WebAssembly and is automatically deployed to |
| 134 |
https://mgeisler.github.io/textwrap/. |
| 135 |
|
| 136 |
For the command-line examples, you’re invited to clone the repository and try |
| 137 |
them out for yourself! Of special note is [`examples/interactive.rs`]. This is a |
| 138 |
demo program which demonstrates most of the available features: you can enter |
| 139 |
text and adjust the width at which it is wrapped interactively. You can also |
| 140 |
adjust the `Options` used to see the effect of different `WordSplitter`s and |
| 141 |
wrap algorithms. |
| 142 |
|
| 143 |
Run the demo with |
| 144 |
|
| 145 |
```sh |
| 146 |
$ cargo run --example interactive |
| 147 |
``` |
| 148 |
|
| 149 |
The demo needs a Linux terminal to function. |
| 150 |
|
| 151 |
## Release History |
| 152 |
|
| 153 |
Please see the [CHANGELOG file] for details on the changes made in each release. |
| 154 |
|
| 155 |
## License |
| 156 |
|
| 157 |
Textwrap can be distributed according to the [MIT license][mit]. Contributions |
| 158 |
will be accepted under the same license. |
| 159 |
|
| 160 |
[crates-io]: https://crates.io/crates/textwrap |
| 161 |
[build-status]: https://github.com/mgeisler/textwrap/actions?query=workflow%3Abuild+branch%3Amain |
| 162 |
[codecov]: https://codecov.io/gh/mgeisler/textwrap |
| 163 |
[wasm-demo]: https://mgeisler.github.io/textwrap/ |
| 164 |
[cargo-features]: https://docs.rs/textwrap/#cargo-features |
| 165 |
[`textwrap-macros` crate]: https://crates.io/crates/textwrap-macros |
| 166 |
[patterns]: https://github.com/tapeinosyne/hyphenation/tree/master/patterns |
| 167 |
[en-us license]: https://github.com/hyphenation/tex-hyphen/blob/master/hyph-utf8/tex/generic/hyph-utf8/patterns/tex/hyph-en-us.tex |
| 168 |
[bincode]: https://github.com/tapeinosyne/hyphenation/tree/master/dictionaries |
| 169 |
[`hyphenation` documentation]: https://docs.rs/hyphenation |
| 170 |
[examples]: https://github.com/mgeisler/textwrap/tree/main/examples |
| 171 |
[`examples/wasm/`]: https://github.com/mgeisler/textwrap/tree/main/examples/wasm |
| 172 |
[`examples/interactive.rs`]: https://github.com/mgeisler/textwrap/blob/main/examples/interactive.rs |
| 173 |
[api-docs]: https://docs.rs/textwrap/ |
| 174 |
[CHANGELOG file]: https://github.com/mgeisler/textwrap/blob/main/CHANGELOG.md |
| 175 |
[mit]: LICENSE |