hex/src/control/cli/progress.rs
Kit Langton fb6c61b0d9
feat(cli): hex transcribe reads any common media file, with or without the app (#1799)
* Make hex transcribe work with any common media file, with or without the app (t1555)

* Regenerate the Labs goldens without Transcribe files (t1555)
2026-11-13 13:14:15 -08:00

32 lines
951 B
Rust

//! One line of progress on stderr, only when stderr is a terminal, cleared
//! before the result prints so a terminal ends with the result alone.
use std::io::{IsTerminal as _, Write as _};
use std::sync::atomic::{AtomicBool, Ordering};
static SHOWN: AtomicBool = AtomicBool::new(false);
/// Replaces the progress line with `text`.
pub(super) fn show(text: &str) {
let mut stderr = std::io::stderr();
if !stderr.is_terminal() {
return;
}
let _ = write!(stderr, "\r\x1b[2K{text}");
let _ = stderr.flush();
SHOWN.store(true, Ordering::Release);
}
/// Removes the progress line, if one is showing.
pub(super) fn clear() {
if SHOWN.swap(false, Ordering::AcqRel) {
let mut stderr = std::io::stderr();
let _ = write!(stderr, "\r\x1b[2K");
let _ = stderr.flush();
}
}
/// Bytes as whole megabytes.
pub(super) fn megabytes(bytes: u64) -> String {
format!("{} MB", bytes / 1_000_000)
}