mirror of
https://github.com/wez/wezterm.git
synced 2024-09-21 19:58:15 +03:00
wezterm: search: redefine trait method as async
This commit is contained in:
parent
cb3606d130
commit
e09d223c7d
@ -201,11 +201,6 @@ impl Tab for SearchOverlay {
|
||||
self.delegate.erase_scrollback()
|
||||
}
|
||||
|
||||
fn search(&self, _pattern: Pattern) -> Vec<SearchResult> {
|
||||
// You can't search the search bar
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn is_mouse_grabbed(&self) -> bool {
|
||||
// Force grabbing off while we're searching
|
||||
false
|
||||
@ -303,13 +298,39 @@ impl SearchRenderable {
|
||||
self.dirty_results.add(bar_pos);
|
||||
|
||||
if !self.pattern.is_empty() {
|
||||
self.results = self.delegate.search(self.pattern.clone());
|
||||
self.results.sort();
|
||||
let tab: Rc<dyn Tab> = self.delegate.clone();
|
||||
let window = self.window.clone();
|
||||
let pattern = self.pattern.clone();
|
||||
promise::spawn::spawn(async move {
|
||||
let mut results = tab.search(pattern).await?;
|
||||
results.sort();
|
||||
|
||||
self.recompute_results();
|
||||
}
|
||||
if !self.results.is_empty() {
|
||||
self.activate_match_number(self.results.len() - 1);
|
||||
let tab_id = tab.tab_id();
|
||||
let mut results = Some(results);
|
||||
window.apply(move |term_window, _window| {
|
||||
let term_window = term_window
|
||||
.downcast_mut::<TermWindow>()
|
||||
.expect("to be TermWindow");
|
||||
let state = term_window.tab_state(tab_id);
|
||||
if let Some(overlay) = state.overlay.as_ref() {
|
||||
if let Some(search_overlay) = overlay.downcast_ref::<SearchOverlay>() {
|
||||
let mut r = search_overlay.renderer.borrow_mut();
|
||||
r.results = results.take().unwrap();
|
||||
r.recompute_results();
|
||||
let num_results = r.results.len();
|
||||
|
||||
if !r.results.is_empty() {
|
||||
r.activate_match_number(num_results - 1);
|
||||
} else {
|
||||
r.set_viewport(None);
|
||||
r.clear_selection();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
anyhow::Result::<()>::Ok(())
|
||||
});
|
||||
} else {
|
||||
self.set_viewport(None);
|
||||
self.clear_selection();
|
||||
|
@ -123,7 +123,7 @@ impl PrevCursorPos {
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct TabState {
|
||||
pub struct TabState {
|
||||
/// If is_some(), the top row of the visible screen.
|
||||
/// Otherwise, the viewport is at the bottom of the
|
||||
/// scrollback.
|
||||
@ -132,7 +132,7 @@ struct TabState {
|
||||
/// If is_some(), rather than display the actual tab
|
||||
/// contents, we're overlaying a little internal application
|
||||
/// tab. We'll also route input to it.
|
||||
overlay: Option<Rc<dyn Tab>>,
|
||||
pub overlay: Option<Rc<dyn Tab>>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
@ -2650,7 +2650,7 @@ impl TermWindow {
|
||||
(fg_color, bg_color, cursor_shape)
|
||||
}
|
||||
|
||||
fn tab_state(&self, tab_id: TabId) -> RefMut<TabState> {
|
||||
pub fn tab_state(&self, tab_id: TabId) -> RefMut<TabState> {
|
||||
RefMut::map(self.tab_state.borrow_mut(), |state| {
|
||||
state.entry(tab_id).or_insert_with(TabState::default)
|
||||
})
|
||||
|
@ -3,6 +3,7 @@ use crate::mux::renderable::Renderable;
|
||||
use crate::mux::tab::{alloc_tab_id, Tab, TabId};
|
||||
use crate::mux::tab::{Pattern, SearchResult};
|
||||
use anyhow::Error;
|
||||
use async_trait::async_trait;
|
||||
use portable_pty::{Child, MasterPty, PtySize};
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::sync::Arc;
|
||||
@ -18,6 +19,7 @@ pub struct LocalTab {
|
||||
domain_id: DomainId,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Tab for LocalTab {
|
||||
#[inline]
|
||||
fn tab_id(&self) -> TabId {
|
||||
@ -100,7 +102,7 @@ impl Tab for LocalTab {
|
||||
self.terminal.borrow().get_current_dir().cloned()
|
||||
}
|
||||
|
||||
fn search(&self, mut pattern: Pattern) -> Vec<SearchResult> {
|
||||
async fn search(&self, mut pattern: Pattern) -> anyhow::Result<Vec<SearchResult>> {
|
||||
let term = self.terminal.borrow();
|
||||
let screen = term.screen();
|
||||
|
||||
@ -203,7 +205,7 @@ impl Tab for LocalTab {
|
||||
}
|
||||
|
||||
collect_matches(&mut results, &pattern, &haystack, &coords);
|
||||
results
|
||||
Ok(results)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::mux::domain::DomainId;
|
||||
use crate::mux::renderable::Renderable;
|
||||
use crate::mux::Mux;
|
||||
use async_trait::async_trait;
|
||||
use downcast_rs::{impl_downcast, Downcast};
|
||||
use portable_pty::PtySize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -83,6 +84,7 @@ pub struct SearchResult {
|
||||
pub end_x: usize,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait Tab: Downcast {
|
||||
fn tab_id(&self) -> TabId;
|
||||
fn renderer(&self) -> RefMut<dyn Renderable>;
|
||||
@ -103,8 +105,8 @@ 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> {
|
||||
vec![]
|
||||
async fn search(&self, _pattern: Pattern) -> anyhow::Result<Vec<SearchResult>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
/// Returns true if the terminal has grabbed the mouse and wants to
|
||||
|
Loading…
Reference in New Issue
Block a user