gecko-dev/third_party/rust/textwrap
Andreas Tolfsen 318d3094c5 Bug 1414254 - Vendor Rust dependencies r=jgraham
MozReview-Commit-ID: CnT3DLl2KBR

--HG--
rename : third_party/rust/base64-0.5.2/LICENSE-APACHE => third_party/rust/cc/LICENSE-APACHE
rename : third_party/rust/gcc-0.3.42/LICENSE-MIT => third_party/rust/cc/LICENSE-MIT
rename : third_party/rust/gcc-0.3.42/src/bin/gcc-shim.rs => third_party/rust/cc/src/bin/gcc-shim.rs
rename : third_party/rust/gcc-0.3.42/src/registry.rs => third_party/rust/cc/src/registry.rs
rename : third_party/rust/clap/.cargo-checksum.json => third_party/rust/clap-2.25.0/.cargo-checksum.json
rename : third_party/rust/clap-2.24.2/.clog.toml => third_party/rust/clap-2.25.0/.clog.toml
rename : third_party/rust/clap-2.24.2/.github/CONTRIBUTING.md => third_party/rust/clap-2.25.0/.github/CONTRIBUTING.md
rename : third_party/rust/clap-2.24.2/.github/ISSUE_TEMPLATE.md => third_party/rust/clap-2.25.0/.github/ISSUE_TEMPLATE.md
rename : third_party/rust/clap-2.24.2/.travis.yml => third_party/rust/clap-2.25.0/.travis.yml
rename : third_party/rust/clap/CHANGELOG.md => third_party/rust/clap-2.25.0/CHANGELOG.md
rename : third_party/rust/clap-2.24.2/CONTRIBUTORS.md => third_party/rust/clap-2.25.0/CONTRIBUTORS.md
rename : third_party/rust/clap/Cargo.toml => third_party/rust/clap-2.25.0/Cargo.toml
rename : third_party/rust/clap-2.24.2/LICENSE-MIT => third_party/rust/clap-2.25.0/LICENSE-MIT
rename : third_party/rust/clap/README.md => third_party/rust/clap-2.25.0/README.md
rename : third_party/rust/clap-2.24.2/index.html => third_party/rust/clap-2.25.0/index.html
rename : third_party/rust/clap-2.24.2/justfile => third_party/rust/clap-2.25.0/justfile
rename : third_party/rust/clap-2.24.2/rustfmt.toml => third_party/rust/clap-2.25.0/rustfmt.toml
rename : third_party/rust/clap/src/app/help.rs => third_party/rust/clap-2.25.0/src/app/help.rs
rename : third_party/rust/clap/src/app/macros.rs => third_party/rust/clap-2.25.0/src/app/macros.rs
rename : third_party/rust/clap-2.24.2/src/app/meta.rs => third_party/rust/clap-2.25.0/src/app/meta.rs
rename : third_party/rust/clap-2.24.2/src/app/mod.rs => third_party/rust/clap-2.25.0/src/app/mod.rs
rename : third_party/rust/clap/src/app/parser.rs => third_party/rust/clap-2.25.0/src/app/parser.rs
rename : third_party/rust/clap/src/app/settings.rs => third_party/rust/clap-2.25.0/src/app/settings.rs
rename : third_party/rust/clap/src/app/validator.rs => third_party/rust/clap-2.25.0/src/app/validator.rs
rename : third_party/rust/clap-2.24.2/src/args/any_arg.rs => third_party/rust/clap-2.25.0/src/args/any_arg.rs
rename : third_party/rust/clap-2.24.2/src/args/arg.rs => third_party/rust/clap-2.25.0/src/args/arg.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_builder/base.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/base.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_builder/flag.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/flag.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_builder/mod.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/mod.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_builder/option.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/option.rs
rename : third_party/rust/clap/src/args/arg_builder/positional.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/positional.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_builder/switched.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/switched.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_builder/valued.rs => third_party/rust/clap-2.25.0/src/args/arg_builder/valued.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_matcher.rs => third_party/rust/clap-2.25.0/src/args/arg_matcher.rs
rename : third_party/rust/clap-2.24.2/src/args/arg_matches.rs => third_party/rust/clap-2.25.0/src/args/arg_matches.rs
rename : third_party/rust/clap-2.24.2/src/args/matched_arg.rs => third_party/rust/clap-2.25.0/src/args/matched_arg.rs
rename : third_party/rust/clap-2.24.2/src/args/mod.rs => third_party/rust/clap-2.25.0/src/args/mod.rs
rename : third_party/rust/clap-2.24.2/src/args/subcommand.rs => third_party/rust/clap-2.25.0/src/args/subcommand.rs
rename : third_party/rust/clap-2.24.2/src/completions/bash.rs => third_party/rust/clap-2.25.0/src/completions/bash.rs
rename : third_party/rust/clap-2.24.2/src/completions/fish.rs => third_party/rust/clap-2.25.0/src/completions/fish.rs
rename : third_party/rust/clap-2.24.2/src/completions/macros.rs => third_party/rust/clap-2.25.0/src/completions/macros.rs
rename : third_party/rust/clap-2.24.2/src/completions/mod.rs => third_party/rust/clap-2.25.0/src/completions/mod.rs
rename : third_party/rust/clap-2.24.2/src/completions/shell.rs => third_party/rust/clap-2.25.0/src/completions/shell.rs
rename : third_party/rust/clap-2.24.2/src/completions/zsh.rs => third_party/rust/clap-2.25.0/src/completions/zsh.rs
rename : third_party/rust/clap/src/lib.rs => third_party/rust/clap-2.25.0/src/lib.rs
rename : third_party/rust/clap/src/macros.rs => third_party/rust/clap-2.25.0/src/macros.rs
rename : third_party/rust/clap-2.24.2/src/osstringext.rs => third_party/rust/clap-2.25.0/src/osstringext.rs
rename : third_party/rust/clap-2.24.2/src/strext.rs => third_party/rust/clap-2.25.0/src/strext.rs
rename : third_party/rust/clap/src/suggestions.rs => third_party/rust/clap-2.25.0/src/suggestions.rs
rename : third_party/rust/clap-2.24.2/src/usage_parser.rs => third_party/rust/clap-2.25.0/src/usage_parser.rs
rename : third_party/rust/gcc-0.3.42/LICENSE-APACHE => third_party/rust/libc-0.2.30/LICENSE-APACHE
rename : third_party/rust/log-0.3.6/LICENSE-MIT => third_party/rust/libc-0.2.30/LICENSE-MIT
rename : third_party/rust/num-integer/.cargo-checksum.json => third_party/rust/num-integer-0.1.33/.cargo-checksum.json
rename : third_party/rust/num-integer/Cargo.toml => third_party/rust/num-integer-0.1.33/Cargo.toml
rename : third_party/rust/log-0.3.6/LICENSE-APACHE => third_party/rust/num-integer-0.1.33/LICENSE-APACHE
rename : third_party/rust/num-traits-0.1.37/LICENSE-MIT => third_party/rust/num-integer-0.1.33/LICENSE-MIT
rename : third_party/rust/num-integer/src/lib.rs => third_party/rust/num-integer-0.1.33/src/lib.rs
rename : third_party/rust/num-traits/.cargo-checksum.json => third_party/rust/num-traits-0.1.39/.cargo-checksum.json
rename : third_party/rust/num-traits/Cargo.toml => third_party/rust/num-traits-0.1.39/Cargo.toml
rename : third_party/rust/num-traits-0.1.37/LICENSE-APACHE => third_party/rust/num-traits-0.1.39/LICENSE-APACHE
rename : third_party/rust/regex-0.2.1/LICENSE-MIT => third_party/rust/num-traits-0.1.39/LICENSE-MIT
rename : third_party/rust/num-traits-0.1.37/src/float.rs => third_party/rust/num-traits-0.1.39/src/float.rs
rename : third_party/rust/num-traits-0.1.37/src/int.rs => third_party/rust/num-traits-0.1.39/src/int.rs
rename : third_party/rust/num-traits-0.1.37/src/ops/mod.rs => third_party/rust/num-traits-0.1.39/src/ops/mod.rs
rename : third_party/rust/num-traits-0.1.37/src/ops/saturating.rs => third_party/rust/num-traits-0.1.39/src/ops/saturating.rs
rename : third_party/rust/num-traits-0.1.37/src/pow.rs => third_party/rust/num-traits-0.1.39/src/pow.rs
rename : third_party/rust/num_cpus-1.2.1/.travis.yml => third_party/rust/num_cpus-1.6.2/.travis.yml
rename : third_party/rust/num_cpus-1.2.1/LICENSE-APACHE => third_party/rust/num_cpus-1.6.2/LICENSE-APACHE
rename : third_party/rust/num_cpus-1.2.1/LICENSE-MIT => third_party/rust/num_cpus-1.6.2/LICENSE-MIT
rename : third_party/rust/textwrap/.appveyor.yml => third_party/rust/textwrap-0.6.0/.appveyor.yml
rename : third_party/rust/textwrap/.cargo-checksum.json => third_party/rust/textwrap-0.6.0/.cargo-checksum.json
rename : third_party/rust/textwrap/.travis.yml => third_party/rust/textwrap-0.6.0/.travis.yml
rename : third_party/rust/textwrap/Cargo.toml => third_party/rust/textwrap-0.6.0/Cargo.toml
rename : third_party/rust/textwrap/README.md => third_party/rust/textwrap-0.6.0/README.md
rename : third_party/rust/textwrap/benches/linear.rs => third_party/rust/textwrap-0.6.0/benches/linear.rs
rename : third_party/rust/textwrap/examples/layout.rs => third_party/rust/textwrap-0.6.0/examples/layout.rs
rename : third_party/rust/textwrap/examples/termwidth.rs => third_party/rust/textwrap-0.6.0/examples/termwidth.rs
rename : third_party/rust/textwrap/src/lib.rs => third_party/rust/textwrap-0.6.0/src/lib.rs
extra : rebase_source : 75ee4cb067c477d9bd7921594fd0138355392635
2017-11-03 13:39:05 +00:00
..
benches Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
examples Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
src Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
tests Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
.appveyor.yml Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
.cargo-checksum.json Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
.rustfmt.toml Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
.travis.yml Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
Cargo.toml Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00
LICENSE
README.md Bug 1414254 - Vendor Rust dependencies r=jgraham 2017-11-03 13:39:05 +00:00

