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

WrapIterImpl: add support for inputs with line breaks #123

Merged
merged 5 commits into from
Feb 20, 2018
Merged
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
38 changes: 37 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,25 @@ impl<'a> WrapIterImpl<'a> {
while let Some((idx, ch)) = self.char_indices.next() {
let char_width = ch.width().unwrap_or(0);
let char_len = ch.len_utf8();
if is_whitespace(ch) {

if ch == '\n' {
self.split = idx;
self.split_len = char_len;
self.line_width_at_split = self.line_width;
self.in_whitespace = false;

// If this is not the final line, return the current line. Otherwise,
// we will return the line with its line break after exiting the loop
if self.split + self.split_len < self.source.len() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't actually try, but would it be possible to combine the logic here with the existing logic that kicks in when the current line it too long? Maybe by changing the conditions slightly:

if ch != '\n' && is_whitespace(ch) {
    // ...
} else if ch == `\n` || self.line_width + char_width > wrapper.width {
    // ...
}

No idea if it works! :-D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm maybe but I can't really seem to get it working this way ^^ feel free to commit if you find a way

let mut line = self.create_result_line(wrapper);
line += &self.source[self.start..self.split];

self.start = self.split + self.split_len;
self.line_width = wrapper.subsequent_indent.width();

return Some(line);
}
} else if is_whitespace(ch) {
// Extend the previous split or create a new one.
if self.in_whitespace {
self.split_len += char_len;
Expand Down Expand Up @@ -1135,6 +1153,24 @@ mod tests {
assert_eq!(wrap("foobar", 0), vec!["f", "o", "o", "b", "a", "r"]);
}

#[test]
fn break_words_line_breaks() {
assert_eq!(fill("ab\ncdefghijkl", 5), "ab\ncdefg\nhijkl");
assert_eq!(fill("abcdefgh\nijkl", 5), "abcde\nfgh\nijkl");
}

#[test]
fn preserve_line_breaks() {
assert_eq!(fill("test\n", 11), "test\n");
assert_eq!(fill("test\n\na\n\n", 11), "test\n\na\n\n");
assert_eq!(fill("1 3 5 7\n1 3 5 7", 7), "1 3 5 7\n1 3 5 7");
}

#[test]
fn wrap_preserve_line_breaks() {
assert_eq!(fill("1 3 5 7\n1 3 5 7", 5), "1 3 5\n7\n1 3 5\n7");
}

#[test]
fn non_breaking_space() {
let wrapper = Wrapper::new(5).break_words(false);
Expand Down