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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::path::Path;

#[derive(Clone, Debug)]
pub struct ListOption {
    // if true, list directories
    dir: bool,
    // if true, list files
    file: bool,
    // if true, show hidden files
    hidden: bool,
    // if true,show unhidden files
    unhidden: bool,
    // if true, list recursively
    recursive: bool,
    // default 1, list only current directory
    level: usize,
    // if not empty, list only files with these extensions
    sufs: Vec<String>,
}
impl Default for ListOption {
    fn default() -> Self {
        Self {
            dir: true,
            file: true,
            hidden: false,
            unhidden: true,
            recursive: false,
            level: 1,
            sufs: Vec::new(),
        }
    }
}

/// specify the list option
impl ListOption {
    pub fn dir(&mut self, if_show: bool) -> Self {
        self.dir = if_show;
        self.clone()
    }
    pub fn file(&mut self, if_show: bool) -> Self {
        self.file = if_show;
        self.clone()
    }
    pub fn hidden(&mut self, if_show: bool) -> Self {
        self.hidden = if_show;
        self.clone()
    }
    pub fn unhidden(&mut self, if_show: bool) -> Self {
        self.unhidden = if_show;
        self.clone()
    }
    pub fn level(&mut self, level: usize) -> Self {
        self.level = level;
        self.clone()
    }
    pub fn recursive(&mut self, if_choose: bool) -> Self {
        self.recursive = if_choose;
        self.clone()
    }
    pub fn ext(&mut self, ext: &str) -> Self {
        self.sufs.push(format!(".{}", ext));
        self.clone()
    }
    pub fn exts(&mut self, exts: Vec<&str>) -> Self {
        self.sufs = exts.iter().map(|s| format!(".{}", s)).collect();
        self.clone()
    }
    pub fn suf(&mut self, suf: &str) -> Self {
        self.sufs.push(suf.to_string());
        self.clone()
    }
    pub fn sufs(&mut self, sufs: Vec<&str>) -> Self {
        self.sufs = sufs.iter().map(|s| s.to_string()).collect();
        self.clone()
    }
}

/// impl list functionality
impl ListOption {
    fn check_if_show_file(&self, file_path: &str) -> bool {
        let base_name = Path::new(file_path).file_name().unwrap().to_str().unwrap();
        // dbg!(file_path);
        // dbg!(self.sufs.is_empty() || self.sufs.iter().any(|suf| base_name.ends_with(suf)));
        // dbg!(&self.sufs);
        self.file
            && (self.sufs.is_empty() || self.sufs.iter().any(|suf| base_name.ends_with(suf)))
            && (self.hidden && base_name.starts_with('.')
                || self.unhidden && !base_name.starts_with('.'))
    }
    fn check_if_list_dir(&self, dir_path: &str) -> bool {
        let dir_path = Path::new(dir_path).canonicalize().unwrap();
        let base_name = dir_path
            .file_name()
            .unwrap_or_else(|| panic!("{:?}", dir_path))
            .to_str()
            .unwrap();
        (self.level > 0 || self.recursive)
            && (self.hidden && base_name.starts_with('.')
                || self.unhidden && !base_name.starts_with('.'))
    }
    pub fn list(&self, path: &str) -> Vec<String> {
        // check if is a file
        let path = Path::new(path);
        // check if exists
        if !path.exists() {
            return Vec::new();
        }
        let mut ret: Vec<String> = Vec::new();
        if path.is_file() {
            if self.file && self.check_if_show_file(path.to_str().unwrap()) {
                ret.push(path.to_str().unwrap().to_string());
            }
        } else if path.is_dir() {
            // list all files
            let files = path.read_dir().unwrap();
            for file in files {
                let file = file.unwrap();
                let file_path = file.path();
                if file_path.is_dir() {
                    let next_level = if self.recursive {
                        self.level
                    } else {
                        self.level - 1
                    };
                    let sub_opt = ListOption {
                        level: next_level,
                        ..self.clone()
                    };
                    let sub_dir = file_path.to_str().unwrap();
                    if self.dir {
                        ret.push(sub_dir.to_string());
                    }
                    if sub_opt.check_if_list_dir(sub_dir) {
                        let sub_list = sub_opt.inner_list(file_path.to_str().unwrap());
                        ret.extend(sub_list);
                    }
                } else if file_path.is_file() {
                    let file_name = file_path.to_str().unwrap();
                    if self.check_if_show_file(file_name) {
                        ret.push(file_name.to_string());
                    }
                }
            }
        }
        ret
    }
    fn inner_list(&self, path: &str) -> Vec<String> {
        // check if is a file
        let path = Path::new(path);
        // check if exists
        if !path.exists() {
            return Vec::new();
        }
        let mut ret: Vec<String> = Vec::new();
        if path.is_file() {
            if self.file && self.check_if_show_file(path.to_str().unwrap()) {
                ret.push(path.to_str().unwrap().to_string());
            }
        } else if path.is_dir() && self.check_if_list_dir(path.to_str().unwrap()) {
            // list all files
            let files = path.read_dir().unwrap();
            for file in files {
                let file = file.unwrap();
                let file_path = file.path();
                if file_path.is_dir() {
                    let next_level = if self.recursive {
                        self.level
                    } else {
                        self.level - 1
                    };
                    let sub_opt = ListOption {
                        level: next_level,
                        ..self.clone()
                    };
                    let sub_dir = file_path.to_str().unwrap();
                    if self.dir {
                        ret.push(sub_dir.to_string());
                    }
                    if sub_opt.check_if_list_dir(sub_dir) {
                        let sub_list = sub_opt.inner_list(file_path.to_str().unwrap());
                        ret.extend(sub_list);
                    }
                } else if file_path.is_file() {
                    let file_name = file_path.to_str().unwrap();
                    if self.check_if_show_file(file_name) {
                        ret.push(file_name.to_string());
                    }
                }
            }
        }
        ret
    }
}