diff options
author | Canop <cano.petrole@gmail.com> | 2020-07-25 16:36:38 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-07-25 16:36:38 +0200 |
commit | e765aa34226a7e9b043272b361b96a4679b466f2 (patch) | |
tree | 1ceb751b07eb864d53798adead7b43351bebf2f3 /src/pattern | |
parent | 564528170fcac15399cac60a2fc013635702d3a9 (diff) |
content search in tree propagated as regex to preview
on :panel_right (ctrl-right)
Diffstat (limited to 'src/pattern')
-rw-r--r-- | src/pattern/composite_pattern.rs | 10 | ||||
-rw-r--r-- | src/pattern/input_pattern.rs | 11 | ||||
-rw-r--r-- | src/pattern/pattern.rs | 17 | ||||
-rw-r--r-- | src/pattern/regex_patterns.rs | 2 |
4 files changed, 37 insertions, 3 deletions
diff --git a/src/pattern/composite_pattern.rs b/src/pattern/composite_pattern.rs index 7ef1981..6e506ad 100644 --- a/src/pattern/composite_pattern.rs +++ b/src/pattern/composite_pattern.rs @@ -165,5 +165,15 @@ impl CompositePattern { } } + /// + pub fn get_content_pattern(&self) -> Option<&ContentPattern> { + for pattern in self.expr.iter_atoms() { + match pattern { + Pattern::Content(cp) => { return Some(&cp); } + _ => {} + } + } + return None; + } } diff --git a/src/pattern/input_pattern.rs b/src/pattern/input_pattern.rs index 6e12eae..02e079d 100644 --- a/src/pattern/input_pattern.rs +++ b/src/pattern/input_pattern.rs @@ -44,4 +44,15 @@ impl InputPattern { pub fn take(&mut self) -> Self { std::mem::replace(self, Self::none()) } + /// from a pattern used to filter a tree, build a pattern + /// which would make sense to filter a previewed file + pub fn tree_to_preview(&self) -> Self { + self.pattern.get_content_pattern() + .and_then(|cp| RegexPattern::from(&cp.to_string(), "").ok()) + .map(|rp| InputPattern { + raw: rp.to_string(), + pattern: Pattern::NameRegex(rp), + }) + .unwrap_or_else(|| InputPattern::none()) + } } diff --git a/src/pattern/pattern.rs b/src/pattern/pattern.rs index 1a64ad8..15e32b0 100644 --- a/src/pattern/pattern.rs +++ b/src/pattern/pattern.rs @@ -43,8 +43,12 @@ impl Pattern { match mode { SearchMode::NameFuzzy => Self::NameFuzzy(FuzzyPattern::from(core)), SearchMode::PathFuzzy => Self::PathFuzzy(FuzzyPattern::from(core)), - SearchMode::NameRegex => Self::NameRegex(RegexPattern::from(core, flags.as_deref().unwrap_or(""))?), - SearchMode::PathRegex => Self::PathRegex(RegexPattern::from(core, flags.unwrap_or(""))?), + SearchMode::NameRegex => { + Self::NameRegex(RegexPattern::from(core, flags.as_deref().unwrap_or(""))?) + } + SearchMode::PathRegex => { + Self::PathRegex(RegexPattern::from(core, flags.unwrap_or(""))?) + } SearchMode::Content => Self::Content(ContentPattern::from(core)), } } @@ -157,5 +161,14 @@ impl Pattern { _ => targeted_size, } } + + /// + pub fn get_content_pattern(&self) -> Option<&ContentPattern> { + match self { + Pattern::Content(cp) => Some(cp), + Pattern::Composite(cp) => cp.get_content_pattern(), + _ => None, + } + } } diff --git a/src/pattern/regex_patterns.rs b/src/pattern/regex_patterns.rs index 7684d42..d32ea8d 100644 --- a/src/pattern/regex_patterns.rs +++ b/src/pattern/regex_patterns.rs @@ -15,7 +15,7 @@ pub struct RegexPattern { impl fmt::Display for RegexPattern { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}/{}", self.rex, self.flags) + write!(f, "/{}/{}", self.rex, self.flags) } } |