ux: add spinner fallback when percent unknown

This commit is contained in:
2025-12-31 23:42:58 -05:00
parent cc853684c6
commit 152701c574

View File

@@ -680,6 +680,7 @@ fn watch_fix(paths: Vec<PathBuf>, config: &Config, ruleset: &RuleSet, dry_run: b
struct ProgressState {
last_emit: Instant,
last_percent: Option<u8>,
spinner_index: usize,
}
fn make_progress_callback(
@@ -694,6 +695,7 @@ fn make_progress_callback(
let state = Arc::new(Mutex::new(ProgressState {
last_emit: now.checked_sub(Duration::from_secs(2)).unwrap_or(now),
last_percent: None,
spinner_index: 0,
}));
Arc::new(move |progress: DecodeProgress| {
@@ -703,19 +705,27 @@ fn make_progress_callback(
.percent
.map(|value| value.max(0.0).min(100.0).floor() as u8);
let mut spinner_char = None;
if let Ok(mut state) = state.lock() {
if let Some(pct) = percent {
if state.last_percent.map_or(true, |last| pct >= last.saturating_add(1)) {
state.last_percent = Some(pct);
should_emit = true;
}
} else if now.duration_since(state.last_emit) >= Duration::from_secs(1) {
should_emit = true;
}
if !should_emit && now.duration_since(state.last_emit) < Duration::from_secs(1) {
if !should_emit {
return;
}
state.last_emit = now;
if percent.is_none() && !progress.done {
const SPINNER: [char; 4] = ['|', '/', '-', '\\'];
spinner_char = Some(SPINNER[state.spinner_index % SPINNER.len()]);
state.spinner_index = state.spinner_index.wrapping_add(1);
}
}
let mut line = String::new();
@@ -724,6 +734,11 @@ fn make_progress_callback(
if let Some(pct) = percent {
line.push_str(&format!("{:>3}% ", pct));
} else if progress.done {
line.push_str("done ");
} else if let Some(spinner) = spinner_char {
line.push(spinner);
line.push(' ');
}
match (progress.out_time, progress.duration) {