Skip to content

Commit 3441728

Browse files
committed
Use the same reliable clearing strategy in quiet mode as in normal mode
1 parent 4282665 commit 3441728

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/status_emitter.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ use std::{
2121
num::NonZeroUsize,
2222
panic::{AssertUnwindSafe, RefUnwindSafe},
2323
path::{Path, PathBuf},
24-
sync::{atomic::AtomicBool, Arc},
24+
sync::{
25+
atomic::{AtomicUsize, Ordering},
26+
Arc,
27+
},
2528
thread::JoinHandle,
2629
time::Duration,
2730
};
@@ -222,23 +225,31 @@ impl Text {
222225
*done = true;
223226
let spinner = spinner.clone();
224227

228+
let print = new_msg.is_some();
229+
225230
if let Some(new_msg) = new_msg {
226231
spinner.finish_with_message(new_msg);
227-
let parent = children[""].0.clone();
228-
if !msg.is_empty() {
229-
parent.inc(1);
230-
}
231-
if children.values().all(|&(_, done)| done) {
232-
bars.remove(&parent);
232+
} else {
233+
spinner.finish();
234+
}
235+
let parent = children[""].0.clone();
236+
if !msg.is_empty() {
237+
parent.inc(1);
238+
}
239+
if children.values().all(|&(_, done)| done) {
240+
bars.remove(&parent);
241+
if print {
233242
bars.println(format!(
234243
"{} {}",
235244
parent.prefix(),
236245
parent.message()
237246
))
238247
.unwrap();
239-
for (msg, (child, _)) in children.iter() {
240-
if !msg.is_empty() {
241-
bars.remove(child);
248+
}
249+
for (msg, (child, _)) in children.iter() {
250+
if !msg.is_empty() {
251+
bars.remove(child);
252+
if print {
242253
bars.println(format!(
243254
" {} {}",
244255
child.prefix(),
@@ -248,8 +259,6 @@ impl Text {
248259
}
249260
}
250261
}
251-
} else {
252-
spinner.finish_and_clear();
253262
}
254263
}
255264

@@ -323,7 +332,8 @@ impl Text {
323332
}
324333
if let Some(progress) = progress {
325334
progress.tick();
326-
assert!(progress.is_finished());
335+
progress.finish();
336+
assert_eq!(Some(progress.position()), progress.length());
327337
}
328338
});
329339
Self {
@@ -369,14 +379,20 @@ struct TextTest {
369379
parent: String,
370380
path: PathBuf,
371381
revision: String,
372-
first: AtomicBool,
382+
/// Increased whenever a revision or sub-path is registered
383+
/// Decreased whenever `done` is called
384+
/// On increase from 0 to 1, adds 1 to the progress bar length
385+
/// On decrease from 1 to 0, removes 1 from progress bar length
386+
inc_counter: Arc<AtomicUsize>,
373387
style: RevisionStyle,
374388
}
375389

376390
impl TestStatus for TextTest {
377391
fn done(&self, result: &TestResult) {
378392
let new_leftover_msg = if self.text.is_quiet_output() {
379-
self.text.sender.send(Msg::Inc).unwrap();
393+
if self.inc_counter.fetch_sub(1, Ordering::Relaxed) == 1 {
394+
self.text.sender.send(Msg::Inc).unwrap();
395+
}
380396
None
381397
} else {
382398
let result = match result {
@@ -466,18 +482,16 @@ impl TestStatus for TextTest {
466482
}
467483

468484
fn for_revision(&self, revision: &str, style: RevisionStyle) -> Box<dyn TestStatus> {
469-
if !self.first.swap(false, std::sync::atomic::Ordering::Relaxed)
470-
&& self.text.is_quiet_output()
471-
{
472-
self.text.sender.send(Msg::IncLength).unwrap();
485+
if self.text.is_quiet_output() {
486+
self.inc_counter.fetch_add(1, Ordering::Relaxed);
473487
}
474488

475489
let text = Self {
476490
text: self.text.clone(),
477491
path: self.path.clone(),
478492
parent: self.parent.clone(),
479493
revision: revision.to_owned(),
480-
first: AtomicBool::new(false),
494+
inc_counter: self.inc_counter.clone(),
481495
style,
482496
};
483497
self.text
@@ -496,12 +510,16 @@ impl TestStatus for TextTest {
496510
}
497511

498512
fn for_path(&self, path: &Path) -> Box<dyn TestStatus> {
513+
if self.text.is_quiet_output() {
514+
self.inc_counter.fetch_add(1, Ordering::Relaxed);
515+
}
516+
499517
let text = Self {
500518
text: self.text.clone(),
501519
path: path.to_path_buf(),
502520
parent: self.parent.clone(),
503521
revision: String::new(),
504-
first: AtomicBool::new(false),
522+
inc_counter: self.inc_counter.clone(),
505523
style: RevisionStyle::Show,
506524
};
507525

@@ -530,7 +548,7 @@ impl StatusEmitter for Text {
530548
parent: display(&path),
531549
path,
532550
revision: String::new(),
533-
first: AtomicBool::new(true),
551+
inc_counter: Default::default(),
534552
style: RevisionStyle::Show,
535553
})
536554
}

0 commit comments

Comments
 (0)