refactor(handler): refer to self.config.keybinds internally

Because `handle_key` can refer to `self.config.keybinds` by itself, it
doesn't have to receive the third argument. Also, this change may
slightly improve the performance as it deletes `clone()`.
This commit is contained in:
Yutaka Kamei 2021-04-28 13:40:04 +09:00
parent f172a01dac
commit 82c5f505f1

View file

@ -58,7 +58,6 @@ impl InputHandler {
fn handle_input(&mut self) { fn handle_input(&mut self) {
let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow()); let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
err_ctx.add_call(ContextType::StdinHandler); err_ctx.add_call(ContextType::StdinHandler);
let keybinds = self.config.keybinds.clone();
let alt_left_bracket = vec![27, 91]; let alt_left_bracket = vec![27, 91];
loop { loop {
if self.should_exit { if self.should_exit {
@ -70,14 +69,14 @@ impl InputHandler {
Ok((event, raw_bytes)) => match event { Ok((event, raw_bytes)) => match event {
termion::event::Event::Key(key) => { termion::event::Event::Key(key) => {
let key = cast_termion_key(key); let key = cast_termion_key(key);
self.handle_key(&key, raw_bytes, &keybinds); self.handle_key(&key, raw_bytes);
} }
termion::event::Event::Unsupported(unsupported_key) => { termion::event::Event::Unsupported(unsupported_key) => {
// we have to do this because of a bug in termion // we have to do this because of a bug in termion
// this should be a key event and not an unsupported event // this should be a key event and not an unsupported event
if unsupported_key == alt_left_bracket { if unsupported_key == alt_left_bracket {
let key = Key::Alt('['); let key = Key::Alt('[');
self.handle_key(&key, raw_bytes, &keybinds); self.handle_key(&key, raw_bytes);
} }
} }
termion::event::Event::Mouse(_) => { termion::event::Event::Mouse(_) => {
@ -90,8 +89,9 @@ impl InputHandler {
} }
} }
} }
fn handle_key(&mut self, key: &Key, raw_bytes: Vec<u8>, keybinds: &Keybinds) { fn handle_key(&mut self, key: &Key, raw_bytes: Vec<u8>) {
for action in Keybinds::key_to_actions(&key, raw_bytes, &self.mode, &keybinds) { let keybinds = &self.config.keybinds;
for action in Keybinds::key_to_actions(&key, raw_bytes, &self.mode, keybinds) {
let should_exit = self.dispatch_action(action); let should_exit = self.dispatch_action(action);
if should_exit { if should_exit {
self.should_exit = true; self.should_exit = true;