Skip to content

Commit

Permalink
Add wrap, fill, refill methods to Options
Browse files Browse the repository at this point in the history
These functions all take an `Options` as a parameter. I usually
construct an `Options` and then use it for multiple calls, so it's more
convenient for me to have access to these as methods.
  • Loading branch information
9999years committed Oct 18, 2023
1 parent 04d36f1 commit eb18fcf
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Options for wrapping text.

use std::borrow::Cow;

use crate::{LineEnding, WordSeparator, WordSplitter, WrapAlgorithm};

/// Holds configuration options for wrapping and filling text.
Expand Down Expand Up @@ -288,6 +290,39 @@ impl<'a> Options<'a> {
word_splitter,
}
}

/// Wrap a line of text at a given width.
///
/// The result is a vector of lines, each line is of type [`Cow<'_,
/// str>`](Cow), which means that the line will borrow from the input
/// `&str` if possible. The lines do not have trailing whitespace,
/// including a final `'\n'`. Please use [`fill()`](crate::fill()) if
/// you need a [`String`] instead.
///
/// See [`crate::wrap`] for more details.
pub fn wrap<'s>(&self, text: &'s str) -> Vec<Cow<'s, str>> {
crate::wrap(text, self)
}

/// Fill a line of text at a given width.
///
/// The result is a [`String`], complete with newlines between each
/// line. Use [`wrap()`] if you need access to the individual lines.
///
/// See [`crate::fill`] for more information.
pub fn fill(&self, text: &str) -> String {
crate::fill(text, self)
}

/// Unpack a paragraph of already-wrapped text.
///
/// This function attempts to recover the original text from a single
/// paragraph of wrapped text, such as what [`fill()`] would produce.
///
/// See [`crate::refill`] for more details.
pub fn refill(&self, filled_text: &str) -> String {
crate::refill(filled_text, self)
}
}

#[cfg(test)]
Expand Down

0 comments on commit eb18fcf

Please sign in to comment.