Add live CLI progress for long-running jobs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::io::Read as _;
|
||||
use std::io::{IsTerminal as _, Read as _};
|
||||
use std::process::ExitCode;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
@@ -74,8 +75,33 @@ fn main() -> ExitCode {
|
||||
return ExitCode::from(1);
|
||||
}
|
||||
|
||||
match bds_cli::run(cli, context) {
|
||||
Ok(output) => {
|
||||
let progress = (!json).then(|| {
|
||||
Arc::new(Mutex::new(bds_cli::HumanProgress::new(
|
||||
std::io::stdout().is_terminal(),
|
||||
)))
|
||||
});
|
||||
let sink = progress.as_ref().map(|progress| {
|
||||
let progress = Arc::clone(progress);
|
||||
Arc::new(move |update: bds_cli::CliProgress| {
|
||||
let _ = progress
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
.write(&update, std::io::stdout().lock());
|
||||
}) as bds_cli::ProgressSink
|
||||
});
|
||||
let result = bds_cli::run_with_progress(cli, context, sink);
|
||||
if let Some(progress) = &progress {
|
||||
let _ = progress
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
.finish(std::io::stdout().lock());
|
||||
}
|
||||
|
||||
match result {
|
||||
Ok(mut output) => {
|
||||
if !json {
|
||||
output.progress.clear();
|
||||
}
|
||||
println!("{output}");
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user