inquire/prompts/multiselect/
action.rs

1use crate::{
2    ui::{Key, KeyModifiers},
3    InnerAction, InputAction,
4};
5
6use super::config::MultiSelectConfig;
7
8/// Set of actions for a MultiSelectPrompt.
9#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10pub enum MultiSelectPromptAction {
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    /// Toggles the selection of the current option.
26    ToggleCurrentOption,
27    /// Selects all options.
28    SelectAll,
29    /// Deselects all options.
30    ClearSelections,
31}
32
33impl InnerAction for MultiSelectPromptAction {
34    type Config = MultiSelectConfig;
35
36    fn from_key(key: Key, config: &MultiSelectConfig) -> Option<Self> {
37        if config.vim_mode {
38            let action = match key {
39                Key::Char('h', KeyModifiers::NONE) => Some(Self::ClearSelections),
40                Key::Char('k', KeyModifiers::NONE) => Some(Self::MoveUp),
41                Key::Char('j', KeyModifiers::NONE) => Some(Self::MoveDown),
42                Key::Char('l', KeyModifiers::NONE) => Some(Self::SelectAll),
43                _ => None,
44            };
45
46            if action.is_some() {
47                return action;
48            }
49        }
50
51        let action = match key {
52            Key::Up(KeyModifiers::NONE) | Key::Char('p', KeyModifiers::CONTROL) => Self::MoveUp,
53            Key::PageUp(_) => Self::PageUp,
54            Key::Home => Self::MoveToStart,
55
56            Key::Down(KeyModifiers::NONE) | Key::Char('n', KeyModifiers::CONTROL) => Self::MoveDown,
57            Key::PageDown(_) => Self::PageDown,
58            Key::End => Self::MoveToEnd,
59
60            Key::Char(' ', KeyModifiers::NONE) => Self::ToggleCurrentOption,
61            Key::Right(KeyModifiers::NONE) => Self::SelectAll,
62            Key::Left(KeyModifiers::NONE) => Self::ClearSelections,
63            key => match InputAction::from_key(key, &()) {
64                Some(action) => Self::FilterInput(action),
65                None => return None,
66            },
67        };
68
69        Some(action)
70    }
71}