Skip to main content

mal/handlers/
common.rs

1// use crate::app::{ActiveBlock, App};
2use crate::event::Key;
3
4pub fn down_event(key: Key) -> bool {
5    matches!(key, Key::Down | Key::Char('j') | Key::Ctrl('n'))
6}
7
8pub fn up_event(key: Key) -> bool {
9    matches!(key, Key::Up | Key::Char('k') | Key::Ctrl('p'))
10}
11
12pub fn left_event(key: Key) -> bool {
13    matches!(key, Key::Left | Key::Char('h') | Key::Ctrl('b'))
14}
15
16pub fn right_event(key: Key) -> bool {
17    matches!(key, Key::Right | Key::Char('l') | Key::Ctrl('f'))
18}
19
20pub fn on_down_press<T>(selection_data: &[T], selection_index: Option<usize>) -> usize {
21    match selection_index {
22        Some(selection_index) => {
23            if !selection_data.is_empty() {
24                let next_index = selection_index + 1;
25                if next_index > selection_data.len() - 1 {
26                    return 0;
27                } else {
28                    return next_index;
29                }
30            }
31            0
32        }
33        None => 0,
34    }
35}
36
37pub fn on_up_press<T>(selection_data: &[T], selection_index: Option<usize>) -> usize {
38    match selection_index {
39        Some(selection_index) => {
40            if !selection_data.is_empty() {
41                if selection_index > 0 {
42                    return selection_index - 1;
43                } else {
44                    return selection_data.len() - 1;
45                }
46            }
47            0
48        }
49        None => 0,
50    }
51}
52
53pub fn quit_event(key: Key) -> bool {
54    matches!(key, Key::Char('q') | Key::Ctrl('C') | Key::Ctrl('c'))
55}
56
57pub fn get_lowercase_key(key: Key) -> Key {
58    match key {
59        Key::Char(c) => Key::Char(c.to_ascii_lowercase()),
60        Key::Ctrl(c) => Key::Ctrl(c.to_ascii_lowercase()),
61        _ => key,
62    }
63}