diff --git a/crates/hyfetch/build.rs b/crates/hyfetch/build.rs index 749323db..987b4720 100644 --- a/crates/hyfetch/build.rs +++ b/crates/hyfetch/build.rs @@ -27,7 +27,7 @@ fn main() { println!("cargo:rerun-if-changed={}", neofetch_path.display()); - let out_dir = env::var("OUT_DIR").unwrap(); + let out_dir = env::var_os("OUT_DIR").unwrap(); let out_path = Path::new(&out_dir); export_distros(neofetch_path, out_path); diff --git a/crates/hyfetch/src/bin/hyfetch.rs b/crates/hyfetch/src/bin/hyfetch.rs index ceb38e2a..6c8ea672 100644 --- a/crates/hyfetch/src/bin/hyfetch.rs +++ b/crates/hyfetch/src/bin/hyfetch.rs @@ -7,6 +7,8 @@ use anyhow::{Context, Result}; use chrono::Datelike; use hyfetch::cli_options::options; use hyfetch::models::Config; +#[cfg(windows)] +use hyfetch::neofetch_util::ensure_git_bash; use hyfetch::neofetch_util::{self, get_distro_ascii}; use hyfetch::presets::AssignLightness; use hyfetch::utils::get_cache_path; @@ -22,12 +24,11 @@ fn main() -> Result<()> { debug!(?options, "CLI options"); - // TODO - // Use a custom distro let distro = options.distro.as_ref(); - // TODO + #[cfg(windows)] + ensure_git_bash().context("failed to find git bash")?; if options.test_print { println!( @@ -37,8 +38,6 @@ fn main() -> Result<()> { return Ok(()); } - // TODO - let config = if options.config { create_config(options.config_file).context("failed to create config")? } else if let Some(config) = diff --git a/crates/hyfetch/src/neofetch_util.rs b/crates/hyfetch/src/neofetch_util.rs index bec88a84..fd9eb965 100644 --- a/crates/hyfetch/src/neofetch_util.rs +++ b/crates/hyfetch/src/neofetch_util.rs @@ -217,7 +217,7 @@ impl ColorAlignment { /// Gets the absolute path of the neofetch command. pub fn get_command_path() -> Result { - if let Ok(workspace_dir) = env::var("CARGO_WORKSPACE_DIR") { + if let Some(workspace_dir) = env::var_os("CARGO_WORKSPACE_DIR") { let path = Path::new(&workspace_dir); if path.exists() { let path = path.join("neofetch"); @@ -236,7 +236,7 @@ pub fn get_command_path() -> Result { } } - let Ok(path_env) = env::var("PATH") else { + let Some(path_env) = env::var_os("PATH") else { return Err(anyhow!("`PATH` env var is not set or invalid")); }; @@ -251,6 +251,49 @@ pub fn get_command_path() -> Result { Err(anyhow!("neofetch command not found")) } +/// Ensures git bash installation for Windows. +/// +/// Returns the path to git bash. +#[cfg(windows)] +pub fn ensure_git_bash() -> Result { + let git_bash_path = { + // Bundled git bash + let current_exe_path = env::current_exe() + .and_then(|p| p.canonicalize()) + .context("failed to get path of current running executable")?; + let bash_path = current_exe_path.join("git/bin/bash.exe"); + if bash_path.is_file() { + Some(bash_path) + } else { + None + } + }; + let git_bash_path = git_bash_path.or_else(|| { + let program_files_path = env::var_os("ProgramFiles")?; + let bash_path = Path::new(&program_files_path).join("Git/bin/bash.exe"); + if bash_path.is_file() { + Some(bash_path) + } else { + None + } + }); + let git_bash_path = git_bash_path.or_else(|| { + let program_files_x86_path = env::var_os("ProgramFiles(x86)")?; + let bash_path = Path::new(&program_files_x86_path).join("Git/bin/bash.exe"); + if bash_path.is_file() { + Some(bash_path) + } else { + None + } + }); + + let Some(git_bash_path) = git_bash_path else { + return Err(anyhow!("failed to find git bash executable")); + }; + + Ok(git_bash_path) +} + /// Gets the distro ascii of the current distro. Or if distro is specified, get /// the specific distro's ascii art instead. #[tracing::instrument(level = "debug")] @@ -430,16 +473,24 @@ fn make_neofetch_command(args: &[S]) -> Result where S: AsRef, { + let neofetch_path = get_command_path().context("failed to get neofetch command path")?; + + debug!(?neofetch_path, "neofetch path"); + #[cfg(not(windows))] { let mut command = Command::new("bash"); - command.arg(get_command_path().context("failed to get neofetch command path")?); + command.arg(neofetch_path); command.args(args); Ok(command) } #[cfg(windows)] { - todo!() + let git_bash_path = ensure_git_bash().context("failed to get git bash path")?; + let mut command = Command::new(git_bash_path); + command.arg(neofetch_path); + command.args(args); + Ok(command) } }