Fix handling of dot folders

This commit is contained in:
Mihai Galos 2022-03-02 22:19:30 +01:00
parent 654cbe9a7e
commit acf8adf2d8
2 changed files with 15 additions and 5 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "untildify" name = "untildify"
description = "Utility to replace ~ with user home directory." description = "Utility to replace ~ with user home directory."
version = "0.1.0" version = "0.1.1"
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"
readme = "README.md" readme = "README.md"

View File

@ -3,12 +3,12 @@ use std::env;
use std::path::PathBuf; use std::path::PathBuf;
/// Convert a tilde path to an absolute path: ~/Desktop → /Users/sathish/Desktop /// Convert a tilde path to an absolute path: ~/Desktop → /Users/sathish/Desktop
/// ///
/// Example /// Example
/// ///
/// ``` /// ```
/// use untildify::untildify; /// use untildify::untildify;
/// ///
/// fn main() { /// fn main() {
/// println!("Untildify : {}", untildify::untildify("~/Desktop")); // prints /Users/<user_name>/Desktop /// println!("Untildify : {}", untildify::untildify("~/Desktop")); // prints /Users/<user_name>/Desktop
/// println!("Untildify : {}", untildify("~/a/b/c/d/e")); // prints "/User/Untildify/a/b/c/d/e" /// println!("Untildify : {}", untildify("~/a/b/c/d/e")); // prints "/User/Untildify/a/b/c/d/e"
@ -23,7 +23,7 @@ pub fn untildify(input_path: &str) -> String {
return match get_host_dir() { return match get_host_dir() {
Some(path) => { Some(path) => {
let host_dir = path.to_str().unwrap(); let host_dir = path.to_str().unwrap();
let re = Regex::new(r"^~([/\w]+)").unwrap(); let re = Regex::new(r"^~([/\w.]+)").unwrap();
match re.captures(input_path) { match re.captures(input_path) {
Some(captures) => { Some(captures) => {
return format!("{}{}", host_dir, &captures[1]); return format!("{}{}", host_dir, &captures[1]);
@ -71,4 +71,14 @@ mod tests {
assert_eq!(untildify("/"), "/"); assert_eq!(untildify("/"), "/");
assert_eq!(untildify("~/Desktop/~/Code"), "/User/Untildify/Desktop/"); assert_eq!(untildify("~/Desktop/~/Code"), "/User/Untildify/Desktop/");
} }
#[test]
fn test_with_dot_folders() {
env::remove_var("HOME");
let home = Path::new("/User/Untildify");
env::set_var("HOME", home.as_os_str());
assert_eq!(untildify("~/.ssh/id_rsa"), "/User/Untildify/.ssh/id_rsa");
}
} }