From 7c0ed55fb8ff5e69cb4d27016c6157df19a6d215 Mon Sep 17 00:00:00 2001 From: wegreenall Date: Mon, 18 Mar 2024 19:05:09 +0200 Subject: [PATCH] Add action code for MultiCount, including Increment and Decrement actions. --- inquire/src/prompts/multicount/action.rs | 75 ++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 inquire/src/prompts/multicount/action.rs diff --git a/inquire/src/prompts/multicount/action.rs b/inquire/src/prompts/multicount/action.rs new file mode 100644 index 00000000..e15d5738 --- /dev/null +++ b/inquire/src/prompts/multicount/action.rs @@ -0,0 +1,75 @@ +use crate::{ui::{Key, KeyModifiers}, InnerAction, InputAction}; + +use super::config::MultiCountConfig; + +/// Set of actions for a MultiCountPrompt. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum MultiCountPromptAction{ + /// Action on the value text input handler. + FilterInput(InputAction), + /// Moves the cursor to the option above. + MoveUp, + /// Moves the cursor to the option below. + MoveDown, + /// Moves the cursor to the page above. + PageUp, + /// Moves the cursor to the page below. + PageDown, + /// Moves the cursor to the start of the list. + MoveToStart, + /// Moves the cursor to the end of the list. + MoveToEnd, + /// Toggles the selection of the current option. + SetCountCurrentOption(u32), + /// Increments the current selection by one + Increment, + /// Decrements the current selection by one + Decrement, + /// Increments the current selection by the given amount + MultiIncrement(u32), + /// Decrements the current selection by the given amount + MultiDecrement(u32), + /// Clears counts for all options. + ClearSelections, +} + +impl InnerAction for MultiCountPromptAction { + type Config = MultiCountConfig; + + fn from_key(key: Key, config: &MultiCountConfig) -> Option { + if config.vim_mode { + let action = match key { + Key::Char('k', KeyModifiers::NONE) => Some(Self::MoveUp), + Key::Char('j', KeyModifiers::NONE) => Some(Self::MoveDown), + Key::Char('+', KeyModifiers::NONE) => Some(Self::Increment), + Key::Char('-', KeyModifiers::NONE) => Some(Self::Decrement), + _ => None, + }; + if action.is_some() { + return action; + } + } + + + let action = match key { + Key::Up(KeyModifiers::NONE) | Key::Char('p', KeyModifiers::CONTROL) => Self::MoveUp, + + Key::PageUp(_) => Self::PageUp, + Key::Home => Self::MoveToStart, + + Key::Down(KeyModifiers::NONE) | Key::Char('n', KeyModifiers::CONTROL) => Self::MoveDown, + Key::PageDown(_) => Self::PageDown, + Key::End => Self::MoveToEnd, + + Key::Right(KeyModifiers::NONE) => Self::Increment, + Key::Left(KeyModifiers::NONE) => Self::Decrement, + Key::Right(KeyModifiers::SHIFT) => Self::MultiIncrement(10), + Key::Left(KeyModifiers::SHIFT) => Self::MultiDecrement(10), + key => match InputAction::from_key(key, &()) { + Some(action) => Self::FilterInput(action), + None => return None, + }, + }; + Some(action) + } +}