Merge pull request #17 from teohhanhui/riir

Run neofetch using git bash on Windows
This commit is contained in:
Teoh Han Hui 2024-07-09 01:36:51 +08:00 committed by GitHub
commit d773c69918
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 96 additions and 11 deletions

10
Cargo.lock generated
View file

@ -214,6 +214,7 @@ dependencies = [
"directories",
"enable-ansi-support",
"indexmap",
"normpath",
"palette",
"regex",
"serde",
@ -324,6 +325,15 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "normpath"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5831952a9476f2fed74b77d74182fa5ddc4d21c72ec45a333b250e3ed0272804"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"

View file

@ -22,6 +22,7 @@ deranged = { version = "0.3.11", default-features = false }
directories = { version = "5.0.1", default-features = false }
enable-ansi-support = { version = "0.2.1", default-features = false }
indexmap = { version = "2.2.6", default-features = false }
normpath = { version = "1.2.0", default-features = false }
palette = { version = "0.7.6", default-features = false }
regex = { version = "1.10.5", default-features = false }
serde = { version = "1.0.203", default-features = false }

View file

@ -38,6 +38,7 @@ unicode-normalization = { workspace = true, features = ["std"] }
[target.'cfg(windows)'.dependencies]
enable-ansi-support = { workspace = true, features = [] }
normpath = { workspace = true, features = [] }
[features]
default = ["autocomplete", "color"]

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;
@ -14,7 +16,9 @@ use tracing::debug;
fn main() -> Result<()> {
#[cfg(windows)]
enable_ansi_support::enable_ansi_support();
if let Err(err) = enable_ansi_support::enable_ansi_support() {
debug!(err, "could not enable ANSI escape code support");
}
let options = options().run();
@ -22,12 +26,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 +40,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

@ -11,6 +11,8 @@ use std::{env, fmt};
use aho_corasick::AhoCorasick;
use anyhow::{anyhow, Context, Result};
use indexmap::IndexMap;
#[cfg(windows)]
use normpath::PathExt as _;
use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;
use tracing::debug;
@ -217,13 +219,19 @@ 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");
match path.try_exists() {
Ok(true) => {
#[cfg(not(windows))]
return path.canonicalize().context("failed to canonicalize path");
#[cfg(windows)]
return path
.normalize()
.map(|p| p.into())
.context("failed to normalize path");
},
Ok(false) => {
Err(anyhow!("{path:?} does not exist or is not readable"))?;
@ -236,7 +244,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"));
};
@ -245,12 +253,68 @@ pub fn get_command_path() -> Result<PathBuf> {
if !path.is_file() {
continue;
}
#[cfg(not(windows))]
return path.canonicalize().context("failed to canonicalize path");
#[cfg(windows)]
return path
.normalize()
.map(|p| p.into())
.context("failed to normalize path");
}
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| {
#[cfg(not(windows))]
{
p.canonicalize()
}
#[cfg(windows)]
p.normalize().map(|p| p.into())
})
.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 +494,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)
}
}