use std::{env, path::PathBuf};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Program {
pub path: String,
pub exec: String,
pub flatpak: bool,
}
pub async fn fetch() -> Vec<Program> {
let mut programs: Vec<Program> = vec![];
match env::var("PATH") {
Ok(paths) => {
for path in paths.split(":") {
let mut path = PathBuf::from(path);
if path.exists() {
// resolve symlinks
if path.is_symlink() {
path = path.canonicalize().unwrap();
}
for binary in path.read_dir().unwrap() {
let binary = binary.unwrap();
programs.push(Program {
path: binary.path().into_os_string().into_string().unwrap(),
exec: binary.file_name().into_string().unwrap(),
flatpak: false,
});
}
}
}
}
Err(_) => {}
}
// sort alphabetically and deduplicate
// TODO: do this some way that doesn't require cloning for each one.
programs.sort_unstable_by_key(|p| p.exec.to_lowercase().clone());
programs.dedup();
programs
}
pub fn launch(path_to_binary: &str) -> std::io::Result<std::process::Child> {
std::process::Command::new(path_to_binary).spawn()
}