Skip to content

Commit

Permalink
feat: silent logging
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelessiet committed Nov 23, 2024
1 parent b5c6d53 commit 8678f41
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 32 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ dialoguer = "0.11"
home = "0.5"
platform-dirs = "0.3"
chrono = "0.4"
indicatif = "0.15"
72 changes: 58 additions & 14 deletions src/homebrew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use crate::platform::Platform;
use anyhow::Result;
use colored::*;
use dialoguer::{theme::ColorfulTheme, Confirm};
use indicatif::{ProgressBar, ProgressStyle};
use serde::Deserialize;
use std::io::Write;
use std::process::Command;
use std::thread;
use std::time::Duration;

#[cfg(target_os = "windows")]
const HOMEBREW_INSTALL_URL: &str =
Expand Down Expand Up @@ -255,16 +258,37 @@ pub async fn install_formula_version(
if name.matches('/').count() == 2 {
println!("Installing {} via Homebrew 🐕", name.cyan());

let status = Command::new(if cfg!(windows) { "brew.exe" } else { "brew" })
let progress_bar = ProgressBar::new(100);
let mut child = Command::new(if cfg!(windows) { "brew.exe" } else { "brew" })
.args(["install", name])
.status()?;
.stdout(std::process::Stdio::piped())
.spawn()?;

if !status.success() {
anyhow::bail!("Failed to install {}", name);
// Create a simple spinner style
progress_bar.set_style(ProgressStyle::default_spinner().template("{spinner:.green} {msg}"));
progress_bar.set_message(&format!("Installing {}", name));

while child.try_wait()?.is_none() {
progress_bar.tick();
thread::sleep(Duration::from_millis(100));
}

println!("{} {} successfully", "Installed".green(), name);
return Ok(());
// Just wait for the process to complete
let status = child.wait()?;

if status.success() {
progress_bar.set_style(ProgressStyle::default_spinner().template("{msg}"));
progress_bar.finish_with_message(&format!(
"{} Successfully installed {}",
"✔".green(),
name
));
return Ok(());
} else {
progress_bar.set_style(ProgressStyle::default_spinner().template("{msg}"));
progress_bar.finish_with_message(&format!("{} Failed to install {}", "✘".red(), name));
anyhow::bail!("Failed to install {}", name);
}
}

// Regular formula installation
Expand Down Expand Up @@ -293,20 +317,40 @@ pub async fn install_formula_version(
};
args.push(&install_name);

let status = Command::new(if cfg!(windows) { "brew.exe" } else { "brew" })
.args(&args)
.status()?;
let progress_bar = ProgressBar::new(100);
let mut child = Command::new(if cfg!(windows) { "brew.exe" } else { "brew" })
.args(["install", name])
.stdout(std::process::Stdio::piped())
.spawn()?;

if !status.success() {
anyhow::bail!("Failed to install {}", install_name);
// Create a simple spinner style
progress_bar.set_style(ProgressStyle::default_spinner().template("{spinner:.green} {msg}"));
progress_bar.set_message(&format!("Installing {}", name));

while child.try_wait()?.is_none() {
progress_bar.tick();
thread::sleep(Duration::from_millis(100));
}

println!("{} {} successfully", "Installed".green(), install_name);
// Just wait for the process to complete
let status = child.wait()?;

if status.success() {
progress_bar.set_style(ProgressStyle::default_spinner().template("{msg}"));
progress_bar.finish_with_message(&format!(
"{} Successfully installed {}",
"✔".green(),
name
));
return Ok(());
} else {
progress_bar.set_style(ProgressStyle::default_spinner().template("{msg}"));
progress_bar.finish_with_message(&format!("{} Failed to install {}", "✘".red(), name));
anyhow::bail!("Failed to install {}", name);
}
} else {
anyhow::bail!("Package {} not found", name);
}

Ok(())
}

pub async fn search_formula(
Expand Down
40 changes: 29 additions & 11 deletions src/node/manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::types::*;
use anyhow::Result;
use colored::*;
use std::process::Command;
use indicatif::{ProgressBar, ProgressStyle};
use std::{process::Command, thread, time::Duration};

pub struct NodeManager {
package_manager: NodePackageManager,
Expand Down Expand Up @@ -32,20 +33,37 @@ impl NodeManager {
self.package_manager.command()
);

let status = Command::new(self.package_manager.command())
let progress_bar = ProgressBar::new(100);
let mut child = Command::new(self.package_manager.command())
.args(&args)
.status()?;
.stdout(std::process::Stdio::piped())
.spawn()?;

if !status.success() {
anyhow::bail!("Failed to install {}", package_with_version);
// Create a simple spinner style
progress_bar.set_style(ProgressStyle::default_spinner().template("{spinner:.green} {msg}"));
progress_bar.set_message(&format!("Installing {}", name));

while child.try_wait()?.is_none() {
progress_bar.tick();
thread::sleep(Duration::from_millis(100));
}

println!(
"{} {} successfully",
"Installed".green(),
package_with_version
);
Ok(())
// Just wait for the process to complete
let status = child.wait()?;

if status.success() {
progress_bar.set_style(ProgressStyle::default_spinner().template("{msg}"));
progress_bar.finish_with_message(&format!(
"{} Successfully installed {}",
"✔".green(),
name
));
return Ok(());
} else {
progress_bar.set_style(ProgressStyle::default_spinner().template("{msg}"));
progress_bar.finish_with_message(&format!("{} Failed to install {}", "✘".red(), name));
anyhow::bail!("Failed to install {}", name);
}
}

pub async fn uninstall_package(&self, name: &str) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/node/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub enum NodePackageManager {
Npm,
Yarn,
Pnpm,
Bun
Bun,
}

impl NodePackageManager {
Expand Down
9 changes: 3 additions & 6 deletions src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use anyhow::{Ok, Result};
use colored::*;
use std::process::Command;

use crate::{
homebrew,
node::NodeManager,
};
use crate::{homebrew, node::NodeManager};

pub async fn search_package(name: &str, is_cask: bool, is_node: bool) -> Result<()> {
if is_node {
Expand Down Expand Up @@ -68,7 +65,7 @@ pub async fn install_package(package: &str, is_cask: bool, is_node: bool) -> Res
println!("Version: {}", formula.versions.stable);

crate::homebrew::install_formula(&formula.full_name, is_cask).await?;
println!("Successfully installed {}", package.green());
// println!("Successfully installed {}", package.green());
} else {
println!("Package {} not found in Homebrew", package.red());
}
Expand Down Expand Up @@ -116,7 +113,7 @@ pub async fn install_package_version(
}

crate::homebrew::install_formula_version(name, version, is_cask).await?;
println!("Successfully installed {}", name.green());
// println!("Successfully installed {}", name.green());
} else {
println!("Package {} not found in Homebrew", name.red());
}
Expand Down

0 comments on commit 8678f41

Please sign in to comment.