README.md

Textwrap

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.

Usage

Add this to your Cargo.toml:

[dependencies]
textwrap = "0.9"

and this to your crate root:

extern crate textwrap;

If you would like to have automatic hyphenation, specify the dependency as:

[dependencies]
textwrap = { version = "0.9", features = ["hyphenation"] }

To conveniently wrap text at the current terminal width, enable the term_size feature:

[dependencies]
textwrap = { version = "0.9", features = ["term_size"] }

Documentation

API documentation

Getting Started

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.

With the hyphenation feature, you can get automatic hyphenation for about 70 languages. Your program must load and configure the hyphenation patterns to use:

extern crate hyphenation;
extern crate textwrap;

use hyphenation::Language;
use textwrap::Wrapper;

fn main() {
    let corpus = hyphenation::load(Language::English_US).unwrap();
    let wrapper = Wrapper::with_splitter(18, 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.

Examples

The library comes with some small example programs that shows various features.

Layout Example

The layout example shows how a fixed example string is wrapped at different widths. Run the example with:

$ 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.

Terminal Width Example

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:

Formatted in within 60 columns:
----
Memory safety without garbage collection. Concurrency
without data races. Zero-cost abstractions.
----

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
Formatted in within 80 columns:
----
Memory safety without garbage collection. Concurrency without data races. Zero-
cost abstractions.
----

Release History

This section lists the largest changes per release.

Version 0.9.0 — October 5th, 2017

The dependency on term_size is now optional, and by default this feature is not enabled. This is a breaking change for users of Wrapper::with_termwidth. Enable the term_size feature to restore the old functionality.

Added a regression test for case where width is set to usize::MAX. Thanks @Fraser999! All public structs now implement Debug.

Issues closed:

  • Fixed #101: Remove term_size as a (hard required) dependency.

Version 0.8.0 — September 4th, 2017

The Wrapper stuct is now generic over the type of word splitter being used. This means less boxing and a nicer API. The Wrapper::word_splitter method has been removed. This is a breaking API change if you used the method to change the word splitter.

The Wrapper struct has two new methods that will wrap the input text lazily: Wrapper::wrap_iter and Wrapper::into_wrap_iter. Use those if you will be iterating over the wrapped lines one by one.

Issues closed:

  • Fixed #59: wrap could return an iterator. Thanks @hcpl!

  • Fixed #81: Set html_root_url

Version 0.7.0 — July 20th, 2017

Version 0.7.0 changes the return type of Wrapper::wrap from Vec<String> to Vec<Cow<'a, str>>. This means that the output lines borrow data from the input string. This is a breaking API change if you relied on the exact return type of Wrapper::wrap. Callers of the textwrap::fill convenience function will see no breakage.

The above change and other optimizations makes version 0.7.0 roughly 15-30% faster than version 0.6.0.

The squeeze_whitespace option has been removed since it was complicating the above optimization. Let us know if this option is important for you so we can provide a work around.

Issues closed:

  • Fixed #58: Add a "fast_wrap" function that reuses the input string

  • Fixed #61: Documentation errors

Version 0.6.0 — May 22nd, 2017

Version 0.6.0 adds builder methods to Wrapper for easy one-line initialization and configuration:

let wrapper = Wrapper::new(60).break_words(false);

It also add a new NoHyphenation word splitter that will never split words, not even at existing hyphens.

Issues closed:

  • Fixed #28: Support not squeezing whitespace

Version 0.5.0 — May 15th, 2017

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

wrapper.corpus = Some(&corpus);

to

wrapper.splitter = Box::new(corpus);

Other changes include optimizations, so version 0.5.0 is roughly 10-15% faster than version 0.4.0.

Issues closed:

  • Fixed #19: Add support for finding terminal size
  • Fixed #25: Handle words longer than self.width
  • Fixed #26: Support custom indentation
  • Fixed #36: Support building without hyphenation
  • Fixed #39: Respect non-breaking spaces

Version 0.4.0 — January 24th, 2017

Documented complexities and tested these via cargo bench.

  • Fixed #13: Immediatedly add word if it fits
  • Fixed #14: Avoid splitting on initial hyphens in --foo-bar

Version 0.3.0 — January 7th, 2017

Added support for automatic hyphenation.

Version 0.2.0 — December 28th, 2016

Introduced Wrapper struct. Added support for wrapping on hyphens.

Version 0.1.0 — December 17th, 2016

First public release with support for wrapping strings on whitespace.

License

Textwrap can be distributed according to the MIT license. Contributions will be accepted under the same license.