Fix path normalization on Windows

This commit is contained in:
Teoh Han Hui 2024-07-09 01:35:31 +08:00
parent fa66bb486b
commit 290da7c363
No known key found for this signature in database
GPG key ID: D43E2BABAF97DCAE
5 changed files with 37 additions and 2 deletions

10
Cargo.lock generated
View file

@ -214,6 +214,7 @@ dependencies = [
"directories", "directories",
"enable-ansi-support", "enable-ansi-support",
"indexmap", "indexmap",
"normpath",
"palette", "palette",
"regex", "regex",
"serde", "serde",
@ -324,6 +325,15 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 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]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.46.0" 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 } directories = { version = "5.0.1", default-features = false }
enable-ansi-support = { version = "0.2.1", default-features = false } enable-ansi-support = { version = "0.2.1", default-features = false }
indexmap = { version = "2.2.6", 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 } palette = { version = "0.7.6", default-features = false }
regex = { version = "1.10.5", default-features = false } regex = { version = "1.10.5", default-features = false }
serde = { version = "1.0.203", 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] [target.'cfg(windows)'.dependencies]
enable-ansi-support = { workspace = true, features = [] } enable-ansi-support = { workspace = true, features = [] }
normpath = { workspace = true, features = [] }
[features] [features]
default = ["autocomplete", "color"] default = ["autocomplete", "color"]

View file

@ -16,7 +16,9 @@ use tracing::debug;
fn main() -> Result<()> { fn main() -> Result<()> {
#[cfg(windows)] #[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(); let options = options().run();

View file

@ -11,6 +11,8 @@ use std::{env, fmt};
use aho_corasick::AhoCorasick; use aho_corasick::AhoCorasick;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
#[cfg(windows)]
use normpath::PathExt as _;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tracing::debug; use tracing::debug;
@ -223,7 +225,13 @@ pub fn get_command_path() -> Result<PathBuf> {
let path = path.join("neofetch"); let path = path.join("neofetch");
match path.try_exists() { match path.try_exists() {
Ok(true) => { Ok(true) => {
#[cfg(not(windows))]
return path.canonicalize().context("failed to canonicalize path"); return path.canonicalize().context("failed to canonicalize path");
#[cfg(windows)]
return path
.normalize()
.map(|p| p.into())
.context("failed to normalize path");
}, },
Ok(false) => { Ok(false) => {
Err(anyhow!("{path:?} does not exist or is not readable"))?; Err(anyhow!("{path:?} does not exist or is not readable"))?;
@ -245,7 +253,13 @@ pub fn get_command_path() -> Result<PathBuf> {
if !path.is_file() { if !path.is_file() {
continue; continue;
} }
#[cfg(not(windows))]
return path.canonicalize().context("failed to canonicalize path"); 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")) Err(anyhow!("neofetch command not found"))
@ -259,7 +273,14 @@ pub fn ensure_git_bash() -> Result<PathBuf> {
let git_bash_path = { let git_bash_path = {
// Bundled git bash // Bundled git bash
let current_exe_path = env::current_exe() 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")?; .context("failed to get path of current running executable")?;
let bash_path = current_exe_path.join("git/bin/bash.exe"); let bash_path = current_exe_path.join("git/bin/bash.exe");
if bash_path.is_file() { if bash_path.is_file() {