Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-stdx/src/env.rs')
| -rw-r--r-- | helix-stdx/src/env.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/helix-stdx/src/env.rs b/helix-stdx/src/env.rs index b3f46c25..6f9a091e 100644 --- a/helix-stdx/src/env.rs +++ b/helix-stdx/src/env.rs @@ -1,3 +1,4 @@ +//! Functions for working with the host environment. use std::{ borrow::Cow, ffi::{OsStr, OsString}, @@ -10,9 +11,9 @@ 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); -// Get the current working directory. -// This information is managed internally as the call to std::env::current_dir -// might fail if the cwd has been deleted. +/// Get the current working directory. +/// This information is managed internally as the call to std::env::current_dir +/// might fail if the cwd has been deleted. pub fn current_working_dir() -> PathBuf { if let Some(path) = &*CWD.read().unwrap() { return path.clone(); @@ -37,6 +38,7 @@ pub fn current_working_dir() -> PathBuf { cwd } +/// Update the current working directory. pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<Option<PathBuf>> { let path = crate::path::canonicalize(path); std::env::set_current_dir(&path)?; @@ -45,14 +47,17 @@ pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<Option Ok(cwd.replace(path)) } +/// Checks if the given environment variable is set. pub fn env_var_is_set(env_var_name: &str) -> bool { std::env::var_os(env_var_name).is_some() } +/// Checks if a binary with the given name exists. pub fn binary_exists<T: AsRef<OsStr>>(binary_name: T) -> bool { which::which(binary_name).is_ok() } +/// Attempts to find a binary of the given name. See [which](https://linux.die.net/man/1/which). pub fn which<T: AsRef<OsStr>>( binary_name: T, ) -> Result<std::path::PathBuf, ExecutableNotFoundError> { |