Skip to content

Commit

Permalink
Add action code for MultiCount, including Increment and Decrement
Browse files Browse the repository at this point in the history
actions.
  • Loading branch information
wegreenall committed Mar 18, 2024
1 parent e1cf5ba commit 7c0ed55
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions inquire/src/prompts/multicount/action.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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)
}
}

0 comments on commit 7c0ed55

Please sign in to comment.