Add support for tilde (~) expansion on windows

This commit is contained in:
Ofer Sadan 2024-01-02 13:36:00 +02:00 committed by Wei Zhang
parent 1e8120ff28
commit a1bd4d6347
3 changed files with 19 additions and 1 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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::*;