pub enum WrapAlgorithm {
    FirstFit,
    OptimalFit(Penalties),
    Custom(for<'a, 'b> fn(words: &'b [Word<'a>], line_widths: &'b [usize]) -> Vec<&'b [Word<'a>]>),
}
Expand description

Describes how to wrap words into lines.

The simplest approach is to wrap words one word at a time and accept the first way of wrapping which fit (WrapAlgorithm::FirstFit). If the smawk Cargo feature is enabled, a more complex algorithm is available which will look at an entire paragraph at a time in order to find optimal line breaks (WrapAlgorithm::OptimalFit).

Variants§

§

FirstFit

Wrap words using a fast and simple algorithm.

This algorithm uses no look-ahead when finding line breaks. Implemented by wrap_first_fit(), please see that function for details and examples.

§

OptimalFit(Penalties)

Wrap words using an advanced algorithm with look-ahead.

This wrapping algorithm considers the entire paragraph to find optimal line breaks. When wrapping text, “penalties” are assigned to line breaks based on the gaps left at the end of lines. See Penalties for details.

The underlying wrapping algorithm is implemented by wrap_optimal_fit(), please see that function for examples.

Note: Only available when the smawk Cargo feature is enabled.

§

Custom(for<'a, 'b> fn(words: &'b [Word<'a>], line_widths: &'b [usize]) -> Vec<&'b [Word<'a>]>)

Custom wrapping function.

Use this if you want to implement your own wrapping algorithm. The function can freely decide how to turn a slice of Words into lines.

§Example

use textwrap::core::Word;
use textwrap::{wrap, Options, WrapAlgorithm};

fn stair<'a, 'b>(words: &'b [Word<'a>], _: &'b [usize]) -> Vec<&'b [Word<'a>]> {
    let mut lines = Vec::new();
    let mut step = 1;
    let mut start_idx = 0;
    while start_idx + step <= words.len() {
      lines.push(&words[start_idx .. start_idx+step]);
      start_idx += step;
      step += 1;
    }
    lines
}

let options = Options::new(10).wrap_algorithm(WrapAlgorithm::Custom(stair));
assert_eq!(wrap("First, second, third, fourth, fifth, sixth", options),
           vec!["First,",
                "second, third,",
                "fourth, fifth, sixth"]);

Implementations§

source§

impl WrapAlgorithm

source

pub const fn new() -> Self

Create new wrap algorithm.

The best wrapping algorithm is used by default, i.e., WrapAlgorithm::OptimalFit if available, otherwise WrapAlgorithm::FirstFit.

source

pub const fn new_optimal_fit() -> Self

New WrapAlgorithm::OptimalFit with default penalties. This works well for monospace text.

Note: Only available when the smawk Cargo feature is enabled.

source

pub fn wrap<'a, 'b>( &self, words: &'b [Word<'a>], line_widths: &'b [usize] ) -> Vec<&'b [Word<'a>]>

Wrap words according to line widths.

The line_widths slice gives the target line width for each line (the last slice element is repeated as necessary). This can be used to implement hanging indentation.

Trait Implementations§

source§

impl Clone for WrapAlgorithm

source§

fn clone(&self) -> WrapAlgorithm

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for WrapAlgorithm

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for WrapAlgorithm

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl PartialEq for WrapAlgorithm

source§

fn eq(&self, other: &Self) -> bool

Compare two wrap algorithms.

use textwrap::WrapAlgorithm;

assert_eq!(WrapAlgorithm::FirstFit, WrapAlgorithm::FirstFit);
#[cfg(feature = "smawk")] {
    assert_eq!(WrapAlgorithm::new_optimal_fit(), WrapAlgorithm::new_optimal_fit());
}

Note that WrapAlgorithm::Custom values never compare equal:

use textwrap::WrapAlgorithm;

assert_ne!(WrapAlgorithm::Custom(|words, line_widths| vec![words]),
           WrapAlgorithm::Custom(|words, line_widths| vec![words]));
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for WrapAlgorithm

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.