summaryrefslogtreecommitdiffstats
path: root/src/joshuto/keymap.rs
blob: 1b90f24b27dc811291eb65caac46537eb2e931d4 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
extern crate fs_extra;
extern crate xdg;

use std::collections::HashMap;
use std::fs;
use std::io;
use std::io::BufRead;
use std::process;

use joshuto::command;
use joshuto::command::*;
use joshuto::keymapll::Keycode;

const MAP_COMMAND: &str = "map";
// const ALIAS_COMMAND: &str = "alias";
// const NEWTAB_COMMAND: &str = "newtab";

const COMMENT_DELIMITER: char = '#';

/* #define KEY_ALT(x) KEY_F(60) + (x - 'A') */

#[derive(Debug)]
pub struct JoshutoKeymap {
    pub keymaps: HashMap<i32, CommandKeybind>,
}

impl JoshutoKeymap {
    pub fn new() -> Self
    {
        let mut keymaps: HashMap<i32, CommandKeybind> = HashMap::new();

        // quit
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::Quit::new()));
        keymaps.insert(Keycode::LOWER_Q as i32, command);

        // up
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMove::new(-1)));
        keymaps.insert(Keycode::UP as i32, command);
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMove::new(-1)));
        keymaps.insert(Keycode::LOWER_K as i32, command);

        // down
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMove::new(1)));
        keymaps.insert(Keycode::DOWN as i32, command);
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMove::new(1)));
        keymaps.insert(Keycode::LOWER_J as i32, command);

        // left
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::ParentDirectory::new()));
        keymaps.insert(Keycode::LEFT as i32, command);
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::ParentDirectory::new()));
        keymaps.insert(Keycode::LOWER_H as i32, command);

        // right
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::OpenFile::new()));
        keymaps.insert(Keycode::RIGHT as i32, command);
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::OpenFile::new()));
        keymaps.insert(Keycode::LOWER_L as i32, command);
        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::OpenFile::new()));
        keymaps.insert(Keycode::ENTER as i32, command);

        let command = CommandKeybind::SimpleKeybind(
            Box::new(command::OpenFileWith::new()));
        keymaps.insert(Keycode::LOWER_R as i32, command);

        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMovePageUp::new()));
        keymaps.insert(Keycode::PPAGE as i32, command);

        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMovePageDown::new()));
        keymaps.insert(Keycode::NPAGE as i32, command);

        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMoveHome::new()));
        keymaps.insert(Keycode::HOME as i32, command);

        let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CursorMoveEnd::new()));
        keymaps.insert(Keycode::END as i32, command);

        let command = CommandKeybind::SimpleKeybind(
            Box::new(command::DeleteFiles::new()));
        keymaps.insert(Keycode::DELETE as i32, command);

        let command = CommandKeybind::SimpleKeybind(
            Box::new(command::RenameFile::new(command::RenameFileMethod::Append)));
        keymaps.insert(Keycode::LOWER_A as i32, command);

        {
            let mut subkeymap: HashMap<i32, CommandKeybind> = HashMap::new();
            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::ToggleHiddenFiles::new()));
            subkeymap.insert(Keycode::LOWER_H as i32, command);

            let command = CommandKeybind::CompositeKeybind(subkeymap);
            keymaps.insert(Keycode::LOWER_Z as i32, command);
        }

        {
            let mut subkeymap: HashMap<i32, CommandKeybind> = HashMap::new();
            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CutFiles::new()));
            subkeymap.insert(Keycode::LOWER_D as i32, command);

            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::DeleteFiles::new()));
            subkeymap.insert(Keycode::UPPER_D as i32, command);

            let command = CommandKeybind::CompositeKeybind(subkeymap);
            keymaps.insert(Keycode::LOWER_D as i32, command);
        }

        {
            let mut subkeymap: HashMap<i32, CommandKeybind> = HashMap::new();

            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::CopyFiles::new()));
            subkeymap.insert(Keycode::LOWER_Y as i32, command);

            let command = CommandKeybind::CompositeKeybind(subkeymap);
            keymaps.insert(Keycode::LOWER_Y as i32, command);
        }

        {
            let mut subkeymap: HashMap<i32, CommandKeybind> = HashMap::new();

            let options = fs_extra::dir::CopyOptions::new();
            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::PasteFiles::new(options)));
            subkeymap.insert(Keycode::LOWER_P as i32, command);

            let mut options = fs_extra::dir::CopyOptions::new();
            options.overwrite = true;
            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::PasteFiles::new(options)));
            subkeymap.insert(Keycode::LOWER_O as i32, command);

            let command = CommandKeybind::CompositeKeybind(subkeymap);
            keymaps.insert(Keycode::LOWER_P as i32, command);
        }

        {
            let mut subkeymap: HashMap<i32, CommandKeybind> = HashMap::new();
            let command = CommandKeybind::SimpleKeybind(
                Box::new(command::NewDirectory::new()));
            subkeymap.insert(Keycode::LOWER_K as i32, command);

            let command = CommandKeybind::CompositeKeybind(subkeymap);
            keymaps.insert(Keycode::LOWER_M as i32, command);
        }

        JoshutoKeymap {
            keymaps,
        }
    }

    fn insert_keycommand(map: &mut HashMap<i32, CommandKeybind>,
            keycommand: Box<dyn JoshutoCommand>, keys: &[&str])
    {
        if keys.len() == 1 {
            match Keycode::from_str(keys[0]) {
                Some(s) => {
                    map.insert(s as i32, CommandKeybind::SimpleKeybind(keycommand));
                },
                None => {}
            }
        } else {
            match Keycode::from_str(keys[0]) {
                Some(s) => {
                    let mut new_map: HashMap<i32, CommandKeybind>;
                    match map.remove(&(s.clone() as i32)) {
                        Some(CommandKeybind::CompositeKeybind(mut m)) => {
                            new_map = m;
                        },
                        Some(_) => {
                            eprintln!("Error: Keybindings ambiguous");
                            process::exit(1);
                        },
                        None => {
                            new_map = HashMap::new();
                        }
                    }
                    JoshutoKeymap::insert_keycommand(&mut new_map, keycommand, &keys[1..]);
                    let composite_command = CommandKeybind::CompositeKeybind(new_map);
                    map.insert(s as i32, composite_command);
                },
                None => {}
            }
        }
    }

    fn parse_line(map: &mut HashMap<i32, CommandKeybind>, line: String)
    {
        let mut line = line;
        {
            if let Some(trunc_index) = line.find(COMMENT_DELIMITER) {
                line.truncate(trunc_index as usize);
            }
        }
        if line.len() == 0 {
            return;
        }
        line.push('\n');

        let args: Vec<&str> = command::split_shell_style(&line);

        if args.len() == 0 {
            return;
        }

        match args[0] {
            MAP_COMMAND => {
                let keys_vec: Vec<&str> =