use crate::event::Key;
pub fn down_event(key: Key) -> bool {
matches!(key, Key::Down | Key::Char('j') | Key::Ctrl('n'))
}
pub fn up_event(key: Key) -> bool {
matches!(key, Key::Up | Key::Char('k') | Key::Ctrl('p'))
}
pub fn left_event(key: Key) -> bool {
matches!(key, Key::Left | Key::Char('h') | Key::Ctrl('b'))
}
pub fn right_event(key: Key) -> bool {
matches!(key, Key::Right | Key::Char('l') | Key::Ctrl('f'))
}
pub fn on_down_press<T>(selection_data: &[T], selection_index: Option<usize>) -> usize {
match selection_index {
Some(selection_index) => {
if !selection_data.is_empty() {
let next_index = selection_index + 1;
if next_index > selection_data.len() - 1 {
return 0;
} else {
return next_index;
}
}
0
}
None => 0,
}
}
pub fn on_up_press<T>(selection_data: &[T], selection_index: Option<usize>) -> usize {
match selection_index {
Some(selection_index) => {
if !selection_data.is_empty() {
if selection_index > 0 {
return selection_index - 1;
} else {
return selection_data.len() - 1;
}
}
0
}
None => 0,
}
}
pub fn quit_event(key: Key) -> bool {
matches!(key, Key::Char('q') | Key::Ctrl('C') | Key::Ctrl('c'))
}
pub fn get_lowercase_key(key: Key) -> Key {
match key {
Key::Char(c) => Key::Char(c.to_ascii_lowercase()),
Key::Ctrl(c) => Key::Ctrl(c.to_ascii_lowercase()),
_ => key,
}
}