inquire/prompts/text/
action.rs

1use crate::{
2    ui::{Key, KeyModifiers},
3    InnerAction, InputAction,
4};
5
6use super::config::TextConfig;
7
8/// Set of actions for a TextPrompt.
9#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10#[allow(clippy::enum_variant_names)]
11pub enum TextPromptAction {
12    /// Action on the value text input handler.
13    ValueInput(InputAction),
14    /// When a suggestion list exists, moves the cursor to the option above.
15    MoveToSuggestionAbove,
16    /// When a suggestion list exists, moves the cursor to the option below.
17    MoveToSuggestionBelow,
18    /// When a suggestion list exists, moves the cursor to the page above.
19    MoveToSuggestionPageUp,
20    /// When a suggestion list exists, moves the cursor to the page below.
21    MoveToSuggestionPageDown,
22    /// When a suggestion list exists, autocompletes the text input with the current suggestion.
23    UseCurrentSuggestion,
24}
25
26impl InnerAction for TextPromptAction {
27    type Config = TextConfig;
28
29    fn from_key(key: Key, _config: &TextConfig) -> Option<Self> {
30        let action = match key {
31            Key::Up(KeyModifiers::NONE) | Key::Char('p', KeyModifiers::CONTROL) => {
32                Self::MoveToSuggestionAbove
33            }
34            Key::PageUp(_) => Self::MoveToSuggestionPageUp,
35
36            Key::Down(KeyModifiers::NONE) | Key::Char('n', KeyModifiers::CONTROL) => {
37                Self::MoveToSuggestionBelow
38            }
39            Key::PageDown(_) => Self::MoveToSuggestionPageDown,
40
41            Key::Tab => Self::UseCurrentSuggestion,
42
43            key => match InputAction::from_key(key, &()) {
44                Some(action) => Self::ValueInput(action),
45                None => return None,
46            },
47        };
48
49        Some(action)
50    }
51}