use std::env; use std::fs; /// Refresh the shell's $PATH /// /// This function caches all valid paths within within the directories /// specified. /// /// # Returns /// * `bins: Vec` - A new cache of all valid file paths in $PATH /// /// # Examples /// ``` /// let mut bins = path::refresh(); /// ... /// // A situation occurs where the $PATH needs to be refreshed /// bins = path::refresh() /// ``` pub fn refresh() -> Vec { let mut bins = Vec::new(); let path = env::var("PATH").unwrap_or(String::new()); let mut path = path.split(':'); loop { let p = match path.next() { Some(p) => p, None => break, }; let files = match fs::read_dir(p) { Ok(files) => files, Err(_) => continue, }; for file in files { let f = match file { Ok(f) => f, Err(_) => continue, }; bins.push(f.path().display().to_string()); } } bins }