From 290da7c363476d6ad953ea7719bbc15e32a4cd5c Mon Sep 17 00:00:00 2001 From: Teoh Han Hui Date: Tue, 9 Jul 2024 01:35:31 +0800 Subject: [PATCH] Fix path normalization on Windows --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + crates/hyfetch/Cargo.toml | 1 + crates/hyfetch/src/bin/hyfetch.rs | 4 +++- crates/hyfetch/src/neofetch_util.rs | 23 ++++++++++++++++++++++- 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4506a714..240dc241 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 58b31d6a..7c0e589d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/crates/hyfetch/Cargo.toml b/crates/hyfetch/Cargo.toml index 4913b22d..2ec896a0 100644 --- a/crates/hyfetch/Cargo.toml +++ b/crates/hyfetch/Cargo.toml @@ -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"] diff --git a/crates/hyfetch/src/bin/hyfetch.rs b/crates/hyfetch/src/bin/hyfetch.rs index 6c8ea672..d2d0655f 100644 --- a/crates/hyfetch/src/bin/hyfetch.rs +++ b/crates/hyfetch/src/bin/hyfetch.rs @@ -16,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(); diff --git a/crates/hyfetch/src/neofetch_util.rs b/crates/hyfetch/src/neofetch_util.rs index fd9eb965..08eee897 100644 --- a/crates/hyfetch/src/neofetch_util.rs +++ b/crates/hyfetch/src/neofetch_util.rs @@ -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; @@ -223,7 +225,13 @@ pub fn get_command_path() -> Result { 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"))?; @@ -245,7 +253,13 @@ pub fn get_command_path() -> Result { 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")) @@ -259,7 +273,14 @@ 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()) + .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() {