1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::zeuslib::events::loopaction::EventLoopAction;
use crate::zeuslib::input::KeySequence;
use crate::zeuslib::state::State;
use std::collections::HashMap;
use termion::event::Key;

pub struct Config {
    pub key_map: HashMap<KeySequence, Box<dyn Fn(&mut State) -> EventLoopAction>>,
}

fn quit_action(_state: &mut State) -> EventLoopAction {
    EventLoopAction::QuitLoop
}

fn next_panel_action(state: &mut State) -> EventLoopAction {
    state.next_panel();
    state.refresh();
    EventLoopAction::ContinueLoop
}

fn move_down_action(state: &mut State) -> EventLoopAction {
    let panel = state.get_current_panel_mut();
    if let Some(p) = panel {
        p.next();
    }
    EventLoopAction::ContinueLoop
}

fn move_up_action(state: &mut State) -> EventLoopAction {
    let panel = state.get_current_panel_mut();
    if let Some(p) = panel {
        p.previous();
    }
    EventLoopAction::ContinueLoop
}

fn mark_action(state: &mut State) -> EventLoopAction {
    let selected = { state.selected() };
    let panel = state.get_current_panel_mut();
    if let Some(i) = selected {
        if let Some(p) = panel {
            let items = &mut p.items;
            items[i].marked = !items[i].marked;
            p.next();
        }
    }
    EventLoopAction::ContinueLoop
}

impl Config {
    pub fn default() -> Self {
        let mut config = Self {
            key_map: HashMap::new(),
        };
        config.map_key(KeySequence::from_keys(&[Key::Char('q')]), quit_action);
        config.map_key(KeySequence::from_keys(&[Key::Char('j')]), move_down_action);
        config.map_key(KeySequence::from_keys(&[Key::Char('k')]), move_up_action);
        config.map_key(KeySequence::from_keys(&[Key::Char(' ')]), mark_action);
        config.map_key(
            KeySequence::from_keys(&[Key::Char('\t')]),
            next_panel_action,
        );
        config
    }
    pub fn map_key<F>(&mut self, k: KeySequence, f: F)
    where
        F: Fn(&mut State) -> EventLoopAction + 'static,
    {
        self.key_map.insert(k, Box::new(f));
    }
}