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 ToggleDebug,
56}
57
58impl Action {
59 pub fn defaults() -> Actions {
61 [
62 (Event::Key(Key::Home), Self::SelectTop),
63 (Event::Key(Key::Up), Self::SelectUp),
64 (Event::Key(Key::PageUp), Self::SelectUpPage),
65 (Event::Key(Key::End), Self::SelectBottom),
66 (Event::Key(Key::Down), Self::SelectDown),
67 (Event::Key(Key::PageDown), Self::SelectDownPage),
68 (Event::Key(Key::Right), Self::Expand),
69 (Event::Char('+'), Self::ExpandRecursive),
70 (Event::Char('>'), Self::ExpandAll),
71 (Event::Key(Key::Left), Self::Collapse),
72 (Event::Char('-'), Self::CollapseRecursive),
73 (Event::Char('<'), Self::CollapseAll),
74 (Event::Key(Key::Enter), Self::ToggleState),
75 ]
76 .into()
77 }
78
79 pub fn remove(&self, actions: &mut Actions) -> bool {
83 let events = self.events(actions);
84 for event in &events {
85 actions.remove(event);
86 }
87 !events.is_empty()
88 }
89
90 pub fn events(&self, actions: &Actions) -> Vec<Event> {
92 actions
93 .into_iter()
94 .filter_map(|(event, action)| if action == self { Some(event.clone()) } else { None })
95 .collect()
96 }
97}