Run neofetch using git bash on Windows

This commit is contained in:
Teoh Han Hui 2024-07-08 15:50:12 +08:00
parent 1239e86d1a
commit fa66bb486b
No known key found for this signature in database
GPG key ID: D43E2BABAF97DCAE
3 changed files with 60 additions and 10 deletions

View file

@ -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);

View file

@ -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) =

View file

@ -217,7 +217,7 @@ impl ColorAlignment {
/// Gets the absolute path of the neofetch command.
pub fn get_command_path() -> Result<PathBuf> {
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<PathBuf> {
}
}
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<PathBuf> {
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<PathBuf> {
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<S>(args: &[S]) -> Result<Command>
where
S: AsRef<OsStr>,
{
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)
}
}