Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on :change-current-directory to subdirectory of hidden directory #629

Closed
heliostatic opened this issue Aug 20, 2021 · 3 comments · Fixed by #692
Closed

Error on :change-current-directory to subdirectory of hidden directory #629

heliostatic opened this issue Aug 20, 2021 · 3 comments · Fixed by #692

Comments

@heliostatic
Copy link
Contributor

heliostatic commented Aug 20, 2021

Trying to change directory to ~/.config/helix when I found this error. Semi-related question: is it possible via config to automatically change the working directory to the directory opened with helix, instead of the current shell path when opening (e.g. hx ~/code/some-directory sets that to the cwd even when executed in `~/some/other/directory)?

image

CleanShot.2021-08-19.at.22.26.53.mp4
@heliostatic heliostatic changed the title Error on :change-working-directory to subdirectory of hidden directory Error on :change-current-directory to subdirectory of hidden directory Aug 20, 2021
@archseer
Copy link
Member

This looks like a missing tilde expansion, you likely need to provide the full directory path? ~ is a shell abstraction and the OS doesn't expand it for you.

@heliostatic
Copy link
Contributor Author

This looks like a missing tilde expansion, you likely need to provide the full directory path? ~ is a shell abstraction and the OS doesn't expand it for you.

I wondered if this was the case, but the subdirectory listing is working fine inside helix. You're right that providing the full path works, but now I'm curious why the behavior is different between the listing and changing.

@archseer
Copy link
Member

That's because we expand the tilde there, as well as hide it on document names:

/// Expands tilde `~` into users home directory if avilable, otherwise returns the path
/// unchanged. The tilde will only be expanded when present as the first component of the path
/// and only slash follows it.
pub fn expand_tilde(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
if let Some(Component::Normal(c)) = components.peek() {
if c == &"~" {
if let Ok(home) = helix_core::home_dir() {
// it's ok to unwrap, the path starts with `~`
return home.join(path.strip_prefix("~").unwrap());
}
}
}
path.to_path_buf()
}
/// Replaces users home directory from `path` with tilde `~` if the directory
/// is available, otherwise returns the path unchanged.
pub fn fold_home_dir(path: &Path) -> PathBuf {
if let Ok(home) = helix_core::home_dir() {
if path.starts_with(&home) {
// it's ok to unwrap, the path starts with home dir
return PathBuf::from("~").join(path.strip_prefix(&home).unwrap());
}
}
path.to_path_buf()
}

You should be able to call expand_tilde on the argument inside the command:

fn change_current_directory(
cx: &mut compositor::Context,
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let dir = args.first().context("target directory not provided")?;
if let Err(e) = std::env::set_current_dir(dir) {
bail!("Couldn't change the current working directory: {:?}", e);
}
let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
cx.editor.set_status(format!(
"Current working directory is now {}",
cwd.display()
));
Ok(())
}

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 a pull request may close this issue.

2 participants