-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
398 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::{cell::RefCell, rc::Rc}; | ||
|
||
use crossterm::event::KeyEvent; | ||
|
||
use crate::{application::event::KeyEventResult, keymap::Keymaps, ui::widgets::prompt::Prompt}; | ||
|
||
pub enum KeyHandler { | ||
Prompt(Rc<RefCell<Prompt>>), | ||
Keymaps(Keymaps), | ||
} | ||
|
||
impl KeyHandler { | ||
fn handle(&mut self, event: &KeyEvent) -> KeyEventResult { | ||
match self { | ||
KeyHandler::Prompt(prompt) => prompt.borrow_mut().handle_key_event(event), | ||
KeyHandler::Keymaps(keymaps) => keymaps.search(event), | ||
} | ||
} | ||
} | ||
|
||
pub struct KeyHandlers { | ||
handlers: Vec<KeyHandler>, | ||
} | ||
|
||
impl KeyHandlers { | ||
pub fn new() -> Self { | ||
Self { | ||
handlers: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn push(&mut self, handler: KeyHandler) { | ||
self.handlers.push(handler); | ||
} | ||
|
||
pub fn remove_prompt(&mut self) { | ||
self.handlers | ||
.retain(|h| !matches!(h, KeyHandler::Prompt(_))); | ||
} | ||
|
||
pub fn keymaps_mut(&mut self) -> Option<&mut Keymaps> { | ||
for handler in &mut self.handlers { | ||
match handler { | ||
KeyHandler::Keymaps(keymaps) => return Some(keymaps), | ||
KeyHandler::Prompt(_) => continue, | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub fn handle(&mut self, event: KeyEvent) -> KeyEventResult { | ||
for handler in self.handlers.iter_mut().rev() { | ||
if let KeyEventResult::Consumed(r) = handler.handle(&event) { | ||
return KeyEventResult::Consumed(r); | ||
} | ||
} | ||
KeyEventResult::Ignored | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::command::Command; | ||
|
||
mod key_handlers; | ||
pub use key_handlers::{KeyHandler, KeyHandlers}; | ||
|
||
pub enum KeyEventResult { | ||
Consumed(Option<Command>), | ||
Ignored, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::{cell::RefCell, fmt, rc::Rc}; | ||
|
||
use nucleo::{ | ||
pattern::{AtomKind, CaseMatching, Normalization, Pattern}, | ||
Utf32Str, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct Matcher { | ||
matcher: Rc<RefCell<nucleo::Matcher>>, | ||
needle: Option<Pattern>, | ||
// For Utf32 conversion | ||
buf: Rc<RefCell<Vec<char>>>, | ||
} | ||
|
||
impl Matcher { | ||
pub fn new() -> Self { | ||
Self { | ||
matcher: Rc::new(RefCell::new(nucleo::Matcher::default())), | ||
needle: None, | ||
buf: Rc::new(RefCell::new(Vec::new())), | ||
} | ||
} | ||
|
||
pub fn update_needle(&mut self, needle: &str) { | ||
if needle.is_empty() { | ||
self.needle = None; | ||
} else { | ||
let needle = Pattern::new( | ||
needle, | ||
CaseMatching::Smart, | ||
Normalization::Smart, | ||
AtomKind::Substring, | ||
); | ||
self.needle = Some(needle); | ||
} | ||
} | ||
|
||
pub fn r#match(&self, haystack: &str) -> bool { | ||
match self.needle.as_ref() { | ||
Some(needle) => { | ||
let mut buf = self.buf.borrow_mut(); | ||
let haystack = Utf32Str::new(haystack, &mut buf); | ||
needle | ||
.score(haystack, &mut self.matcher.borrow_mut()) | ||
.unwrap_or(0) | ||
> 0 | ||
} | ||
None => true, | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for Matcher { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Matcher") | ||
.field("needle", &self.needle) | ||
.finish_non_exhaustive() | ||
} | ||
} |
Oops, something went wrong.