Allow ~ expansion

Allow the expansion of ~ to the user's home directory in a
cross-platform way since it is a popular convention in multiple shells.

Fixes: https://github.com/nushell/nushell/issues/357
Fixes: https://github.com/nushell/nushell/issues/340
This commit is contained in:
Ujjwal Sharma 2019-08-24 14:56:55 +05:30
parent cd1e16d574
commit 9fed5590da
No known key found for this signature in database
GPG Key ID: BB9110C042D7C804

View File

@ -338,6 +338,17 @@ async fn process_line(readline: Result<String, ReadlineError>, ctx: &mut Context
Ok(line) if line.trim() == "" => LineResult::Success(line.clone()),
Ok(line) => {
// Replace ~ with the address of the home directory.
let mut line_string = String::from(line);
if line.contains("~") {
match dirs::home_dir() {
// unwrap() is fine here since we know home is a valid unicode string.
Some(home) => line_string = line_string.replace("~", home.to_str().unwrap()),
None => panic!("Cannot find home directory"),
};
};
let line = &line_string;
let result = match crate::parser::parse(&line) {
Err(err) => {
return LineResult::Error(line.clone(), err);