(window_size: usize, window_bytes: usize, enabled: bool)
| 338 | |
| 339 | impl Dashboard { |
| 340 | pub fn start(window_size: usize, window_bytes: usize, enabled: bool) -> Self { |
| 341 | let enabled = enabled && !crate::output::live::is_active(); |
| 342 | let state = Arc::new(Mutex::new(Progress { |
| 343 | phase: "preflight", |
| 344 | total: 0, |
| 345 | checked: 0, |
| 346 | loaded: 0, |
| 347 | published: 0, |
| 348 | queued: 0, |
| 349 | in_flight: 0, |
| 350 | window_size, |
| 351 | reserved_bytes: 0, |
| 352 | window_bytes, |
| 353 | })); |
| 354 | let interactive = std::io::stderr().is_terminal() |
| 355 | && std::env::var_os("TERM").is_some_and(|term| term != "dumb"); |
| 356 | let (stop, worker) = if enabled { |
| 357 | let (tx, rx) = mpsc::channel(); |
| 358 | let shared = Arc::clone(&state); |
| 359 | let handle = thread::spawn(move || { |
| 360 | let mut screen = interactive.then(Screen::enter).and_then(Result::ok); |
| 361 | let mut metrics = Metrics::default(); |
| 362 | let start = Instant::now(); |
| 363 | let mut previous: Option<(Instant, f64)> = None; |
| 364 | let mut stopping = false; |
| 365 | loop { |
| 366 | let now = Instant::now(); |
| 367 | let sample = resources(); |
| 368 | let cpu = |
| 369 | previous |
| 370 | .zip(sample.cpu_seconds) |
| 371 | .and_then(|((time, prior), current)| { |
| 372 | let delta = now.duration_since(time).as_secs_f64(); |
| 373 | (delta > 0.0) |
| 374 | .then_some(((current - prior).max(0.0) / delta) * 100.0) |
| 375 | }); |
| 376 | if let Some(current) = sample.cpu_seconds { |
| 377 | previous = Some((now, current)); |
| 378 | } |
| 379 | let progress = shared.lock().unwrap_or_else(|e| e.into_inner()).clone(); |
| 380 | metrics.observe(&sample, cpu); |
| 381 | if let Some(active) = &mut screen { |
| 382 | if active |
| 383 | .terminal |
| 384 | .draw(|frame| { |
| 385 | draw(frame, &progress, &sample, cpu, start.elapsed(), &metrics) |
| 386 | }) |
| 387 | .is_err() |
| 388 | { |
| 389 | // A broken terminal must not fail the import. |
| 390 | screen = None; |
| 391 | } |
| 392 | } else { |
| 393 | let text = plain(&progress, &sample, cpu, start.elapsed()); |
| 394 | let mut stderr = std::io::stderr().lock(); |
| 395 | let _ = stderr.write_all(text.as_bytes()); |
| 396 | let _ = stderr.flush(); |
| 397 | } |
nothing calls this directly
no test coverage detected