1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 22:01:47 +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 {
delegate: Rc<dyn Tab>,
/// The text that the user entered
pattern: String,
pattern: Pattern,
/// The most recently queried set of matches
results: Vec<SearchResult>,
by_line: HashMap<StableRowIndex, Vec<MatchResult>>,
@ -49,14 +49,14 @@ struct SearchRenderable {
}
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 dims = tab.renderer().get_dimensions();
let window = term_window.window.clone().unwrap();
let mut renderer = SearchRenderable {
delegate: Rc::clone(tab),
pattern: String::new(),
pattern,
results: vec![],
by_line: HashMap::new(),
dirty_results: RangeSet::default(),
@ -292,7 +292,7 @@ impl SearchRenderable {
self.dirty_results.add(bar_pos);
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.recompute_results();
@ -343,7 +343,7 @@ impl Renderable for SearchRenderable {
0,
&format!(
"Search: {} ({}/{} matches)",
self.pattern,
*self.pattern,
self.result_pos.map(|x| x + 1).unwrap_or(0),
self.results.len()
),

View File

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

View File

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

View File

@ -137,7 +137,7 @@ impl Tab for LocalTab {
return;
}
match pattern {
Pattern::String(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);

View File

@ -3,6 +3,7 @@ use crate::mux::renderable::Renderable;
use crate::mux::Mux;
use downcast_rs::{impl_downcast, Downcast};
use portable_pty::PtySize;
use serde::{Deserialize, Serialize};
use std::cell::RefMut;
use std::sync::{Arc, Mutex};
use term::color::ColorPalette;
@ -44,11 +45,29 @@ fn schedule_next_paste(paste: &Arc<Mutex<Paste>>) {
});
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Pattern {
String(String),
CaseSensitiveString(String),
// 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)]
pub struct SearchResult {
pub start_y: StableRowIndex,