diff options
author | Canop <cano.petrole@gmail.com> | 2022-03-27 20:48:30 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2022-03-27 20:48:30 +0200 |
commit | a697c79919301313a6942808363f5a9483c3b1d7 (patch) | |
tree | e5d718af7d3a67028302411c7206fbc8a1d41c2f /src | |
parent | a9c90d1d98d52db4e27f0484303dc157b431519c (diff) |
verb filtering by file extension
Fix #508
Diffstat (limited to 'src')
-rw-r--r-- | src/app/selection.rs | 20 | ||||
-rw-r--r-- | src/command/panel_input.rs | 6 | ||||
-rw-r--r-- | src/conf/verb_conf.rs | 8 | ||||
-rw-r--r-- | src/verb/verb.rs | 4 | ||||
-rw-r--r-- | src/verb/verb_store.rs | 10 |
5 files changed, 45 insertions, 3 deletions
diff --git a/src/app/selection.rs b/src/app/selection.rs index eebd791..f435dcb 100644 --- a/src/app/selection.rs +++ b/src/app/selection.rs @@ -124,4 +124,24 @@ impl<'a> SelInfo<'a> { _ => None, } } + pub fn extension(&self) -> Option<&str> { + match self { + SelInfo::None => None, + SelInfo::One(sel) => sel.path.extension().and_then(|e| e.to_str()), + SelInfo::More(stage) => { + let common_extension = stage.paths()[0] + .extension().and_then(|e| e.to_str()); + if common_extension.is_none() { + return None; + } + for path in stage.paths().iter().skip(1) { + let extension = path.extension().and_then(|e| e.to_str()); + if extension != common_extension { + return None; + } + } + common_extension + } + } + } } diff --git a/src/command/panel_input.rs b/src/command/panel_input.rs index 6ccc57a..f1a9406 100644 --- a/src/command/panel_input.rs +++ b/src/command/panel_input.rs @@ -328,6 +328,12 @@ impl PanelInput { if mode != Mode::Input && verb.is_internal(Internal::mode_input) { self.enter_input_mode_with_key(key, &parts); } + if !verb.file_extensions.is_empty() { + let extension = sel_info.extension(); + if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) { + continue; + } + } if verb.auto_exec { return Command::VerbTrigger { index, diff --git a/src/conf/verb_conf.rs b/src/conf/verb_conf.rs index c4c79c5..51540b7 100644 --- a/src/conf/verb_conf.rs +++ b/src/conf/verb_conf.rs @@ -9,7 +9,7 @@ use { serde::Deserialize, }; -/// a deserializable verb entry in the configuration +/// A deserializable verb entry in the configuration #[derive(Default, Debug, Clone, Deserialize)] pub struct VerbConf { @@ -30,6 +30,9 @@ pub struct VerbConf { #[serde(default)] keys: Vec<String>, + #[serde(default)] + extensions: Vec<String>, + shortcut: Option<String>, leave_broot: Option<bool>, @@ -140,6 +143,9 @@ impl VerbConf { } checked_keys.push(key); } + for extension in &self.extensions { + verb.file_extensions.push(extension.clone()); + } if !checked_keys.is_empty() { verb.add_keys(checked_keys); } diff --git a/src/verb/verb.rs b/src/verb/verb.rs index 14b8777..701f3d9 100644 --- a/src/verb/verb.rs +++ b/src/verb/verb.rs @@ -48,6 +48,9 @@ pub struct Verb { /// the type of selection this verb applies to pub selection_condition: SelectionType, + /// extension filtering. If empty, all extensions apply + pub file_extensions: Vec<String>, + /// whether the verb needs a selection pub needs_selection: bool, @@ -101,6 +104,7 @@ impl Verb { execution, description, selection_condition: SelectionType::Any, + file_extensions: Vec::new(), needs_selection, needs_another_panel, auto_exec: true, diff --git a/src/verb/verb_store.rs b/src/verb/verb_store.rs index 534ff53..2cf5628 100644 --- a/src/verb/verb_store.rs +++ b/src/verb/verb_store.rs @@ -57,14 +57,14 @@ impl VerbStore { ) -> PrefixSearchResult<'v, &Verb> { let stype = sel_info.common_stype(); let count = sel_info.count_paths(); - self.search(prefix, stype, Some(count)) + self.search(prefix, stype, Some(count), sel_info.extension()) } pub fn search_prefix<'v>( &'v self, prefix: &str, ) -> PrefixSearchResult<'v, &Verb> { - self.search(prefix, None, None) + self.search(prefix, None, None, None) } pub fn search<'v>( @@ -72,6 +72,7 @@ impl VerbStore { prefix: &str, stype: Option<SelectionType>, sel_count: Option<usize>, + extension: Option<&str>, ) -> PrefixSearchResult<'v, &Verb> { let mut found_index = 0; let mut nb_found = 0; @@ -101,6 +102,11 @@ impl VerbStore { continue; } } + if !verb.file_extensions.is_empty() { + if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) { + continue; + } + } } match nb_found { 0 => PrefixSearchResult::NoMatch, |