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(|| {
let default = BINDINGS_DIRECTORY().join("tmux");
if default.join("powerline-base.conf").exists() {
return default;
}
if let Some(manifest) = option_env!("CARGO_MANIFEST_DIR") {
let manifest = PathBuf::from(manifest);
let mirror = manifest.join("src/ported/bindings/tmux");
if mirror.join("powerline-base.conf").exists() {
return mirror;
}
let vendor = manifest.join("vendor/powerline/powerline/bindings/tmux");
if vendor.join("powerline-base.conf").exists() {
return vendor;
}
}
default
})
}
#[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"));
let upstream = bindings.join("tmux");
let mirror =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/ported/bindings/tmux");
let vendor = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("vendor/powerline/powerline/bindings/tmux");
assert!(
tmux == &upstream || tmux == &mirror || tmux == &vendor,
"TMUX_CONFIG_DIRECTORY {} not in {{upstream, mirror, vendor}}",
tmux.display()
);
assert!(system.is_none());
}
}