file_picker/
lib.rs

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use dialoguer::theme::ColorfulTheme;
use std::fmt::Formatter;
use std::path::PathBuf;

static FILE: dialoguer::console::Emoji<'_, '_> = dialoguer::console::Emoji("📃", "📃");
static FOLDER: dialoguer::console::Emoji<'_, '_> = dialoguer::console::Emoji("📂", "📂");

struct Item(PathBuf);

impl Item {
    pub fn new(item: PathBuf) -> Self {
        Self(item)
    }
}

impl std::fmt::Display for Item {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let file_name = match self.0.file_name() {
            Some(file) => file.to_string_lossy().to_string(),
            None => self.0.display().to_string(),
        };
        let icon = match self.0.is_file() {
            true => FILE,
            false => FOLDER,
        };
        write!(f, "{} {}", icon, file_name)
    }
}

/// Prompts the user to pick a file interactively from the current directory.
///
/// # Arguments
///
/// * `label` - A string slice that holds the prompt label to display to the user.
///
/// # Returns
///
/// * `std::io::Result<PathBuf>` - Returns the `PathBuf` of the selected file.
///
/// # Examples
///
/// ```rust
/// use crate::file_picker::file_picker;
///
/// let file_path = file_picker("Select a file:").expect("Failed to pick a file");
/// println!("You selected the file: {:?}", file_path);
/// ```
///
pub fn file_picker(label: &str) -> std::io::Result<PathBuf> {
    let mut base_dir = std::env::current_dir()?;
    loop {
        let items = items(base_dir.clone());
        if let Ok(item) = dialoguer::FuzzySelect::with_theme(&ColorfulTheme::default())
            .items(&items)
            .default(0)
            .with_prompt(label)
            .highlight_matches(true)
            .interact()
        {
            match items.get(item) {
                None => {}
                Some(item) => {
                    if item.0.display().to_string() == *".." {
                        base_dir.pop();
                    } else if item.0.is_file() {
                        return Ok(item.0.to_path_buf());
                    } else {
                        base_dir = base_dir.join(&item.0);
                    }
                }
            }
        }
    }
}

fn items(wd: PathBuf) -> Vec<Item> {
    let mut items = vec![Item::new(PathBuf::from(".."))];
    let x = match std::fs::read_dir(&wd) {
        Ok(rd) => rd
            .filter_map(|x| x.ok())
            .map(|entry| Item::new(entry.path()))
            .collect::<Vec<_>>(),
        Err(_) => vec![],
    };

    items.extend(x);
    items
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let file = file_picker("Pick a file to assert is_file and exists").unwrap();
        assert!(file.is_file());
        assert!(file.exists());
    }
}