summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOfer Sadan <ofersadan85@gmail.com>2024-01-02 13:36:00 +0200
committerWei Zhang <kweizh@gmail.com>2024-03-22 23:59:12 +0800
commita1bd4d634722e223ad562323e4cea8e1f8f97963 (patch)
treefe486d07a5b3a0d9031466cc1148751e3720af47
parent1e8120ff28736041d834b139c3503cc8b841f952 (diff)
Add support for tilde (~) expansion on windows
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/core.rs5
-rw-r--r--src/meta/windows_utils.rs14
3 files changed, 19 insertions, 1 deletions
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<PathBuf> = 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::*;