From 2f7d7d238ed25ae510b1fae33b5538ddeb99215d Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 2 May 2020 12:48:31 +0800 Subject: [PATCH] add resolve_file_path --- src/util_file.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/util_file.rs b/src/util_file.rs index 5fc8223..894e487 100644 --- a/src/util_file.rs +++ b/src/util_file.rs @@ -17,6 +17,17 @@ pub fn get_home_str() -> Option { iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None) } +pub fn resolve_file_path(path: &str) -> String { + let home_path = match get_home_str() { + Some(p) => p, None => return path.to_owned(), + }; + match path { + "~" => home_path, + p if p.starts_with("~/") => home_path + &path.chars().skip(1).collect::(), + p => p.to_owned(), + } +} + pub fn get_home_path() -> Option { Some(PathBuf::from(get_home_str()?)) }