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

feat: :cd - changes to the previous working directory #12194

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: remove prev implementation of prev_cwd
  • Loading branch information
nik-rev committed Dec 5, 2024
commit 5f92474b2007cd76360c4a9a412863c07e650c51
21 changes: 5 additions & 16 deletions helix-stdx/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ use std::{

use once_cell::sync::Lazy;

// We keep the CWD as a static so that we can access it in places where we don't have access to the Editor
static CWD: RwLock<Option<PathBuf>> = RwLock::new(None);
// previous working directory, stored to allow `:cd -`
static PREV_CWD: RwLock<Option<PathBuf>> = RwLock::new(None);

fn change_cwd(new_dir: PathBuf) {
let mut cwd = CWD.write().unwrap();
let mut prev_cwd = PREV_CWD.write().unwrap();
*prev_cwd = cwd.clone();
*cwd = Some(new_dir);
}

// Get the current working directory.
// This information is managed internally as the call to std::env::current_dir
Expand All @@ -39,24 +31,21 @@ pub fn current_working_dir() -> PathBuf {
cwd = pwd;
}
}
change_cwd(cwd.clone());
let mut dst = CWD.write().unwrap();
*dst = Some(cwd.clone());

cwd
}

pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
let path = crate::path::canonicalize(path);
std::env::set_current_dir(&path)?;
change_cwd(path);
let mut cwd = CWD.write().unwrap();
*cwd = Some(path);

Ok(())
}

pub fn previous_working_dir() -> Option<PathBuf> {
let prev_cwd = &*PREV_CWD.read().unwrap();
prev_cwd.clone()
}

pub fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}
Expand Down