Crate textwrap[][src]

Expand description

The textwrap library provides functions for word wrapping and indenting text.

Wrapping Text

Wrapping text can be very useful in command-line programs where you want to format dynamic output nicely so it looks good in a terminal. A quick example:

fn main() {
    let text = "textwrap: a small library for wrapping text.";
    println!("{}", textwrap::fill(text, 18));
}

When you run this program, it will display the following output:

textwrap: a small
library for
wrapping text.

If you enable the hyphenation Cargo feature, you can get automatic hyphenation for a number of languages:

use hyphenation::{Language, Load, Standard};
use textwrap::{fill, Options};

fn main() {
    let text = "textwrap: a small library for wrapping text.";
    let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();
    let options = Options::new(18).word_splitter(dictionary);
    println!("{}", fill(text, &options));
}

The program will now output:

textwrap: a small
library for wrap-
ping text.

See also the unfill and refill functions which allow you to manipulate already wrapped text.

Wrapping Strings at Compile Time

If your strings are known at compile time, please take a look at the procedural macros from the textwrap-macros crate.

Displayed Width vs Byte Size

To word wrap text, one must know the width of each word so one can know when to break lines. This library will by default measure the width of text using the displayed width, not the size in bytes. The unicode-width Cargo feature controls this.

This is important for non-ASCII text. ASCII characters such as a and ! are simple and take up one column each. This means that the displayed width is equal to the string length in bytes. However, non-ASCII characters and symbols take up more than one byte when UTF-8 encoded: é is 0xc3 0xa9 (two bytes) and is 0xe2 0x9a 0x99 (three bytes) in UTF-8, respectively.

This is why we take care to use the displayed width instead of the byte count when computing line lengths. All functions in this library handle Unicode characters like this when the unicode-width Cargo feature is enabled (it is enabled by default).

Indentation and Dedentation

The textwrap library also offers functions for adding a prefix to every line of a string and to remove leading whitespace. As an example, the indent function allows you to turn lines of text into a bullet list:

let before = "\
foo
bar
baz
";
let after = "\
* foo
* bar
* baz
";
assert_eq!(textwrap::indent(before, "* "), after);

Removing leading whitespace is done with dedent:

let before = "
    Some
      indented
        text
";
let after = "
Some
  indented
    text
";
assert_eq!(textwrap::dedent(before), after);

Cargo Features

The textwrap library can be slimmed down as needed via a number of Cargo features. This means you only pay for the features you actually use.

The full dependency graph, where dashed lines indicate optional dependencies, is shown below:

Default Features

These features are enabled by default:

  • unicode-linebreak: enables finding words using the unicode-linebreak crate, which implements the line breaking algorithm described in Unicode Standard Annex #14.

    This feature can be disabled if you are happy to find words separated by ASCII space characters only. People wrapping text with emojis or East-Asian characters will want most likely want to enable this feature. See the word_separators::WordSeparator trait for details.

  • unicode-width: enables correct width computation of non-ASCII characters via the unicode-width crate. Without this feature, every char is 1 column wide, except for emojis which are 2 columns wide. See the core::display_width function for details.

    This feature can be disabled if you only need to wrap ASCII text, or if the functions in core are used directly with core::Fragments for which the widths have been computed in other ways.

  • smawk: enables linear-time wrapping of the whole paragraph via the smawk crate. See the wrap_algorithms::wrap_optimal_fit function for details on the optimal-fit algorithm.

    This feature can be disabled if you only ever intend to use wrap_algorithms::wrap_first_fit.

Optional Features

These Cargo features enable new functionality:

Modules

core

Building blocks for advanced wrapping functionality.

word_separators

Functionality for finding words.

word_splitters

Word splitting functionality.

wrap_algorithms

Word wrapping algorithms.

Structs

Options

Holds settings for wrapping and filling text.

Functions

dedent

Removes common leading whitespace from each line.

fill

Fill a line of text at a given width.

fill_inplace

Fill text in-place without reallocating the input string.

indent

Indent each line by the given prefix.

refill

Refill a paragraph of wrapped text with a new width.

termwidth

Return the current terminal width.

unfill

Unpack a paragraph of already-wrapped text.

wrap

Wrap a line of text at a given width.

wrap_columns

Wrap text into columns with a given total width.