Skip to content

Commit 59cd606

Browse files
RDruonchris-laplante
authored andcommitted
Handle newline in msg and empty msg
1 parent 6d31845 commit 59cd606

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/draw_target.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,15 @@ impl DrawState {
496496
let len = self.lines.len();
497497
let mut real_len = 0;
498498
for (idx, line) in self.lines.iter().enumerate() {
499-
// Calculate real length based on terminal width
500-
// This take in account linewrap from terminal
501-
real_len +=
502-
(console::measure_text_width(line) as f64 / term.width() as f64).ceil() as usize;
503-
499+
if line.is_empty() {
500+
// Empty line are new line
501+
real_len += 1;
502+
} else {
503+
// Calculate real length based on terminal width
504+
// This take in account linewrap from terminal
505+
real_len += (console::measure_text_width(line) as f64 / term.width() as f64).ceil()
506+
as usize;
507+
}
504508
if idx + 1 != len {
505509
term.write_line(line)?;
506510
} else {

src/state.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ impl BarState {
143143
};
144144

145145
let mut draw_state = drawable.state();
146-
draw_state.lines.extend(msg.lines().map(Into::into));
146+
let lines: Vec<String> = msg.lines().map(Into::into).collect();
147+
// Empty msg should trigger newline as we are in println
148+
if lines.is_empty() {
149+
draw_state.lines.push(String::new());
150+
} else {
151+
draw_state.lines.extend(lines);
152+
}
147153
draw_state.orphan_lines_count = draw_state.lines.len();
148154
if !matches!(self.state.status, Status::DoneHidden) {
149155
self.style

tests/render.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,42 @@ n terminal width"#
10441044
.trim()
10451045
);
10461046
}
1047+
1048+
#[test]
1049+
fn basic_progress_bar_newline() {
1050+
let in_mem = InMemoryTerm::new(10, 80);
1051+
let pb = ProgressBar::with_draw_target(
1052+
Some(10),
1053+
ProgressDrawTarget::term_like(Box::new(in_mem.clone())),
1054+
);
1055+
1056+
assert_eq!(in_mem.contents(), String::new());
1057+
1058+
pb.println("\nhello");
1059+
pb.tick();
1060+
assert_eq!(
1061+
in_mem.contents(),
1062+
r#"
1063+
hello
1064+
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/10"#
1065+
);
1066+
1067+
pb.inc(1);
1068+
pb.println("");
1069+
assert_eq!(
1070+
in_mem.contents(),
1071+
r#"
1072+
hello
1073+
1074+
███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/10"#
1075+
);
1076+
1077+
pb.finish();
1078+
assert_eq!(
1079+
in_mem.contents(),
1080+
"
1081+
hello
1082+
1083+
██████████████████████████████████████████████████████████████████████████ 10/10"
1084+
);
1085+
}

0 commit comments

Comments
 (0)