summaryrefslogtreecommitdiffstats
path: root/src/config/keymap.rs
blob: 6bf915c8d39c255cdb89f8baff7219481fa19000 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::collections::{hash_map::Entry, HashMap};

use serde_derive::Deserialize;

use termion::event::Key;

use super::{parse_to_config_file, ConfigStructure, Flattenable};
use crate::commands::{self, CommandKeybind, JoshutoCommand};
use crate::util::key_mapping::str_to_key;
use crate::KEYMAP_FILE;

pub type JoshutoCommandMapping = HashMap<Key, CommandKeybind>;

#[derive(Debug, Deserialize)]
struct JoshutoMapCommand {
    pub command: String,
    #[serde(default)]
    pub args: Vec<String>,
    pub keys: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct JoshutoRawCommandMapping {
    #[serde(default)]
    mapcommand: Vec<JoshutoMapCommand>,
}

impl Flattenable<JoshutoCommandMapping> for JoshutoRawCommandMapping {
    fn flatten(self) -> JoshutoCommandMapping {
        let mut keymaps = JoshutoCommandMapping::new();
        for m in self.mapcommand {
            match commands::from_args(m.command, m.args) {
                Ok(command) => {
                    let keycodes: Vec<&str> = m.keys.iter().map(|s| s.as_str()).collect();

                    let result = insert_keycommand(&mut keymaps, command, &keycodes);
                    match result {
                        Ok(_) => {}
                        Err(e) => eprintln!("{}", e),
                    }
                }
                Err(e) => eprintln!("{}", e.cause()),
            }
        }
        keymaps
    }
}

impl ConfigStructure for JoshutoCommandMapping {
    fn get_config() -> Self {
        parse_to_config_file::<JoshutoRawCommandMapping, JoshutoCommandMapping>(KEYMAP_FILE)
            .unwrap_or_else(Self::default)
    }
}

fn insert_keycommand(
    keymap: &mut JoshutoCommandMapping,
    keycommand: Box<dyn JoshutoCommand>,
    keycodes: &[&str],
) -> Result<(), String> {
    let keycode_len = keycodes.len();

    if keycode_len == 0 {
        return Ok(());
    }

    let key = match str_to_key(keycodes[0]) {
        Some(k) => k,
        None => return Err(format!("Unknown keycode: {}", keycodes[0])),
    };

    if keycode_len == 1 {
        match keymap.entry(key) {
            Entry::Occupied(_) => {
                return Err(format!("Error: Keybindings ambiguous for {}", keycommand))
            }
            Entry::Vacant(entry) => entry.insert(CommandKeybind::SimpleKeybind(keycommand)),
        };
        return Ok(());
    }

    match keymap.entry(key) {
        Entry::Occupied(mut entry) => match entry.get_mut() {
            CommandKeybind::CompositeKeybind(ref mut m) => {
                insert_keycommand(m, keycommand, &keycodes[1..])
            }
            _ => Err(format!("Error: Keybindings ambiguous for {}", keycommand)),
        },
        Entry::Vacant(entry) => {
            let mut new_map = JoshutoCommandMapping::new();
            let result = insert_keycommand(&mut new_map, keycommand, &keycodes[1..]);
            if result.is_ok() {
                let composite_command = CommandKeybind::CompositeKeybind(new_map);
                entry.insert(composite_command);
            }
            result
        }
    }
}