use indicatif::ProgressStyle;
use tracing::Span;
use crate::IndicatifSpanContext;
use crate::WithContext;
fn apply_to_indicatif_span(span: &Span, f: impl FnMut(&mut IndicatifSpanContext)) {
span.with_subscriber(|(id, subscriber)| {
if let Some(get_context) = subscriber.downcast_ref::<WithContext>() {
get_context.with_context(subscriber, id, f);
}
});
}
pub trait IndicatifSpanExt {
fn pb_set_style(&self, style: &ProgressStyle);
fn pb_start(&self);
fn pb_set_length(&self, len: u64);
fn pb_set_position(&self, pos: u64);
fn pb_inc(&self, delta: u64);
fn pb_inc_length(&self, delta: u64);
fn pb_set_message(&self, msg: &str);
fn pb_tick(&self);
fn pb_reset(&self);
fn pb_reset_elapsed(&self);
fn pb_reset_eta(&self);
fn pb_set_finish_message(&self, msg: &str);
}
impl IndicatifSpanExt for Span {
fn pb_set_style(&self, style: &ProgressStyle) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.set_progress_bar_style(style.clone());
});
}
fn pb_start(&self) {
let _ = self.enter();
}
fn pb_set_length(&self, len: u64) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.set_progress_bar_length(len);
});
}
fn pb_set_position(&self, pos: u64) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.set_progress_bar_position(pos);
});
}
fn pb_inc(&self, pos: u64) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.inc_progress_bar_position(pos);
});
}
fn pb_inc_length(&self, delta: u64) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.inc_progress_bar_length(delta);
});
}
fn pb_set_message(&self, msg: &str) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.set_progress_bar_message(msg.to_string());
});
}
fn pb_tick(&self) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.progress_bar_tick();
});
}
fn pb_reset(&self) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.reset_progress_bar();
});
}
fn pb_reset_elapsed(&self) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.reset_progress_bar_elapsed();
});
}
fn pb_reset_eta(&self) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.reset_progress_bar_eta();
});
}
fn pb_set_finish_message(&self, msg: &str) {
apply_to_indicatif_span(self, |indicatif_ctx| {
indicatif_ctx.set_progress_bar_finish_message(msg.to_string());
});
}
}