diff --git a/Cargo.toml b/Cargo.toml index 77c1498a22c9c16ee966bff95a9258b2eb8a9b89..2838578f2bca3977f36f88a200ac4187e7bc8869 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,6 @@ glob = "0.2" liner = "0.1" peg-syntax-ext = "0.4" permutate = "0.2" + +[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies] +users = "0.5.1" diff --git a/src/main.rs b/src/main.rs index d8435f7c1cfe52d438d17cad47b29dc9a8dd9f3d..0c0b990cb65e1beaf1f5cfce6a7485895a9630d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ extern crate glob; extern crate liner; +#[cfg(all(unix, not(target_os = "redox")))] +extern crate users as users_unix; + pub mod completer; pub mod pipe; pub mod directory_stack; diff --git a/src/variables.rs b/src/variables.rs index 6966674cf5bbcb2e18590ec24812040d778c01ab..1f2d4dbae298399b0186852525f114f2e4f6565a 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -147,15 +147,22 @@ impl Variables { neg = false; } - if let Ok(num) = tilde_num.parse::<usize>() { - let res = if neg { - dir_stack.dir_from_top(num) - } else { - dir_stack.dir_from_bottom(num) - }; - - if let Some(path) = res { - return Some(path.to_str().unwrap().to_string()); + match tilde_num.parse() { + Ok(num) => { + let res = if neg { + dir_stack.dir_from_top(num) + } else { + dir_stack.dir_from_bottom(num) + }; + + if let Some(path) = res { + return Some(path.to_str().unwrap().to_string()); + } + } + Err(_) => { + if let Some(home) = get_user_home(tilde_prefix) { + return Some(home + remainder); + } } } } @@ -180,6 +187,29 @@ impl Variables { } } +#[cfg(all(unix, not(target_os = "redox")))] +fn get_user_home(username: &str) -> Option<String> { + use users_unix::get_user_by_name; + use users_unix::os::unix::UserExt; + + match get_user_by_name(username) { + Some(user) => Some(user.home_dir().to_string_lossy().into_owned()), + None => None, + } +} + +#[cfg(target_os = "redox")] +fn get_user_home(_username: &str) -> Option<String> { + // TODO + None +} + +#[cfg(not(any(unix, target_os = "redox")))] +fn get_user_home(_username: &str) -> Option<String> { + // TODO + None +} + #[cfg(test)] mod tests { use super::*;