1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 14:22:37 +03:00

wezterm: search: allow specifying pattern in key assignment

The default opens up search with an empty pattern in case sensitive
string matching mode.

The revised structure will allow doing things like eg: defining a key
assignment that searches for git hashes by regex.
This commit is contained in:
Wez Furlong 2020-05-29 08:41:06 -07:00
parent 44c3ee0e83
commit 6b939c9048
5 changed files with 40 additions and 12 deletions

View File

@ -31,7 +31,7 @@ struct MatchResult {
struct SearchRenderable { struct SearchRenderable {
delegate: Rc<dyn Tab>, delegate: Rc<dyn Tab>,
/// The text that the user entered /// The text that the user entered
pattern: String, pattern: Pattern,
/// The most recently queried set of matches /// The most recently queried set of matches
results: Vec<SearchResult>, results: Vec<SearchResult>,
by_line: HashMap<StableRowIndex, Vec<MatchResult>>, by_line: HashMap<StableRowIndex, Vec<MatchResult>>,
@ -49,14 +49,14 @@ struct SearchRenderable {
} }
impl SearchOverlay { impl SearchOverlay {
pub fn with_tab(term_window: &TermWindow, tab: &Rc<dyn Tab>) -> Rc<dyn Tab> { pub fn with_tab(term_window: &TermWindow, tab: &Rc<dyn Tab>, pattern: Pattern) -> Rc<dyn Tab> {
let viewport = term_window.get_viewport(tab.tab_id()); let viewport = term_window.get_viewport(tab.tab_id());
let dims = tab.renderer().get_dimensions(); let dims = tab.renderer().get_dimensions();
let window = term_window.window.clone().unwrap(); let window = term_window.window.clone().unwrap();
let mut renderer = SearchRenderable { let mut renderer = SearchRenderable {
delegate: Rc::clone(tab), delegate: Rc::clone(tab),
pattern: String::new(), pattern,
results: vec![], results: vec![],
by_line: HashMap::new(), by_line: HashMap::new(),
dirty_results: RangeSet::default(), dirty_results: RangeSet::default(),
@ -292,7 +292,7 @@ impl SearchRenderable {
self.dirty_results.add(bar_pos); self.dirty_results.add(bar_pos);
if !self.pattern.is_empty() { if !self.pattern.is_empty() {
self.results = self.delegate.search(&Pattern::String(self.pattern.clone())); self.results = self.delegate.search(&self.pattern);
self.results.sort(); self.results.sort();
self.recompute_results(); self.recompute_results();
@ -343,7 +343,7 @@ impl Renderable for SearchRenderable {
0, 0,
&format!( &format!(
"Search: {} ({}/{} matches)", "Search: {} ({}/{} matches)",
self.pattern, *self.pattern,
self.result_pos.map(|x| x + 1).unwrap_or(0), self.result_pos.map(|x| x + 1).unwrap_or(0),
self.results.len() self.results.len()
), ),

View File

@ -1489,8 +1489,8 @@ impl TermWindow {
let window = self.window.as_ref().unwrap(); let window = self.window.as_ref().unwrap();
window.invalidate(); window.invalidate();
} }
Search => { Search(pattern) => {
let search = SearchOverlay::with_tab(self, tab); let search = SearchOverlay::with_tab(self, tab, pattern.clone());
self.assign_overlay(tab.tab_id(), search); self.assign_overlay(tab.tab_id(), search);
} }
}; };

View File

@ -1,6 +1,7 @@
use crate::config::configuration; use crate::config::configuration;
use crate::frontend::gui::SelectionMode; use crate::frontend::gui::SelectionMode;
use crate::mux::domain::DomainId; use crate::mux::domain::DomainId;
use crate::mux::tab::Pattern;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
@ -98,7 +99,7 @@ pub enum KeyAssignment {
SpawnCommandInNewWindow(SpawnCommand), SpawnCommandInNewWindow(SpawnCommand),
ShowLauncher, ShowLauncher,
ClearScrollback, ClearScrollback,
Search, Search(Pattern),
SelectTextAtMouseCursor(SelectionMode), SelectTextAtMouseCursor(SelectionMode),
ExtendSelectionToMouseCursor(Option<SelectionMode>), ExtendSelectionToMouseCursor(Option<SelectionMode>),
@ -163,8 +164,16 @@ impl InputMap {
[ctrl_shift, KeyCode::Char('N'), SpawnWindow], [ctrl_shift, KeyCode::Char('N'), SpawnWindow],
[KeyModifiers::SUPER, KeyCode::Char('k'), ClearScrollback], [KeyModifiers::SUPER, KeyCode::Char('k'), ClearScrollback],
[ctrl_shift, KeyCode::Char('K'), ClearScrollback], [ctrl_shift, KeyCode::Char('K'), ClearScrollback],
[KeyModifiers::SUPER, KeyCode::Char('f'), Search], [
[ctrl_shift, KeyCode::Char('F'), Search], KeyModifiers::SUPER,
KeyCode::Char('f'),
Search(Pattern::CaseSensitiveString("".into()))
],
[
ctrl_shift,
KeyCode::Char('F'),
Search(Pattern::CaseSensitiveString("".into()))
],
// Font size manipulation // Font size manipulation
[KeyModifiers::CTRL, KeyCode::Char('-'), DecreaseFontSize], [KeyModifiers::CTRL, KeyCode::Char('-'), DecreaseFontSize],
[KeyModifiers::CTRL, KeyCode::Char('0'), ResetFontSize], [KeyModifiers::CTRL, KeyCode::Char('0'), ResetFontSize],

View File

@ -137,7 +137,7 @@ impl Tab for LocalTab {
return; return;
} }
match pattern { match pattern {
Pattern::String(s) => { Pattern::CaseSensitiveString(s) => {
for (idx, s) in haystack.match_indices(s) { for (idx, s) in haystack.match_indices(s) {
let (start_x, start_y) = haystack_idx_to_coord(idx, coords); let (start_x, start_y) = haystack_idx_to_coord(idx, coords);
let (end_x, end_y) = haystack_idx_to_coord(idx + s.len(), coords); let (end_x, end_y) = haystack_idx_to_coord(idx + s.len(), coords);

View File

@ -3,6 +3,7 @@ use crate::mux::renderable::Renderable;
use crate::mux::Mux; use crate::mux::Mux;
use downcast_rs::{impl_downcast, Downcast}; use downcast_rs::{impl_downcast, Downcast};
use portable_pty::PtySize; use portable_pty::PtySize;
use serde::{Deserialize, Serialize};
use std::cell::RefMut; use std::cell::RefMut;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use term::color::ColorPalette; use term::color::ColorPalette;
@ -44,11 +45,29 @@ fn schedule_next_paste(paste: &Arc<Mutex<Paste>>) {
}); });
} }
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Pattern { pub enum Pattern {
String(String), CaseSensitiveString(String),
// Regex(regex::Regex), // Regex(regex::Regex),
} }
impl std::ops::Deref for Pattern {
type Target = String;
fn deref(&self) -> &String {
match self {
Pattern::CaseSensitiveString(s) => s,
}
}
}
impl std::ops::DerefMut for Pattern {
fn deref_mut(&mut self) -> &mut String {
match self {
Pattern::CaseSensitiveString(s) => s,
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
pub struct SearchResult { pub struct SearchResult {
pub start_y: StableRowIndex, pub start_y: StableRowIndex,