Struct broot::pattern::ContentExactPattern
source · pub struct ContentExactPattern { /* private fields */ }
Expand description
A pattern for searching in file content
Implementations§
source§impl ContentExactPattern
impl ContentExactPattern
sourcepub fn new(pat: &str, max_file_size: usize) -> Self
pub fn new(pat: &str, max_file_size: usize) -> Self
Examples found in repository?
src/pattern/pattern.rs (line 73)
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
pub fn new(
raw_expr: &BeTree<PatternOperator, PatternParts>,
search_modes: &SearchModeMap,
content_search_max_file_size: usize,
) -> Result<Self, PatternError> {
let expr: BeTree<PatternOperator, Pattern> = raw_expr
.try_map_atoms::<_, PatternError, _>(|pattern_parts| {
let core = pattern_parts.core();
Ok(
if core.is_empty() {
Pattern::None
} else {
let parts_mode = pattern_parts.mode();
let mode = search_modes.search_mode(parts_mode)?;
let flags = pattern_parts.flags();
match mode {
SearchMode::NameExact => Self::NameExact(
ExactPattern::from(core)
),
SearchMode::NameFuzzy => Self::NameFuzzy(
FuzzyPattern::from(core)
),
SearchMode::NameRegex => Self::NameRegex(
RegexPattern::from(core, flags.unwrap_or(""))?
),
SearchMode::NameTokens => Self::NameTokens(
TokPattern::new(core)
),
SearchMode::PathExact => Self::PathExact(
ExactPattern::from(core)
),
SearchMode::PathFuzzy => Self::PathFuzzy(
FuzzyPattern::from(core)
),
SearchMode::PathRegex => Self::PathRegex(
RegexPattern::from(core, flags.unwrap_or(""))?
),
SearchMode::PathTokens => Self::PathTokens(
TokPattern::new(core)
),
SearchMode::ContentExact => Self::ContentExact(
ContentExactPattern::new(core, content_search_max_file_size)
),
SearchMode::ContentRegex => Self::ContentRegex(
ContentRegexPattern::new(
core,
flags.unwrap_or(""),
content_search_max_file_size,
)?
),
}
}
)
})?;
Ok(if expr.is_empty() {
Pattern::None
} else if expr.is_atomic() {
expr.atoms().pop().unwrap()
} else {
Self::Composite(CompositePattern::new(expr))
})
}
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Examples found in repository?
src/pattern/content_pattern.rs (line 21)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl ContentExactPattern {
pub fn new(pat: &str, max_file_size: usize) -> Self {
Self { needle: Needle::new(pat, max_file_size) }
}
pub fn as_str(&self) -> &str {
self.needle.as_str()
}
pub fn is_empty(&self) -> bool {
self.needle.is_empty()
}
pub fn to_regex_parts(&self) -> (String, String) {
(regex::escape(self.as_str()), "".to_string())
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Examples found in repository?
src/pattern/pattern.rs (line 202)
199 200 201 202 203 204 205 206 207 208 209 210
pub fn is_empty(&self) -> bool {
match self {
Self::NameExact(ep) | Self::PathExact(ep) => ep.is_empty(),
Self::ContentExact(ep) => ep.is_empty(),
Self::NameFuzzy(fp) | Self::PathFuzzy(fp) => fp.is_empty(),
Self::NameRegex(rp) | Self::PathRegex(rp) => rp.is_empty(),
Self::ContentRegex(rp) => rp.is_empty(),
Self::NameTokens(tp) | Self::PathTokens(tp) => tp.is_empty(),
Self::Composite(cp) => cp.is_empty(),
Self::None => true,
}
}
sourcepub fn to_regex_parts(&self) -> (String, String)
pub fn to_regex_parts(&self) -> (String, String)
Examples found in repository?
src/pattern/input_pattern.rs (line 64)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
pub fn tree_to_preview(&self) -> Self {
let regex_parts: Option<(String, String)> = match &self.pattern {
Pattern::ContentExact(cp) => Some(cp.to_regex_parts()),
Pattern::ContentRegex(rp) => Some(rp.to_regex_parts()),
Pattern::Composite(cp) => cp.expr
.iter_atoms()
.find_map(|p| match p {
Pattern::ContentExact(ce) => Some(ce.to_regex_parts()),
Pattern::ContentRegex(cp) => Some(cp.to_regex_parts()),
_ => None
}),
_ => None,
};
regex_parts
.and_then(|rp| RegexPattern::from(&rp.0, &rp.1).ok())
.map(|rp| InputPattern {
raw: rp.to_string(),
pattern: Pattern::NameRegex(rp),
})
.unwrap_or_else(InputPattern::none)
}
sourcepub fn score_of(&self, candidate: Candidate<'_>) -> Option<i32>
pub fn score_of(&self, candidate: Candidate<'_>) -> Option<i32>
Examples found in repository?
src/pattern/pattern.rs (line 169)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
pub fn score_of(&self, candidate: Candidate) -> Option<i32> {
match self {
Self::NameExact(ep) => ep.score_of(candidate.name),
Self::NameFuzzy(fp) => fp.score_of(candidate.name),
Self::NameRegex(rp) => rp.find(candidate.name).map(|m| m.score),
Self::NameTokens(tp) => tp.score_of(candidate.name),
Self::PathExact(ep) => ep.score_of(candidate.subpath),
Self::PathFuzzy(fp) => fp.score_of(candidate.subpath),
Self::PathRegex(rp) => rp.find(candidate.subpath).map(|m| m.score),
Self::PathTokens(tp) => tp.score_of(candidate.subpath),
Self::ContentExact(cp) => cp.score_of(candidate),
Self::ContentRegex(cp) => cp.score_of(candidate),
Self::Composite(cp) => cp.score_of(candidate),
Self::None => Some(1),
}
}
sourcepub fn get_match_line_count(&self, path: &Path) -> Option<usize>
pub fn get_match_line_count(&self, path: &Path) -> Option<usize>
get the line of the first match, if any
sourcepub fn get_content_match(
&self,
path: &Path,
desired_len: usize
) -> Option<ContentMatch>
pub fn get_content_match(
&self,
path: &Path,
desired_len: usize
) -> Option<ContentMatch>
Examples found in repository?
src/pattern/pattern.rs (line 139)
133 134 135 136 137 138 139 140 141 142 143 144
pub fn search_content(
&self,
candidate: &Path,
desired_len: usize, // available space for content match display
) -> Option<ContentMatch> {
match self {
Self::ContentExact(cp) => cp.get_content_match(candidate, desired_len),
Self::ContentRegex(cp) => cp.get_content_match(candidate, desired_len),
Self::Composite(cp) => cp.search_content(candidate, desired_len),
_ => None,
}
}
Trait Implementations§
source§impl Clone for ContentExactPattern
impl Clone for ContentExactPattern
source§fn clone(&self) -> ContentExactPattern
fn clone(&self) -> ContentExactPattern
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more