cursive_tree/view/
actions.rs1use {cursive::event::*, std::collections::*};
2
3pub type Actions = HashMap<Event, Action>;
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum Action {
13 SelectTop,
15
16 SelectUp,
18
19 SelectUpPage,
21
22 SelectBottom,
24
25 SelectDown,
27
28 SelectDownPage,
30
31 Expand,
33
34 ExpandRecursive,
36
37 ExpandAll,
39
40 Collapse,
42
43 CollapseRecursive,
45
46 CollapseAll,
48
49 ToggleState,
51}
52
53impl Action {
54 pub fn defaults() -> Actions {
56 [
57 (Event::Key(Key::Home), Self::SelectTop),
58 (Event::Key(Key::Up), Self::SelectUp),
59 (Event::Key(Key::PageUp), Self::SelectUpPage),
60 (Event::Key(Key::End), Self::SelectBottom),
61 (Event::Key(Key::Down), Self::SelectDown),
62 (Event::Key(Key::PageDown), Self::SelectDownPage),
63 (Event::Key(Key::Right), Self::Expand),
64 (Event::Char('+'), Self::ExpandRecursive),
65 (Event::Char('>'), Self::ExpandAll),
66 (Event::Key(Key::Left), Self::Collapse),
67 (Event::Char('-'), Self::CollapseRecursive),
68 (Event::Char('<'), Self::CollapseAll),
69 (Event::Key(Key::Enter), Self::ToggleState),
70 ]
71 .into()
72 }
73
74 pub fn remove(&self, actions: &mut Actions) -> bool {
78 let events = self.events(actions);
79 for event in &events {
80 actions.remove(event);
81 }
82 !events.is_empty()
83 }
84
85 pub fn events(&self, actions: &Actions) -> Vec<Event> {
87 actions
88 .into_iter()
89 .filter_map(|(event, action)| if action == self { Some(event.clone()) } else { None })
90 .collect()
91 }
92}