use crate::core::pane::Pane;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::collections::HashMap;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::sync::{Mutex, OnceLock};
#[cfg(any(target_os = "linux", target_os = "macos"))]
type Seen = HashMap<(u32, String), Option<Pane>>;
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn cache() -> &'static Mutex<Seen> {
static SEEN: OnceLock<Mutex<Seen>> = OnceLock::new();
SEEN.get_or_init(Default::default)
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn lookup_cached(key: &(u32, String)) -> Option<Option<Pane>> {
cache().lock().ok().and_then(|s| s.get(key).cloned())
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn store_cached(key: (u32, String), value: Option<Pane>) {
if let Ok(mut s) = cache().lock() {
if s.len() > 512 {
s.clear();
}
s.insert(key, value);
}
}
pub fn pane_of(pid: u32, proc_start: Option<&str>) -> Option<Pane> {
#[cfg(target_os = "linux")]
{
let start = match proc_start {
Some(s) if !s.is_empty() => s.to_string(),
Some(_) | None => crate::procs::proc_start_ticks(pid).unwrap_or_default(),
};
let key = (pid, start);
if let Some(hit) = lookup_cached(&key) {
return hit;
}
let found = std::fs::read(format!("/proc/{pid}/environ"))
.ok()
.and_then(|e| Pane::from_proc_environ(&e));
store_cached(key, found.clone());
found
}
#[cfg(target_os = "macos")]
{
use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};
if let Some(s) = proc_start.filter(|s| !s.is_empty()) {
let key = (pid, s.to_string());
if let Some(hit) = lookup_cached(&key) {
return hit;
}
let found = read_macos_env(pid);
store_cached(key, found.clone());
return found;
}
static SYS: OnceLock<Option<Mutex<System>>> = OnceLock::new();
let sys_lock = SYS
.get_or_init(|| System::new().ok().map(Mutex::new))
.as_ref()?;
let Ok(mut sys) = sys_lock.lock() else {
return None;
};
let spid = Pid::from_u32(pid);
sys.refresh_processes_specifics(
ProcessesToUpdate::Some(&[spid]),
true,
ProcessRefreshKind::nothing(),
);
let start = sys.process(spid)?.start_time().to_string();
let key = (pid, start);
if let Some(hit) = lookup_cached(&key) {
return hit;
}
sys.refresh_processes_specifics(
ProcessesToUpdate::Some(&[spid]),
true,
ProcessRefreshKind::nothing().with_environ(UpdateKind::OnlyIfNotSet),
);
let found = sys.process(spid).and_then(|p| {
let entries: Vec<String> = p
.environ()
.iter()
.map(|s| s.to_string_lossy().into_owned())
.collect();
Pane::from_environ_pairs(entries.iter().filter_map(|e| e.split_once('=')))
});
store_cached(key, found.clone());
found
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = (pid, proc_start);
None
}
}
#[cfg(target_os = "macos")]
fn read_macos_env(pid: u32) -> Option<Pane> {
use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind};
let mut sys = System::new().ok()?;
let spid = Pid::from_u32(pid);
sys.refresh_processes_specifics(
ProcessesToUpdate::Some(&[spid]),
true,
ProcessRefreshKind::nothing().with_environ(UpdateKind::OnlyIfNotSet),
);
let p = sys.process(spid)?;
let entries: Vec<String> = p
.environ()
.iter()
.map(|s| s.to_string_lossy().into_owned())
.collect();
Pane::from_environ_pairs(entries.iter().filter_map(|e| e.split_once('=')))
}