use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::OnceLock;
pub struct Invocation {
pub args: Vec<OsString>,
pub relaunch: Vec<OsString>,
pub identity: Option<PathBuf>,
}
static HOSTED: OnceLock<Invocation> = OnceLock::new();
pub(crate) fn host(invocation: Invocation) {
HOSTED.set(invocation).ok();
}
pub(crate) fn args_os() -> Vec<OsString> {
match HOSTED.get() {
Some(h) => h.args.clone(),
None => std::env::args_os().collect(),
}
}
pub(crate) fn args() -> Vec<String> {
args_os()
.iter()
.map(|a| a.to_string_lossy().into_owned())
.collect()
}
#[cfg(unix)]
#[cfg(feature = "adapters")]
pub(crate) fn relaunch() -> std::io::Result<(OsString, Vec<OsString>)> {
match HOSTED.get().map(|h| h.relaunch.as_slice()) {
Some([program, before @ ..]) => Ok((program.clone(), before.to_vec())),
_ => Ok((std::env::current_exe()?.into_os_string(), Vec::new())),
}
}
#[cfg(unix)]
#[cfg(feature = "adapters")]
pub(crate) fn identity() -> Option<PathBuf> {
match HOSTED.get() {
Some(h) => h.identity.clone(),
None => std::env::current_exe().ok(),
}
}