inquire/prompts/select/
action.rs

1use crate::{
2    ui::{Key, KeyModifiers},
3    InnerAction, InputAction,
4};
5
6use super::config::SelectConfig;
7
8/// Set of actions for a SelectPrompt.
9#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10pub enum SelectPromptAction {
11    /// Action on the value text input handler.
12    FilterInput(InputAction),
13    /// Moves the cursor to the option above.
14    MoveUp,
15    /// Moves the cursor to the option below.
16    MoveDown,
17    /// Moves the cursor to the page above.
18    PageUp,
19    /// Moves the cursor to the page below.
20    PageDown,
21    /// Moves the cursor to the start of the list.
22    MoveToStart,
23    /// Moves the cursor to the end of the list.
24    MoveToEnd,
25}
26
27impl InnerAction for SelectPromptAction {
28    type Config = SelectConfig;
29
30    fn from_key(key: Key, config: &SelectConfig) -> Option<Self> {
31        if config.vim_mode {
32            let action = match key {
33                Key::Char('k', KeyModifiers::NONE) => Some(Self::MoveUp),
34                Key::Char('j', KeyModifiers::NONE) => Some(Self::MoveDown),
35                _ => None,
36            };
37
38            if action.is_some() {
39                return action;
40            }
41        }
42
43        let action = match key {
44            Key::Up(KeyModifiers::NONE) | Key::Char('p', KeyModifiers::CONTROL) => Self::MoveUp,
45            Key::PageUp(_) => Self::PageUp,
46            Key::Home => Self::MoveToStart,
47
48            Key::Down(KeyModifiers::NONE) | Key::Char('n', KeyModifiers::CONTROL) => Self::MoveDown,
49            Key::PageDown(_) => Self::PageDown,
50            Key::End => Self::MoveToEnd,
51
52            key => match InputAction::from_key(key, &()) {
53                Some(action) => Self::FilterInput(action),
54                None => return None,
55            },
56        };
57
58        Some(action)
59    }
60}