rd_dir/
lib.rs

1use colored::Colorize;
2
3pub struct ReadDirOptions {
4    // Show files beginning with a '.'
5    pub dotfiles: bool,
6
7    // Show '.' and '..' (overriden by `dotfiles`)
8    pub implied: bool,
9
10    // Should the files be sorted
11    pub sort: bool,
12
13    // Reverse the finalized vector
14    pub reverse: bool,
15}
16
17impl ReadDirOptions {
18    pub fn new() -> Self {
19        Self {
20            dotfiles: false,
21            implied: false,
22            sort: true,
23            reverse: false,
24        }
25    }
26}
27
28pub fn read_dir_to_string(options: ReadDirOptions) -> String {
29    let mut paths: Vec<String> = std::fs::read_dir("./")
30        .unwrap()
31        .map(|path| path.unwrap().file_name().into_string().unwrap())
32        .collect();
33    
34    if options.implied {
35        paths.push(String::from("."));
36        paths.push(String::from(".."));
37    }
38
39    if !options.dotfiles{
40        paths = paths
41            .into_iter()
42            .filter(|path| path.as_bytes()[0] != b'.')
43            .collect();
44    }
45
46    if options.sort {
47        paths.sort();
48    }
49
50    if options.reverse {
51        paths.reverse();
52    }
53
54    // Colorize directories
55    for path in &mut paths {
56        if std::path::Path::new(&path).is_dir() {
57             *path = path.bold().blue().to_string();
58        }     
59    }
60
61    paths.join(" ")
62}