-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkey_handler.rs
96 lines (88 loc) · 2.86 KB
/
key_handler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::time::Duration;
use crossterm::event::{self, KeyCode, KeyModifiers};
use crate::model::{InputModel, InputState, Message, Model};
pub fn register_key_handler(tx: &kanal::Sender<Message>) {
let tx = tx.clone();
std::thread::spawn(move || -> anyhow::Result<()> {
loop {
if let Ok(true) = event::poll(Duration::from_millis(100)) {
let _ = tx.send(Message::TermEvent(event::read()?));
}
}
});
}
pub fn handle_key(key: event::KeyEvent, model: &Model) -> Option<Message> {
if let InputState::Active(state) = &model.search_input {
handle_search_input(state, key)
} else if let InputState::Active(state) = &model.path_navigator_input {
handle_navigator_input(state, key)
} else if let InputState::Active(state) = &model.new_bookmark_input {
handle_bookmark_input(state, key)
} else {
handle_normal_input(key)
}
}
fn handle_search_input(state: &InputModel, key: event::KeyEvent) -> Option<Message> {
if !state.typing {
match key.code {
KeyCode::Char('n') => Some(Message::SearchNext),
KeyCode::Char('N') => Some(Message::SearchPrev),
KeyCode::Esc => Some(Message::SearchExit),
_ => None,
}
} else {
match key.code {
KeyCode::Esc => Some(Message::SearchExit),
_ => Some(Message::SearchInput(key)),
}
}
}
pub fn handle_bookmark_input(_: &InputModel, key: event::KeyEvent) -> Option<Message> {
match key.code {
KeyCode::Esc => Some(Message::BookmarkInputExit),
KeyCode::Enter => Some(Message::CreateBookmark),
_ => Some(Message::BookmarkInput(key)),
}
}
pub fn handle_navigator_input(state: &InputModel, key: event::KeyEvent) -> Option<Message> {
if !state.typing {
match key.code {
KeyCode::Char('n') => Some(Message::NavigatorNext),
KeyCode::Char('N') => Some(Message::NavigatorPrev),
KeyCode::Esc => Some(Message::NavigatorExit),
_ => Some(Message::NavigatorInput(key)),
}
} else {
match key.code {
KeyCode::Esc => Some(Message::NavigatorExit),
_ => Some(Message::NavigatorInput(key)),
}
}
}
pub fn handle_normal_input(key: event::KeyEvent) -> Option<Message> {
match key.code {
KeyCode::Char('q') => Some(Message::Quit),
KeyCode::Char('h') | KeyCode::Left => Some(Message::Back),
KeyCode::Char('j') | KeyCode::Down => Some(Message::ListDown),
KeyCode::Char('k') | KeyCode::Up => Some(Message::ListUp),
KeyCode::Char('l') | KeyCode::Right => Some(Message::EnterItem),
KeyCode::Char('f') | KeyCode::Char('/') => Some(Message::SearchEnter),
KeyCode::Char('s') => Some(Message::BookmarkInputEnter),
KeyCode::Char('r') => Some(Message::Refresh),
KeyCode::Char('d') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
Some(Message::PageDown)
} else {
Some(Message::DeleteBookmark)
}
}
KeyCode::Char('u') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
return Some(Message::PageUp);
}
None
}
KeyCode::Char('.') => Some(Message::NavigatorEnter),
_ => None,
}
}