use crate::state::{AppState, Modal};
use crossterm::event::{KeyCode, KeyEvent};
pub(super) fn restore_if_not_closed_with_excluded_keys(
app: &mut AppState,
ke: &KeyEvent,
excluded_keys: &[KeyCode],
modal: Modal,
) {
if matches!(app.modal, Modal::None) && !excluded_keys.contains(&ke.code) {
app.modal = modal;
}
}
pub(super) fn restore_if_not_closed_with_option_result(
app: &mut AppState,
ke: &KeyEvent,
should_stop: Option<bool>,
modal: Modal,
) -> bool {
if matches!(app.modal, Modal::None) {
if matches!(ke.code, KeyCode::Esc | KeyCode::Char('q')) {
return true;
}
if should_stop.is_none() || should_stop == Some(false) {
app.modal = modal;
}
}
should_stop.unwrap_or(false)
}
pub(super) fn restore_if_not_closed_with_esc(app: &mut AppState, ke: &KeyEvent, modal: Modal) {
if matches!(app.modal, Modal::None) && !matches!(ke.code, KeyCode::Esc) {
app.modal = modal;
}
}
pub(super) fn restore_if_not_closed_with_bool_result(
app: &mut AppState,
result: bool,
modal: Modal,
) -> bool {
if !result && matches!(app.modal, Modal::None) {
app.modal = modal;
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
fn create_key_event(code: KeyCode) -> KeyEvent {
KeyEvent {
code,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
modifiers: KeyModifiers::NONE,
}
}
fn create_app_state_with_modal(modal: Modal) -> AppState {
AppState {
modal,
..Default::default()
}
}
#[test]
fn test_restore_if_not_closed_with_excluded_keys_restores_when_not_excluded() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::Help;
let excluded = [KeyCode::Esc, KeyCode::Char('q')];
restore_if_not_closed_with_excluded_keys(&mut app, &ke, &excluded, modal);
assert!(matches!(app.modal, Modal::Help));
}
#[test]
fn test_restore_if_not_closed_with_excluded_keys_doesnt_restore_when_excluded() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Esc);
let modal = Modal::Help;
let excluded = [KeyCode::Esc, KeyCode::Char('q')];
restore_if_not_closed_with_excluded_keys(&mut app, &ke, &excluded, modal);
assert!(matches!(app.modal, Modal::None));
}
#[test]
fn test_restore_if_not_closed_with_excluded_keys_doesnt_restore_when_modal_not_none() {
let mut app = create_app_state_with_modal(Modal::Help);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::News {
items: vec![],
selected: 0,
scroll: 0,
};
let excluded = [KeyCode::Esc];
restore_if_not_closed_with_excluded_keys(&mut app, &ke, &excluded, modal);
assert!(matches!(app.modal, Modal::Help));
}
#[test]
fn test_restore_if_not_closed_with_option_result_restores_when_none() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::Help;
let result = restore_if_not_closed_with_option_result(&mut app, &ke, None, modal);
assert!(matches!(app.modal, Modal::Help));
assert!(!result);
}
#[test]
fn test_restore_if_not_closed_with_option_result_restores_when_false_and_not_esc() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::Help;
let result = restore_if_not_closed_with_option_result(&mut app, &ke, Some(false), modal);
assert!(matches!(app.modal, Modal::Help));
assert!(!result);
}
#[test]
fn test_restore_if_not_closed_with_option_result_doesnt_restore_when_esc() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Esc);
let modal = Modal::Help;
let result = restore_if_not_closed_with_option_result(&mut app, &ke, Some(false), modal);
assert!(matches!(app.modal, Modal::None));
assert!(result); }
#[test]
fn test_restore_if_not_closed_with_option_result_doesnt_restore_when_q() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Char('q'));
let modal = Modal::Help;
let result = restore_if_not_closed_with_option_result(&mut app, &ke, Some(false), modal);
assert!(matches!(app.modal, Modal::None));
assert!(result); }
#[test]
fn test_restore_if_not_closed_with_option_result_returns_true_when_some_true() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::Help;
let result = restore_if_not_closed_with_option_result(&mut app, &ke, Some(true), modal);
assert!(matches!(app.modal, Modal::None));
assert!(result);
}
#[test]
fn test_restore_if_not_closed_with_esc_restores_when_not_esc() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::Help;
restore_if_not_closed_with_esc(&mut app, &ke, modal);
assert!(matches!(app.modal, Modal::Help));
}
#[test]
fn test_restore_if_not_closed_with_esc_doesnt_restore_when_esc() {
let mut app = create_app_state_with_modal(Modal::None);
let ke = create_key_event(KeyCode::Esc);
let modal = Modal::Help;
restore_if_not_closed_with_esc(&mut app, &ke, modal);
assert!(matches!(app.modal, Modal::None));
}
#[test]
fn test_restore_if_not_closed_with_esc_doesnt_restore_when_modal_not_none() {
let mut app = create_app_state_with_modal(Modal::Help);
let ke = create_key_event(KeyCode::Char('a'));
let modal = Modal::News {
items: vec![],
selected: 0,
scroll: 0,
};
restore_if_not_closed_with_esc(&mut app, &ke, modal);
assert!(matches!(app.modal, Modal::Help));
}
#[test]
fn test_restore_if_not_closed_with_bool_result_restores_when_false() {
let mut app = create_app_state_with_modal(Modal::None);
let modal = Modal::Help;
let result = restore_if_not_closed_with_bool_result(&mut app, false, modal);
assert!(matches!(app.modal, Modal::Help));
assert!(!result);
}
#[test]
fn test_restore_if_not_closed_with_bool_result_doesnt_restore_when_true() {
let mut app = create_app_state_with_modal(Modal::None);
let modal = Modal::Help;
let result = restore_if_not_closed_with_bool_result(&mut app, true, modal);
assert!(matches!(app.modal, Modal::None));
assert!(result);
}
#[test]
fn test_restore_if_not_closed_with_bool_result_doesnt_restore_when_modal_not_none() {
let mut app = create_app_state_with_modal(Modal::Help);
let modal = Modal::News {
items: vec![],
selected: 0,
scroll: 0,
};
let result = restore_if_not_closed_with_bool_result(&mut app, false, modal);
assert!(matches!(app.modal, Modal::Help));
assert!(!result);
}
}