summaryrefslogtreecommitdiffstats
path: root/src/path.rs
blob: f201f53ea44ca0dac74681e80586143d93004e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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<String>` - 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<String> {
    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
}