Create libs for common code
Update libs
This commit is contained in:
14
command-with-spinner/Cargo.toml
Normal file
14
command-with-spinner/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "command-with-spinner"
|
||||
version = "0.1.0"
|
||||
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
license-file.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
colored = "3.0.0"
|
||||
indicatif = "0.17.11"
|
||||
thiserror = "2.0"
|
12
command-with-spinner/src/error.rs
Normal file
12
command-with-spinner/src/error.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
/// Git operations error type
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum CommandError {
|
||||
#[error("Command failed with exit code: {0}")]
|
||||
Failed(i32),
|
||||
|
||||
#[error("Command failed without exit code")]
|
||||
FailedNoCode,
|
||||
|
||||
#[error("Failed to execute command: {0}")]
|
||||
ExecutionError(#[from] std::io::Error),
|
||||
}
|
42
command-with-spinner/src/lib.rs
Normal file
42
command-with-spinner/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
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()),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user