use std::path::PathBuf; use std::sync::OnceLock;
#[allow(non_upper_case_globals)]
static POWERLINE_ROOT_CELL: OnceLock<PathBuf> = OnceLock::new();
#[allow(non_upper_case_globals)]
static BINDINGS_DIRECTORY_CELL: OnceLock<PathBuf> = OnceLock::new();
#[allow(non_upper_case_globals)]
static TMUX_CONFIG_DIRECTORY_CELL: OnceLock<PathBuf> = OnceLock::new();
#[allow(non_upper_case_globals)]
static DEFAULT_SYSTEM_CONFIG_DIR_CELL: OnceLock<Option<PathBuf>> = OnceLock::new();
#[allow(non_snake_case)]
pub fn POWERLINE_ROOT() -> &'static PathBuf {
POWERLINE_ROOT_CELL.get_or_init(|| {
std::env::current_exe()
.ok()
.and_then(|p| p.parent().and_then(|p| p.parent()).map(PathBuf::from))
.unwrap_or_else(|| PathBuf::from("."))
})
}
#[allow(non_snake_case)]
pub fn BINDINGS_DIRECTORY() -> &'static PathBuf {
BINDINGS_DIRECTORY_CELL.get_or_init(|| {
POWERLINE_ROOT().join("powerline").join("bindings")
})
}
#[allow(non_snake_case)]
pub fn TMUX_CONFIG_DIRECTORY() -> &'static PathBuf {
TMUX_CONFIG_DIRECTORY_CELL.get_or_init(|| {
BINDINGS_DIRECTORY().join("tmux")
})
}
#[allow(non_snake_case)]
pub fn DEFAULT_SYSTEM_CONFIG_DIR() -> &'static Option<PathBuf> {
DEFAULT_SYSTEM_CONFIG_DIR_CELL.get_or_init(|| None) }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn config_constants_are_consistent() {
let root = POWERLINE_ROOT();
let bindings = BINDINGS_DIRECTORY();
let tmux = TMUX_CONFIG_DIRECTORY();
let system = DEFAULT_SYSTEM_CONFIG_DIR();
assert_eq!(bindings, &root.join("powerline").join("bindings"));
assert_eq!(tmux, &bindings.join("tmux"));
assert!(system.is_none());
}
}