Enum textwrap::wrap_algorithms::WrapAlgorithm
source · 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
Word
s 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
impl WrapAlgorithm
sourcepub const fn new() -> Self
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
.
sourcepub const fn new_optimal_fit() -> Self
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.
sourcepub fn wrap<'a, 'b>(
&self,
words: &'b [Word<'a>],
line_widths: &'b [usize],
) -> Vec<&'b [Word<'a>]>
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
impl Clone for WrapAlgorithm
source§fn clone(&self) -> WrapAlgorithm
fn clone(&self) -> WrapAlgorithm
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for WrapAlgorithm
impl Debug for WrapAlgorithm
source§impl Default for WrapAlgorithm
impl Default for WrapAlgorithm
source§impl PartialEq for WrapAlgorithm
impl PartialEq for WrapAlgorithm
source§fn eq(&self, other: &Self) -> bool
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]));
impl Copy for WrapAlgorithm
Auto Trait Implementations§
impl Freeze for WrapAlgorithm
impl RefUnwindSafe for WrapAlgorithm
impl Send for WrapAlgorithm
impl Sync for WrapAlgorithm
impl Unpin for WrapAlgorithm
impl UnwindSafe for WrapAlgorithm
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)