Textwrap is a small Rust crate for word wrapping text. You can use it to format strings for display in commandline applications. The crate name and interface is inspired by the Python textwrap module.
Add this to your Cargo.toml
:
toml
[dependencies]
textwrap = "0.5"
and this to your crate root:
rust
extern crate textwrap;
If you would like to have automatic hyphenation, specify the
dependency as:
toml
[dependencies]
textwrap = { version: "0.5", features: ["hyphenation"] }
Word wrapping single strings is easy using the fill
function:
```rust
extern crate textwrap;
use textwrap::fill;
fn main() {
let text = "textwrap: a small library for wrapping text.";
println!("{}", fill(text, 18));
}
The output is
textwrap: a small
library for
wrapping text.
```
With the hyphenation
feature, you can get automatic hyphenation
for about 70 languages. Your program must load and
configure the hyphenation patterns to use:
```rust
extern crate hyphenation;
extern crate textwrap;
use hyphenation::Language; use textwrap::Wrapper;
fn main() { let corpus = hyphenation::load(Language::English_US).unwrap(); let mut wrapper = Wrapper::new(18); wrapper.splitter = Box::new(corpus); let text = "textwrap: a small library for wrapping text."; println!("{}", wrapper.fill(text)) } ```
The output now looks like this:
textwrap: a small
library for wrap-
ping text.
The hyphenation uses high-quality TeX hyphenation patterns.
The library comes with a small example programs that shows various features.
The layout
example shows how a fixed example string is wrapped at
different widths. Run the example with:
shell
$ cargo run --features hyphenation --example layout
The program will use the following string:
Memory safety without garbage collection. Concurrency without data races. Zero-cost abstractions.
The string is wrapped at all widths between 15 and 60 columns. With narrow columns the output looks like this:
.--- Width: 15 ---.
| Memory safety |
| without garbage |
| collection. |
| Concurrency |
| without data |
| races. Zero- |
| cost abstrac- |
| tions. |
.--- Width: 16 ----.
| Memory safety |
| without garbage |
| collection. Con- |
| currency without |
| data races. Ze- |
| ro-cost abstrac- |
| tions. |
Later, longer lines are used and the output now looks like this:
.-------------------- Width: 49 --------------------.
| Memory safety without garbage collection. Concur- |
| rency without data races. Zero-cost abstractions. |
.---------------------- Width: 53 ----------------------.
| Memory safety without garbage collection. Concurrency |
| without data races. Zero-cost abstractions. |
.------------------------- Width: 59 -------------------------.
| Memory safety without garbage collection. Concurrency with- |
| out data races. Zero-cost abstractions. |
Notice how words are split at hyphens (such as "zero-cost") but also how words are hyphenated using automatic/machine hyphenation.
The termwidth
example simply shows how the width can be set
automatically to the current terminal width. Run it with this command:
$ cargo run --example termwidth
If you run it in a narrow terminal, you'll see output like this: ```
Memory safety without garbage collection. Concurrency
```
If stdout
is not connected to the terminal, the program will use a
default of 80 columns for the width:
``` $ cargo run --example termwidth | cat
Memory safety without garbage collection. Concurrency without data races. Zero-
```
This section lists the largest changes per release.
Version 0.5.0 has breaking API changes. However, this only affects
code using the hyphenation feature. The feature is now optional, so
you will first need to enable the hyphenation
feature as described
above. Afterwards, please change your code from
rust
wrapper.corpus = Some(&corpus);
to
rust
wrapper.splitter = corpus;
Other changes include optimizations, so version 0.5.0 is roughly 10-15% faster than version 0.4.0.
Issues closed:
self.width
hyphenation
Documented complexities and tested these via cargo bench
.
--foo-bar
Added support for automatic hyphenation.
Introduced Wrapper
struct. Added support for wrapping on hyphens.
First public release with support for wrapping strings on whitespace.
Textwrap can be distributed according to the MIT license. Contributions will be accepted under the same license.