Files
repo-utils/command-with-spinner/src/lib.rs
Noah Knegt 69f94c64b6 Create libs for common code
Update libs
2025-05-08 16:50:03 +02:00

43 lines
1.3 KiB
Rust

use std::process::{Command, Stdio};
use anyhow::{Context, Result};
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
mod error;
use error::CommandError;
/// Runs a git command with a progress spinner
pub fn run_command(command: &mut Command, message: &str) -> Result<()> {
let spinner = ProgressBar::new_spinner();
spinner.set_style(
ProgressStyle::default_spinner()
.tick_chars("⣾⣽⣻⢿⡿⣟⣯⣷")
.template("{spinner:.green} {msg}")
.expect("Invalid template format"),
);
spinner.set_message(message.to_string());
// Configure the command to not show output
command.stdout(Stdio::null()).stderr(Stdio::null());
// Execute the command and wait for it to complete
spinner.enable_steady_tick(std::time::Duration::from_millis(100));
let status = command.status().context("Failed to execute command")?;
spinner.finish_and_clear();
if status.success() {
println!("{message} {}", "Done.".green());
Ok(())
} else {
println!("{message} {}", "FAILED.".red());
let code = status.code();
match code {
Some(code) => Err(CommandError::Failed(code).into()),
None => Err(CommandError::FailedNoCode.into()),
}
}
}