From a1bd4d634722e223ad562323e4cea8e1f8f97963 Mon Sep 17 00:00:00 2001 From: Ofer Sadan Date: Tue, 2 Jan 2024 13:36:00 +0200 Subject: [PATCH] Add support for tilde (~) expansion on windows --- CHANGELOG.md | 1 + src/core.rs | 5 +++++ src/meta/windows_utils.rs | 14 +++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b60fd7..610fb6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 configuration fields) to truncate user and group names if they exceed a certain number of characters (disabled by default). - Add support for `--literal` from [PanGan21](https://github.com/PanGan21) +- Add support for tilde (`~`) expansion on Windows from [Ofer Sadan](https://github.com/ofersadan85) ## [v1.0.0] - 2023-08-25 diff --git a/src/core.rs b/src/core.rs index d806ada..7e90770 100644 --- a/src/core.rs +++ b/src/core.rs @@ -18,6 +18,8 @@ use std::os::unix::io::AsRawFd; use crate::flags::blocks::Block; use crate::git_theme::GitTheme; #[cfg(target_os = "windows")] +use crate::meta::windows_utils; +#[cfg(target_os = "windows")] use terminal_size::terminal_size; pub struct Core { @@ -104,6 +106,9 @@ impl Core { _ => 1, }; + #[cfg(target_os = "windows")] + let paths: Vec = paths.into_iter().map(windows_utils::expand_home).collect(); + for path in paths { let mut meta = match Meta::from_path(&path, self.flags.dereference.0, self.flags.permission) { diff --git a/src/meta/windows_utils.rs b/src/meta/windows_utils.rs index e647c26..a9db172 100644 --- a/src/meta/windows_utils.rs +++ b/src/meta/windows_utils.rs @@ -2,7 +2,7 @@ use std::ffi::{OsStr, OsString}; use std::io; use std::mem::MaybeUninit; use std::os::windows::ffi::{OsStrExt, OsStringExt}; -use std::path::Path; +use std::path::{Path, PathBuf}; use windows::Win32::Foundation::PSID; use windows::Win32::Security::{self, Authorization::TRUSTEE_W, ACL}; @@ -343,6 +343,18 @@ pub fn is_path_system(path: &Path) -> bool { ) } +/// Expands the `~` in a path to the current user's home directory +pub fn expand_home(path: PathBuf) -> PathBuf { + if path.starts_with("~") { + if let Some(home) = dirs::home_dir() { + let mut expanded = home.to_path_buf(); + expanded.push(path.strip_prefix("~").unwrap()); + return expanded; + } + } + path +} + #[cfg(test)] mod test { use super::*;