Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrap, fill, refill methods to Options #523

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading