1use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
2
3use super::action::Action;
4use super::app::Screen;
5
6pub fn resolve_action(screen: &Screen, key: &KeyEvent) -> Action {
7 if key.modifiers.contains(KeyModifiers::CONTROL)
9 && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('d'))
10 {
11 return Action::Quit;
12 }
13
14 match screen {
15 Screen::ArticleList => resolve_list_action(key),
16 Screen::ArticleView => resolve_view_action(key),
17 }
18}
19
20fn resolve_list_action(key: &KeyEvent) -> Action {
21 match key.code {
22 KeyCode::Char('q') | KeyCode::Esc => Action::Quit,
23 KeyCode::Char('j') | KeyCode::Down => Action::MoveDown,
24 KeyCode::Char('k') | KeyCode::Up => Action::MoveUp,
25 KeyCode::Enter | KeyCode::Char(' ') => Action::OpenArticle,
26 KeyCode::Char('o') => Action::OpenInBrowser,
27 KeyCode::Char('m') => Action::ToggleRead,
28 KeyCode::Char('a') => Action::ToggleReadFilter,
29 KeyCode::Char('r') => Action::Refresh,
30 _ => Action::None,
31 }
32}
33
34fn resolve_view_action(key: &KeyEvent) -> Action {
35 match key.code {
36 KeyCode::Char('q') | KeyCode::Esc => Action::BackToList,
37 KeyCode::Char('j') | KeyCode::Down => Action::ScrollDown,
38 KeyCode::Char('k') | KeyCode::Up => Action::ScrollUp,
39 KeyCode::Char('h') | KeyCode::Left => Action::PrevArticle,
40 KeyCode::Char('l') | KeyCode::Right => Action::NextArticle,
41 KeyCode::Char(' ') => Action::PageDown,
42 KeyCode::Char('o') => Action::OpenInBrowser,
43 KeyCode::Char('m') => Action::ToggleRead,
44 _ => Action::None,
45 }
46}