Repository navigation
Conversation
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: nushell#357 Fixes: nushell#340
db73c56 to
9fed559
Compare
|
P.S. I am still unsure that panicking is the right thing to do here, and I'm open to suggestions. |
|
Unless I'm missing something I don't think this approach correctly handles escaping because it's just doing a global replace for |
|
I'm not sure how useful this is but here's how the Ion shell handles this: |
|
@aidanharris while I understood the home directory semantics of the tilde symbol, I didn't quite read up on the others that are handled in the file you linked to. I could either:
|
| 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"), |
There was a problem hiding this comment.
Instead of searching and replacing across the whole string, we should separate how we handle tilde for the two types of commands:
- internal commands - these will say when they expect a path, and we can then replace the ~ in the path.
- external commands - we should process the arguments given to external like you're suggesting, by replacing the tilde
There was a problem hiding this comment.
If you want, you could do this in stages like you suggest.
| Ok(line) => { | ||
| // Replace ~ with the address of the home directory. | ||
| let mut line_string = String::from(line); | ||
| if line.contains("~") { |
There was a problem hiding this comment.
Do we need it in the whole string? Are there any scenarios where ~ is not the first char?
There was a problem hiding this comment.
I think line here is the entire line, e.g. cd ~/foo
There was a problem hiding this comment.
@superfunc exactly. It applies to all arguments, e.g. mv ~/foo ~/bar/
There was a problem hiding this comment.
What about ~a_user/, which should expand to the home dir of user a_user.
| 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()), |
There was a problem hiding this comment.
This is a little bit more complex than just .replace(). You don't want to expand ~~, only a single ~.
|
This ended up being a bit deeper of a change to switch strings to paths and then expand tilde. We just landed #479 as part of that work, so going ahead and closing this version. |
* Port over the clear command from nushell * cargo fmt
* Port over the clear command from nushell * cargo fmt
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: #357
Fixes: #340
/cc @RokuKent @twe4ked @jonathandturner @superfunc