command_fs/
file_query.rs

1use super::file_handel::CommandFS;
2
3impl<'a> CommandFS<'a> {
4    pub fn whereami(&mut self) -> &str {
5        match self.dir.to_str() {
6            Some(path) => path,
7            None => {
8                self.err_msg = "Unable to know Path".to_string();
9                ""
10            }
11        }
12    }
13    pub fn know_home_dir(&mut self) -> String {
14        match homedir::get_my_home() {
15            Ok(home_option) => match home_option {
16                Some(home) => {
17                    let home_bind = home.clone();
18                    match home_bind.to_str() {
19                        Some(x) => x.to_string(),
20                        None => "".to_string(),
21                    }
22                }
23                None => {
24                    self.err_msg = "Somethings Wrong".to_string();
25                    "".to_string()
26                }
27            },
28            Err(error) => {
29                self.err_msg = error.to_string();
30                "".to_string()
31            }
32        }
33    }
34    pub fn dir_list(&mut self) -> Vec<String> {
35        match self.dir.read_dir() {
36            Ok(read_dir) => {
37                let mut output = vec![];
38                for dir in read_dir {
39                    match dir {
40                        Ok(dir_entry) => {
41                            if dir_entry.path().is_dir() {
42                                let len_path = match self.dir.to_str() {
43                                    Some(str) => str.len(),
44                                    None => 0,
45                                };
46                                output.push(
47                                    dir_entry.path().display().to_string().as_str()[len_path..]
48                                        .to_string(),
49                                )
50                            }
51                        }
52                        Err(error) => self.err_msg = error.to_string(),
53                    }
54                }
55                return output;
56            }
57            Err(error) => {
58                self.err_msg = error.to_string();
59                vec![]
60            }
61        }
62    }
63
64    pub fn file_list(&mut self) -> Vec<String> {
65        if !self.dir.is_dir() {
66            self.err_msg = String::from(
67                "!WARNING! Seems like Given path is file please set path to Directory on given",
68            );
69            vec![]
70        } else {
71            match self.dir.read_dir() {
72                Ok(read_dir) => {
73                    let mut output = vec![];
74                    for dir in read_dir {
75                        match dir {
76                            Ok(dir_entry) => {
77                                if !dir_entry.path().is_dir() {
78                                    let len_path = match self.dir.to_str() {
79                                        Some(str) => str.len(),
80                                        None => 0,
81                                    };
82                                    output.push(
83                                        dir_entry.path().display().to_string().as_str()[len_path..]
84                                            .to_string(),
85                                    )
86                                }
87                            }
88                            Err(error) => self.err_msg = error.to_string(),
89                        }
90                    }
91                    return output;
92                }
93                Err(error) => {
94                    self.err_msg = error.to_string();
95                    vec![]
96                }
97            }
98        }
99    }
100    pub fn file_dir_list(&mut self) -> Vec<String> {
101        if !self.dir.is_dir() {
102            self.err_msg = String::from(
103                "!WARNING! Seems like Given path is file please set path to Directory on given",
104            );
105            vec![]
106        } else {
107            match self.dir.read_dir() {
108                Ok(read_dir) => {
109                    let mut output = vec![];
110                    for dir in read_dir {
111                        match dir {
112                            Ok(dir_entry) => {
113                                let len_path = match self.dir.to_str() {
114                                    Some(str) => str.len(),
115                                    None => 0,
116                                };
117                                output.push(
118                                    dir_entry.path().display().to_string().as_str()[len_path..]
119                                        .to_string(),
120                                )
121                            }
122                            Err(error) => self.err_msg = error.to_string(),
123                        }
124                    }
125                    return output;
126                }
127                Err(error) => {
128                    self.err_msg = error.to_string();
129                    vec![]
130                }
131            }
132        }
133    }
134    pub fn query_dir(&mut self, query: &'a str, accurate: bool) -> Vec<String> {
135        let mut output = vec![];
136        let mut listed_dir = self.dir_list();
137        while listed_dir.len() != 0 {
138            if accurate {
139                match listed_dir.last() {
140                    Some(x) => {
141                        if x.matches(query).collect::<Vec<&str>>().len() != 0 {
142                            output.push(x.to_string());
143                            listed_dir.pop()
144                        } else {
145                            listed_dir.pop()
146                        };
147                    }
148                    None => break,
149                }
150            } else {
151                let char_q: Vec<char> = query.chars().collect();
152                match listed_dir.last() {
153                    Some(x) => {
154                        if x.matches(query).collect::<Vec<&str>>().len() != 0 {
155                            output.push(x.to_string());
156                            listed_dir.pop()
157                        } else {
158                            let mut allow = false;
159                            for each_char in char_q {
160                                if x.matches(each_char).collect::<Vec<&str>>().len() != 0 {
161                                    allow = true;
162                                } else {
163                                    allow = false;
164                                    break;
165                                }
166                            }
167                            if allow {
168                                output.push(x.to_string());
169                            }
170                            listed_dir.pop()
171                        };
172                    }
173                    None => break,
174                }
175            }
176        }
177        output
178    }
179    pub fn query_file(&mut self, query: &'a str, accurate: bool) -> Vec<String> {
180        let mut output = vec![];
181        let mut listed_file = self.file_list();
182        while listed_file.len() != 0 {
183            if accurate {
184                match listed_file.last() {
185                    Some(x) => {
186                        if x.matches(query).collect::<Vec<&str>>().len() != 0 {
187                            output.push(x.to_string());
188                            listed_file.pop()
189                        } else {
190                            listed_file.pop()
191                        };
192                    }
193                    None => break,
194                }
195            } else {
196                let char_q: Vec<char> = query.chars().collect();
197                match listed_file.last() {
198                    Some(x) => {
199                        if x.matches(query).collect::<Vec<&str>>().len() != 0 {
200                            output.push(x.to_string());
201                            listed_file.pop()
202                        } else {
203                            let mut allow = false;
204                            for each_char in char_q {
205                                if x.matches(each_char).collect::<Vec<&str>>().len() != 0 {
206                                    allow = true;
207                                } else {
208                                    allow = false;
209                                    break;
210                                }
211                            }
212                            if allow {
213                                output.push(x.to_string());
214                            }
215                            listed_file.pop()
216                        };
217                    }
218                    None => break,
219                }
220            }
221        }
222        output
223    }
224    pub fn query_file_dir(&mut self, query: &'a str, accurate: bool) -> Vec<String> {
225        let mut output = vec![];
226        let mut listed_file_dir = self.file_dir_list();
227        while listed_file_dir.len() != 0 {
228            if accurate {
229                match listed_file_dir.last() {
230                    Some(x) => {
231                        if x.matches(query).collect::<Vec<&str>>().len() != 0 {
232                            output.push(x.to_string());
233                            listed_file_dir.pop()
234                        } else {
235                            listed_file_dir.pop()
236                        };
237                    }
238                    None => break,
239                }
240            } else {
241                let char_q: Vec<char> = query.chars().collect();
242                match listed_file_dir.last() {
243                    Some(x) => {
244                        if x.matches(query).collect::<Vec<&str>>().len() != 0 {
245                            output.push(x.to_string());
246                            listed_file_dir.pop()
247                        } else {
248                            let mut allow = false;
249                            for each_char in char_q {
250                                if x.matches(each_char).collect::<Vec<&str>>().len() != 0 {
251                                    allow = true;
252                                } else {
253                                    allow = false;
254                                    break;
255                                }
256                            }
257                            if allow {
258                                output.push(x.to_string());
259                            }
260                            listed_file_dir.pop()
261                        };
262                    }
263                    None => break,
264                }
265            }
266        }
267        output
268    }
269}