Skip to content

Commit

Permalink
Merge pull request #48 from mgeisler/squeeze-whitespace
Browse files Browse the repository at this point in the history
Wrapper: add squeeze_whitespace option
  • Loading branch information
mgeisler committed May 19, 2017
2 parents 1c9da17 + af89924 commit c424273
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ pub struct Wrapper<'a> {
/// When set to `false`, some lines be being longer than
/// `self.width`.
pub break_words: bool,
/// This crate treats every whitespace character (space, newline,
/// TAB, ...) as a space character. When this option is set to
/// `true`, the whitespace between words will be squeezed into a
/// single space character. Otherwise, the whitespace is
/// significant and will be kept in the output. Whitespace
/// characters such as TAB will still be turned into a single
/// space -- consider expanding TABs manually if this is a
/// concern.
pub squeeze_whitespace: bool,
/// The method for splitting words. If the `hyphenation` feature
/// is enabled, you can use a `hyphenation::language::Corpus` here
/// to get language-aware hyphenation.
Expand All @@ -227,6 +236,7 @@ impl<'a> Wrapper<'a> {
initial_indent: "",
subsequent_indent: "",
break_words: true,
squeeze_whitespace: false,
splitter: Box::new(HyphenSplitter {}),
}
}
Expand Down Expand Up @@ -301,7 +311,7 @@ impl<'a> Wrapper<'a> {

for mut word in s.split(|c: char| c.is_whitespace() && c != NBSP) {
// Skip over adjacent whitespace characters.
if word.is_empty() {
if self.squeeze_whitespace && word.is_empty() {
continue;
}

Expand Down Expand Up @@ -569,9 +579,25 @@ mod tests {
assert_eq!(wrap("foo bar", 0), vec!["foo", "bar"]);
}

#[test]
fn whitespace_is_significant() {
assert_eq!(wrap("foo: bar baz", 10), vec!["foo: bar", "baz"]);
}

#[test]
fn extra_whitespace_start_of_line() {
// Whitespace is only significant inside a line. After a line
// gets too long and is broken, the first word starts in
// column zero and is not indented. The line before might end
// up with trailing whitespace.
assert_eq!(wrap("foo bar", 5), vec!["foo ", "bar"]);
}

#[test]
fn whitespace_is_squeezed() {
assert_eq!(wrap(" foo \t bar ", 10), vec!["foo bar"]);
let mut wrapper = Wrapper::new(10);
wrapper.squeeze_whitespace = true;
assert_eq!(wrapper.wrap(" foo \t bar "), vec!["foo bar"]);
}

#[test]
Expand Down

0 comments on commit c424273

Please sign in to comment.