diff options
author | Noah Too <krivahtoo@gmail.com> | 2022-10-17 19:39:53 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-17 12:39:53 -0400 |
commit | be452d4c9abf7891111138000fe7683f169d69a5 (patch) | |
tree | 75d9967795d601f7dc7eacd5e3dede155bd36d1f /src/config | |
parent | 02c7e941566a2d828e77dd33a6dda74c8a0c42ef (diff) |
add `filter` command (#205)
* add `filter` command
* cargo fmt
Diffstat (limited to 'src/config')
-rw-r--r-- | src/config/general/display_raw.rs | 1 | ||||
-rw-r--r-- | src/config/option/display_option.rs | 63 |
2 files changed, 49 insertions, 15 deletions
diff --git a/src/config/general/display_raw.rs b/src/config/general/display_raw.rs index dc0dc9f..c594e5c 100644 --- a/src/config/general/display_raw.rs +++ b/src/config/general/display_raw.rs @@ -125,6 +125,7 @@ impl From<DisplayOptionRaw> for DisplayOption { no_preview_layout, default_tab_display_option: TabDisplayOption { _sort_options: raw.sort_options.into(), + filter_string: "".to_owned(), }, } } diff --git a/src/config/option/display_option.rs b/src/config/option/display_option.rs index 1578e06..a076db8 100644 --- a/src/config/option/display_option.rs +++ b/src/config/option/display_option.rs @@ -36,6 +36,7 @@ pub struct DisplayOption { #[derive(Clone, Debug)] pub struct TabDisplayOption { pub _sort_options: SortOption, + pub filter_string: String, } #[derive(Clone, Copy, Debug)] @@ -53,6 +54,14 @@ impl TabDisplayOption { pub fn sort_options_mut(&mut self) -> &mut SortOption { &mut self._sort_options } + + pub fn set_filter_string(&mut self, pattern: &str) { + self.filter_string = pattern.to_owned(); + } + + pub fn filter_string_ref(&self) -> &str { + &self.filter_string + } } impl DisplayOption { @@ -100,12 +109,10 @@ impl DisplayOption { self._line_nums = style; } - pub fn filter_func(&self) -> fn(&Result<fs::DirEntry, std::io::Error>) -> bool { - if self.show_hidden() { - no_filter - } else { - filter_hidden - } + pub fn filter_func( + &self, + ) -> fn(&Result<fs::DirEntry, std::io::Error>, &DisplayOption, &TabDisplayOption) -> bool { + filter } } @@ -140,22 +147,48 @@ impl std::default::Default for DisplayOption { no_preview_layout, default_tab_display_option: TabDisplayOption { _sort_options: SortOption::default(), + filter_string: "".to_owned(), }, } } } -const fn no_filter(_: &Result<fs::DirEntry, std::io::Error>) -> bool { - true +fn has_str(entry: &fs::DirEntry, pat: &str) -> bool { + match entry.file_name().into_string().ok() { + Some(s) => s + .to_ascii_lowercase() + .contains(pat.to_ascii_lowercase().as_str()), + None => false, + } } -fn filter_hidden(result: &Result<fs::DirEntry, std::io::Error>) -> bool { - match result { - Err(_) => true, - Ok(entry) => { - let file_name = entry.file_name(); - let lossy_string = file_name.as_os_str().to_string_lossy(); - !lossy_string.starts_with('.') +fn filter( + result: &Result<fs::DirEntry, std::io::Error>, + opt: &DisplayOption, + tab_opts: &TabDisplayOption, +) -> bool { + if opt.show_hidden() && tab_opts.filter_string_ref().is_empty() { + true + } else { + match result { + Err(_) => true, + Ok(entry) => { + if tab_opts.filter_string_ref().is_empty() { + let file_name = entry.file_name(); + let lossy_string = file_name.as_os_str().to_string_lossy(); + !lossy_string.starts_with('.') + } else if opt.show_hidden() { + has_str(entry, tab_opts.filter_string_ref()) + } else { + let file_name = entry.file_name(); + let lossy_string = file_name.as_os_str().to_string_lossy(); + if !lossy_string.starts_with('.') { + has_str(entry, tab_opts.filter_string_ref()) + } else { + false + } + } + } } } } |