1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 22:01:47 +03:00

wezterm: add case insensitive search option

This commit is contained in:
Wez Furlong 2020-05-29 09:04:00 -07:00
parent 6b939c9048
commit 7f83d2172c
3 changed files with 26 additions and 8 deletions

View File

@ -190,7 +190,7 @@ impl Tab for SearchOverlay {
self.delegate.erase_scrollback()
}
fn search(&self, _pattern: &Pattern) -> Vec<SearchResult> {
fn search(&self, _pattern: Pattern) -> Vec<SearchResult> {
// You can't search the search bar
vec![]
}
@ -292,7 +292,7 @@ impl SearchRenderable {
self.dirty_results.add(bar_pos);
if !self.pattern.is_empty() {
self.results = self.delegate.search(&self.pattern);
self.results = self.delegate.search(self.pattern.clone());
self.results.sort();
self.recompute_results();

View File

@ -104,10 +104,15 @@ impl Tab for LocalTab {
self.terminal.borrow().get_current_dir().cloned()
}
fn search(&self, pattern: &Pattern) -> Vec<SearchResult> {
fn search(&self, mut pattern: Pattern) -> Vec<SearchResult> {
let term = self.terminal.borrow();
let screen = term.screen();
if let Pattern::CaseInSensitiveString(s) = &mut pattern {
// normalize the case so we match everything lowercase
*s = s.to_lowercase()
}
let mut results = vec![];
let mut haystack = String::new();
let mut coords = vec![];
@ -137,7 +142,10 @@ impl Tab for LocalTab {
return;
}
match pattern {
Pattern::CaseSensitiveString(s) => {
// Rust only provides a case sensitive match_indices function, so
// we have to pre-arrange to lowercase both the pattern and the
// haystack strings
Pattern::CaseInSensitiveString(s) | Pattern::CaseSensitiveString(s) => {
for (idx, s) in haystack.match_indices(s) {
let (start_x, start_y) = haystack_idx_to_coord(idx, coords);
let (end_x, end_y) = haystack_idx_to_coord(idx + s.len(), coords);
@ -166,18 +174,25 @@ impl Tab for LocalTab {
grapheme_idx,
stable_row,
});
haystack.push_str(cell.str());
let s = cell.str();
if let Pattern::CaseInSensitiveString(_) = &pattern {
// normalize the case so we match everything lowercase
haystack.push_str(&s.to_lowercase());
} else {
haystack.push_str(cell.str());
}
wrapped = cell.attrs().wrapped();
}
if !wrapped {
collect_matches(&mut results, pattern, &haystack, &coords);
collect_matches(&mut results, &pattern, &haystack, &coords);
haystack.clear();
coords.clear();
}
}
collect_matches(&mut results, pattern, &haystack, &coords);
collect_matches(&mut results, &pattern, &haystack, &coords);
results
}
}

View File

@ -48,6 +48,7 @@ fn schedule_next_paste(paste: &Arc<Mutex<Paste>>) {
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Pattern {
CaseSensitiveString(String),
CaseInSensitiveString(String),
// Regex(regex::Regex),
}
@ -56,6 +57,7 @@ impl std::ops::Deref for Pattern {
fn deref(&self) -> &String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
}
}
}
@ -64,6 +66,7 @@ impl std::ops::DerefMut for Pattern {
fn deref_mut(&mut self) -> &mut String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
}
}
}
@ -98,7 +101,7 @@ pub trait Tab: Downcast {
/// Performs a search.
/// If the result is empty then there are no matches.
/// Otherwise, the result shall contain all possible matches.
fn search(&self, _pattern: &Pattern) -> Vec<SearchResult> {
fn search(&self, _pattern: Pattern) -> Vec<SearchResult> {
vec![]
}