Skip to content

Commit

Permalink
Improve multiline support in format_style
Browse files Browse the repository at this point in the history
During format_style, we now split up expanded
lines before they are added, accounting for the
fact that there could be additional newlines in
the line we're adding.
  • Loading branch information
happenslol committed Sep 17, 2022
1 parent 271ebbd commit fc29f46
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions src/style.rs
Expand Up @@ -379,10 +379,23 @@ impl ProgressStyle {
target_width: u16,
wide: &Option<WideElement>,
) {
lines.push(match wide {
let expanded = match wide {
Some(inner) => inner.expand(mem::take(cur), self, state, buf, target_width),
None => mem::take(cur),
});
};

// If there are newlines, we need to split them up
// and add the lines separately so that they're counted
// correctly on re-render.
for (i, line) in expanded.split('\n').enumerate() {
// No newlines found in this case
if i == 0 && line.len() == expanded.len() {
lines.push(expanded);
break;
}

lines.push(line.to_string());
}
}
}

Expand Down Expand Up @@ -938,4 +951,42 @@ mod tests {
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "\u{1b}[31m\u{1b}[44m foobar \u{1b}[0m");
}

#[test]
fn multiline_handling() {
const WIDTH: u16 = 80;
let pos = Arc::new(AtomicPosition::new());
let mut state = ProgressState::new(Some(10), pos);
let mut buf = Vec::new();

let mut style = ProgressStyle::default_bar();
state.message = TabExpandedString::new("foo\nbar\nbaz".into(), 2);
style.template = Template::from_str("{msg}").unwrap();
style.format_state(&state, &mut buf, WIDTH);

assert_eq!(buf.len(), 3);
assert_eq!(&buf[0], "foo");
assert_eq!(&buf[1], "bar");
assert_eq!(&buf[2], "baz");

buf.clear();
style.template = Template::from_str("{wide_msg}").unwrap();
style.format_state(&state, &mut buf, WIDTH);

assert_eq!(buf.len(), 3);
assert_eq!(&buf[0], "foo");
assert_eq!(&buf[1], "bar");
assert_eq!(&buf[2], "baz");

buf.clear();
state.prefix = TabExpandedString::new("prefix\nprefix".into(), 2);
style.template = Template::from_str("{prefix} {wide_msg}").unwrap();
style.format_state(&state, &mut buf, WIDTH);

assert_eq!(buf.len(), 4);
assert_eq!(&buf[0], "prefix");
assert_eq!(&buf[1], "prefix foo");
assert_eq!(&buf[2], "bar");
assert_eq!(&buf[3], "baz");
}
}

0 comments on commit fc29f46

Please sign in to comment.