Skip to content

Commit

Permalink
Format directly into draw state buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 2, 2022
1 parent d307a59 commit 307240e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/progress_bar.rs
Expand Up @@ -264,9 +264,9 @@ impl ProgressBar {
.extend(msg.as_ref().lines().map(Into::into));
draw_state.orphan_lines = draw_state.lines.len();
if draw_lines {
draw_state
.lines
.extend(state.style.format_state(&state, width));
state
.style
.format_state(&state, &mut draw_state.lines, width);
}

drop(draw_state);
Expand Down
9 changes: 5 additions & 4 deletions src/state.rs
Expand Up @@ -120,10 +120,11 @@ impl BarState {
// finished progress bar, so it's kept as to not cause compatibility issues in weird cases.
draw_state.force_draw = force_draw || self.state.is_finished();

draw_state.lines = match self.state.should_render() {
true => self.state.style.format_state(&self.state, width),
false => Vec::new(),
};
if self.state.should_render() {
self.state
.style
.format_state(&self.state, &mut draw_state.lines, width);
}

drop(draw_state);
drawable.draw()
Expand Down
56 changes: 34 additions & 22 deletions src/style.rs
Expand Up @@ -227,10 +227,14 @@ impl ProgressStyle {
}
}

pub(crate) fn format_state(&self, state: &ProgressState, target_width: usize) -> Vec<String> {
pub(crate) fn format_state(
&self,
state: &ProgressState,
lines: &mut Vec<String>,
target_width: usize,
) {
let mut cur = String::new();
let mut buf = String::new();
let mut rv = vec![];
let mut wide = None;
for part in &self.template.parts {
match part {
Expand Down Expand Up @@ -359,7 +363,7 @@ impl ProgressStyle {
}
}
TemplatePart::Literal(s) => cur.push_str(s),
TemplatePart::NewLine => rv.push(match wide {
TemplatePart::NewLine => lines.push(match wide {
Some(inner) => {
inner.expand(mem::take(&mut cur), self, state, &mut buf, target_width)
}
Expand All @@ -369,14 +373,13 @@ impl ProgressStyle {
}

if !cur.is_empty() {
rv.push(match wide {
lines.push(match wide {
Some(inner) => {
inner.expand(mem::take(&mut cur), self, state, &mut buf, target_width)
}
None => mem::take(&mut cur),
})
}
rv
}
}

Expand Down Expand Up @@ -713,46 +716,55 @@ mod tests {

#[test]
fn test_expand_template() {
let mut style = ProgressStyle::default_bar();
style.format_map.0.insert("foo", |_| "FOO".into());
style.format_map.0.insert("bar", |_| "BAR".into());
let draw_target = ProgressDrawTarget::stdout();
let width = draw_target.width();
let state = ProgressState::new(10);
let mut buf = Vec::new();

let mut style = ProgressStyle::default_bar();
style.format_map.0.insert("foo", |_| "FOO".into());
style.format_map.0.insert("bar", |_| "BAR".into());

style.template = Template::from_str("{{ {foo} {bar} }}");
let rv = style.format_state(&state, width);
assert_eq!(&rv[0], "{ FOO BAR }");
style.format_state(&state, &mut buf, width);
assert_eq!(&buf[0], "{ FOO BAR }");

buf.clear();
style.template = Template::from_str(r#"{ "foo": "{foo}", "bar": {bar} }"#);
let rv = style.format_state(&state, width);
assert_eq!(&rv[0], r#"{ "foo": "FOO", "bar": BAR }"#);
style.format_state(&state, &mut buf, width);
assert_eq!(&buf[0], r#"{ "foo": "FOO", "bar": BAR }"#);
}

#[test]
fn test_expand_template_flags() {
use console::set_colors_enabled;
set_colors_enabled(true);
let mut style = ProgressStyle::default_bar();
style.format_map.0.insert("foo", |_| "XXX".into());

let draw_target = ProgressDrawTarget::stdout();
let width = draw_target.width();
let state = ProgressState::new(10);
let mut buf = Vec::new();

let mut style = ProgressStyle::default_bar();
style.format_map.0.insert("foo", |_| "XXX".into());

style.template = Template::from_str("{foo:5}");
let rv = style.format_state(&state, width);
assert_eq!(&rv[0], "XXX ");
style.format_state(&state, &mut buf, width);
assert_eq!(&buf[0], "XXX ");

buf.clear();
style.template = Template::from_str("{foo:.red.on_blue}");
let rv = style.format_state(&state, width);
assert_eq!(&rv[0], "\u{1b}[31m\u{1b}[44mXXX\u{1b}[0m");
style.format_state(&state, &mut buf, width);
assert_eq!(&buf[0], "\u{1b}[31m\u{1b}[44mXXX\u{1b}[0m");

buf.clear();
style.template = Template::from_str("{foo:^5.red.on_blue}");
let rv = style.format_state(&state, width);
assert_eq!(&rv[0], "\u{1b}[31m\u{1b}[44m XXX \u{1b}[0m");
style.format_state(&state, &mut buf, width);
assert_eq!(&buf[0], "\u{1b}[31m\u{1b}[44m XXX \u{1b}[0m");

buf.clear();
style.template = Template::from_str("{foo:^5.red.on_blue/green.on_cyan}");
let rv = style.format_state(&state, width);
assert_eq!(&rv[0], "\u{1b}[31m\u{1b}[44m XXX \u{1b}[0m");
style.format_state(&state, &mut buf, width);
assert_eq!(&buf[0], "\u{1b}[31m\u{1b}[44m XXX \u{1b}[0m");
}
}

0 comments on commit 307240e

Please sign in to comment.