inquire/prompts/dateselect/
action.rs

1use crate::{
2    ui::{Key, KeyModifiers},
3    InnerAction,
4};
5
6use super::config::DateSelectConfig;
7
8/// Set of actions for a DateSelectPrompt.
9#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10#[allow(clippy::enum_variant_names)]
11pub enum DateSelectPromptAction {
12    /// Move day cursor to the previous day.
13    GoToPrevDay,
14    /// Move day cursor to the next day.
15    GoToNextDay,
16    /// Move day cursor to the previous week.
17    GoToPrevWeek,
18    /// Move day cursor to the next week.
19    GoToNextWeek,
20    /// Move day cursor to the previous month.
21    GoToPrevMonth,
22    /// Move day cursor to the next month.
23    GoToNextMonth,
24    /// Move day cursor to the previous year.
25    GoToPrevYear,
26    /// Move day cursor to the next year.
27    GoToNextYear,
28}
29
30impl InnerAction for DateSelectPromptAction {
31    type Config = DateSelectConfig;
32
33    fn from_key(key: Key, _: &DateSelectConfig) -> Option<Self> {
34        let action = match key {
35            Key::Left(KeyModifiers::NONE) // standard
36            | Key::Char('b', KeyModifiers::CONTROL) // emacs
37            | Key::Char('h', KeyModifiers::NONE) // vim
38            => Self::GoToPrevDay,
39
40            Key::Right(KeyModifiers::NONE) // standard
41            | Key::Char('f', KeyModifiers::CONTROL) // emacs
42            | Key::Char('l', KeyModifiers::NONE) // vim
43            => Self::GoToNextDay,
44
45            Key::Up(KeyModifiers::NONE) // standard
46            | Key::Char('p', KeyModifiers::CONTROL) // emacs
47            | Key::Char('k', KeyModifiers::NONE) // vim
48             => Self::GoToPrevWeek,
49
50            Key::Down(KeyModifiers::NONE) // standard
51            | Key::Char('n', KeyModifiers::CONTROL) // emacs
52            | Key::Char('j', KeyModifiers::NONE) // vim
53            | Key::Tab // not sure? keeping it for compatibility reasons now
54            => Self::GoToNextWeek,
55
56            Key::PageUp(KeyModifiers::NONE) // standard
57            | Key::Char('[', KeyModifiers::NONE) // alternative when page up is not available
58            | Key::Left(_) // alternative 2, when the left above with no modifiers is not matched
59            | Key::Char('v' | 'V', KeyModifiers::ALT | KeyModifiers::META) // emacs
60            | Key::Char('b' | 'B', _) // vim, ideally ctrl-b should be used, but it's not available due to emacs
61             => Self::GoToPrevMonth,
62
63            Key::PageDown(KeyModifiers::NONE) // standard
64            | Key::Char(']', KeyModifiers::NONE) // alternative when page down is not available
65            | Key::Right(_) // alternative 2, when the right above with no modifiers is not matched
66            | Key::Char('v' | 'V', KeyModifiers::CONTROL) // emacs
67            | Key::Char('f' | 'F', _) // vim, ideally ctrl-f should be used, but it's not available due to emacs
68             => Self::GoToNextMonth,
69
70            Key::PageUp(_) // standard, when the above with no modifiers is not matched
71            | Key::Char('{' | '[', _) // alternative when page up is not available
72            | Key::Up(_) // alternative 2, when the up above with no modifiers is not matched
73            => Self::GoToPrevYear,
74
75            Key::PageDown(_) // standard, when the above with no modifiers is not matched
76            | Key::Char('}' | ']', _) // alternative when page down is not available
77            | Key::Down(_) // alternative 2, when the down above with no modifiers is not matched
78            => Self::GoToNextYear,
79
80            _ => return None,
81        };
82
83        Some(action)
84    }
85}