commit | cb1e2a00e169227b4bbb4c70f2af74120a3e55d2 | [log] [tgz] |
---|---|---|
author | Martin Geisler <martin@geisler.net> | Tue Jan 24 06:46:39 2017 |
committer | GitHub <noreply@github.com> | Tue Jan 24 06:46:39 2017 |
tree | 29eec5a25b1b468b068e1b2a4039725fbc7d5dff | |
parent | fab11cdf458592ff50ed22a01a7c86268099dbf8 [diff] | |
parent | 38c018c6c2a91e721ee9c739fb920963b7048a64 [diff] |
Merge pull request #24 from mgeisler/release-0.4.0 Release 0.4.0
Textwrap is a small Rust crate for word wrapping text. You can use it to format strings for display in commandline applications.
Add this to your Cargo.toml
:
[dependencies] textwrap = "0.4"
and this to your crate root:
extern crate textwrap;
Word wrapping single strings is easy using the fill
function:
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.
You can use automatic hyphenation using TeX hyphenation patterns (with support for about 70 languages) and get:
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.corpus = Some(&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 library comes with a small example program that shows how a fixed example string is wrapped at different widths. Run the example with:
$ cargo run --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 a s “zero-cost”) but also how words are hyphenated using automatic/machine hyphenation.
This section lists the largest changes per release.
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.