34 lines
1 KiB
Rust
34 lines
1 KiB
Rust
|
|
use std::time::Duration;
|
||
|
|
|
||
|
|
/// A terminal spinner for a research phase, shown only when logging is off —
|
||
|
|
/// with logging on, the trace output already tells the user something is
|
||
|
|
/// happening, and interleaving both would just be noisy. Clearing on drop
|
||
|
|
/// means call sites don't need an explicit "stop" at every early return.
|
||
|
|
pub(crate) struct Spinner(Option<indicatif::ProgressBar>);
|
||
|
|
|
||
|
|
impl Spinner {
|
||
|
|
pub(crate) fn start(enabled: bool, message: &'static str) -> Self {
|
||
|
|
if !enabled {
|
||
|
|
return Self(None);
|
||
|
|
}
|
||
|
|
|
||
|
|
let bar = indicatif::ProgressBar::new_spinner();
|
||
|
|
bar.enable_steady_tick(Duration::from_millis(100));
|
||
|
|
bar.set_style(
|
||
|
|
indicatif::ProgressStyle::with_template("{spinner:.cyan} {msg}")
|
||
|
|
.expect("static template is valid"),
|
||
|
|
);
|
||
|
|
bar.set_message(message);
|
||
|
|
|
||
|
|
Self(Some(bar))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Drop for Spinner {
|
||
|
|
fn drop(&mut self) {
|
||
|
|
if let Some(bar) = &self.0 {
|
||
|
|
bar.finish_and_clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|