aboutsummaryrefslogtreecommitdiffstats
path: root/src/run/var.rs
diff options
context:
space:
mode:
authorJonas Maier <>2026-05-23 22:45:12 +0200
committerJonas Maier <>2026-05-23 22:45:12 +0200
commita9ea5669915f4ac6b0d9e7d826d1ee341a2e3c80 (patch)
treee5756d045b25e45c46de1609e735074eb21e23e6 /src/run/var.rs
parent1ea4d307556ff522ab3a5bcca011966f3a0f440e (diff)
downloadpish-a9ea5669915f4ac6b0d9e7d826d1ee341a2e3c80.tar.gz
allow registration of variable watchers
Diffstat (limited to 'src/run/var.rs')
-rw-r--r--src/run/var.rs127
1 files changed, 0 insertions, 127 deletions
diff --git a/src/run/var.rs b/src/run/var.rs
deleted file mode 100644
index 8e22f5c..0000000
--- a/src/run/var.rs
+++ /dev/null
@@ -1,127 +0,0 @@
-use std::borrow::Cow;
-use std::collections::{HashMap, HashSet};
-use std::os::unix::ffi::OsStrExt;
-use std::sync::LazyLock;
-use std::time::Instant;
-
-use crate::BString;
-use crate::bstr;
-
-pub struct Vars {
- simple: HashMap<BString, BString>,
- magic: HashMap<BString, fn() -> BString>,
- all: HashSet<BString>,
- export: HashSet<BString>,
-}
-
-impl Vars {
- fn new(
- mut simple: HashMap<BString, BString>,
- magic: HashMap<BString, fn() -> BString>,
- ) -> Self {
- for (var, val) in std::env::vars_os() {
- // explicitly added variables get priority.
- if !simple.contains_key(var.as_bytes()) {
- simple.insert(var.into_encoded_bytes(), val.into_encoded_bytes());
- }
- }
- let all = simple
- .keys()
- .cloned()
- .chain(magic.keys().cloned())
- .collect();
- let export = std::env::vars_os()
- .map(|x| x.0.into_encoded_bytes())
- .collect();
- Self {
- simple,
- magic,
- all,
- export,
- }
- }
-
- pub fn set(&mut self, var: BString, val: BString) {
- self.simple.insert(var.clone(), val);
- self.all.insert(var);
- }
-
- pub fn lookup<'a>(&'a self, var: &bstr) -> Option<Cow<'a, bstr>> {
- if let Some(fun) = self.magic.get(var) {
- return Some(Cow::Owned(fun()));
- }
-
- if let Some(val) = self.simple.get(var) {
- return Some(Cow::Borrowed(val));
- }
-
- None
- }
-
- pub fn allow_export(&mut self, var: &bstr, allow: bool) {
- if allow {
- self.export.insert(var.to_vec());
- } else {
- self.export.remove(var);
- }
- }
-
- pub fn export<'a>(&'a self) -> HashMap<&'a bstr, Cow<'a, bstr>> {
- self.export
- .iter()
- .filter_map(|var| {
- let val = self.lookup(var)?;
- Some((var.as_ref(), val))
- })
- .collect()
- }
-
- pub fn vars(&self) -> &HashSet<BString> {
- &self.all
- }
-}
-
-macro_rules! map {
- ($($key:expr => $val:expr),* $(,)?) => {{
- let mut map = HashMap::<BString, _, _>::new();
- $(map.insert($key.into(), $val);)*
- map
- }};
-}
-
-impl Default for Vars {
- fn default() -> Self {
- use crate::basedir::xdg;
-
- let simple = map! {
- b"PISH_VERSION" => crate::consts::PISH_VERSION.as_bytes().to_vec(),
- b"PISH_COMMIT" => crate::consts::PISH_COMMIT.as_bytes().to_vec(),
- b"PISH_DIRTY" => vec![crate::consts::PISH_DIRTY as u8 + b'0'],
- xdg::DATA_HOME => xdg::data_home().as_os_str().as_bytes().to_vec(),
- xdg::STATE_HOME => xdg::state_home().as_os_str().as_bytes().to_vec(),
- xdg::CONFIG_HOME => xdg::config_home().as_os_str().as_bytes().to_vec(),
- };
-
- let magic = map! {
- b"CWD_PRETTY" => crate::pretty_cwd as _,
- b"SECONDS" => seconds_since_startup as _,
- };
-
- // call it once such that lazylock gets initialized
- seconds_since_startup();
-
- let mut this = Self::new(simple.clone(), magic);
- for var in simple.keys() {
- if var.starts_with(b"XDG_") {
- this.allow_export(var, true);
- }
- }
- this
- }
-}
-
-static START: LazyLock<Instant> = LazyLock::new(Instant::now);
-
-fn seconds_since_startup() -> BString {
- format!("{}", START.elapsed().as_secs_f64() as u64).into_bytes()
-}