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