Skip to content

Commit

Permalink
Wrapper: add squeeze_whitespace option
Browse files Browse the repository at this point in the history
With this option, whitespace between words can be significant. That
is, whitespace added for alignment purposes can be kept in the output.
This means that wrapping strings like

  -v:        be very verbose and output lots of messages
  --foo:     inject extra foos in the output
  --foobar:  enable the foobar optimization (if possible)

can end up looking like

  -v:        be very verbose and
  output lots of messages
  --foo:     inject extra foos
  in the output
  --foobar:  enable the foobar
  optimization (if possible)

Fixes #28. Related to clap-rs/clap#617.
  • Loading branch information
mgeisler committed May 18, 2017
1 parent 1c9da17 commit af89924
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 af89924

Please sign in to comment.