Skip to content

Allow ~ expansion - #373

Closed
ryzokuken wants to merge 1 commit into
nushell:masterfrom
ryzokuken:feature/home-dir-expansion
Closed

ryzokuken wants to merge 1 commit into
nushell:masterfrom
ryzokuken:feature/home-dir-expansion

Conversation

@ryzokuken

Copy link
Copy Markdown

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

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
@ryzokuken
ryzokuken force-pushed the feature/home-dir-expansion branch from db73c56 to 9fed559 Compare August 24, 2019 09:35
@ryzokuken

Copy link
Copy Markdown
Author

P.S. I am still unsure that panicking is the right thing to do here, and I'm open to suggestions.

@aidanharris

aidanharris commented Aug 24, 2019 •

Copy link
Copy Markdown
Contributor

Unless I'm missing something I don't think this approach correctly handles escaping because it's just doing a global replace for ~? What if you want a literal ~ instead of it expanding? It should ignore \~ (compare bash -c 'echo ~' with bash -c 'echo \~').

@aidanharris

Copy link
Copy Markdown
Contributor

@ryzokuken

Copy link
Copy Markdown
Author

@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:

  1. Handle escaping and leave the rest of the behavior unhandled for a follow-up PR (merging this sooner). I believe it's a net gain still.
  2. Wait on this until I read up on all the semantics of the tilde symbol and write code that handles each of them, not unlike the code you linked to, I'd imagine.

Comment thread src/cli.rs
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"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want, you could do this in stages like you suggest.

Comment thread src/cli.rs
Ok(line) => {
// Replace ~ with the address of the home directory.
let mut line_string = String::from(line);
if line.contains("~") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need it in the whole string? Are there any scenarios where ~ is not the first char?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think line here is the entire line, e.g. cd ~/foo

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@superfunc exactly. It applies to all arguments, e.g. mv ~/foo ~/bar/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ~a_user/, which should expand to the home dir of user a_user.

Comment thread src/cli.rs
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()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit more complex than just .replace(). You don't want to expand ~~, only a single ~.

@sophiajt

Copy link
Copy Markdown
Contributor

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.

@sophiajt sophiajt closed this Aug 27, 2019
@ryzokuken
ryzokuken deleted the feature/home-dir-expansion branch August 27, 2019 07:22
elferherrera pushed a commit to elferherrera/nushell that referenced this pull request Feb 7, 2022
* Port over the clear command from nushell

* cargo fmt
kubouch pushed a commit that referenced this pull request Feb 7, 2022
* Port over the clear command from nushell

* cargo fmt
Hofer-Julian pushed a commit to Hofer-Julian/nushell that referenced this pull request Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

~ expansion nu shell should understand tilde ~ home directory

7 participants