Skip to content

Commit

Permalink
feat: allow to manually finish progressbar
Browse files Browse the repository at this point in the history
this is originally a workaround for the `deadlock` found in issue emersonford#10
  • Loading branch information
mokurin000 committed Dec 14, 2024
1 parent c70d4e8 commit 41e5c76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,17 @@ impl IndicatifSpanContext {
}
}

fn progress_bar_tick(&mut self) {
fn progress_bar_tick(&self) {
if let Some(ref pb) = self.progress_bar {
pb.tick()
}
}

fn progress_bar_finish_clear(&self) {
if let Some(ref pb) = self.progress_bar {
pb.finish_and_clear();
}
}
}

/// The layer that handles creating and managing indicatif progress bars for active spans. This
Expand Down
8 changes: 7 additions & 1 deletion src/pb_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl ProgressBarManager {
self.tick_settings = tick_settings;
}

fn decrement_pending_pb(&mut self) {
fn decrement_pending_pb(&self) {
let prev_val = self
.pending_progress_bars
.fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
Expand Down Expand Up @@ -266,6 +266,12 @@ impl ProgressBarManager {
return;
}

// PorgressBar is already finished
if pb.is_finished() {
self.decrement_pending_pb();
return;
}

// This span had an active/shown progress bar.
pb.finish_and_clear();
self.mp.remove(&pb);
Expand Down
9 changes: 9 additions & 0 deletions src/span_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub trait IndicatifSpanExt {
///
/// Has no effect if the progress bar for this span is not active.
fn pb_tick(&self);

/// Finish the progress bar
fn pb_finish_clear(&self);
}

impl IndicatifSpanExt for Span {
Expand Down Expand Up @@ -125,4 +128,10 @@ impl IndicatifSpanExt for Span {
indicatif_ctx.progress_bar_tick();
});
}

fn pb_finish_clear(&self) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.progress_bar_finish_clear();
});
}
}

0 comments on commit 41e5c76

Please sign in to comment.