summaryrefslogtreecommitdiffstats
path: root/src/commands/mod.rs
blob: 09fbfb77c347ad8bdb9857e666daf6f7783e7407 (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
mod change_directory;
mod cursor_move;
mod delete_files;
mod file_operations;
mod new_directory;
mod open_file;
mod parent_directory;
mod quit;
mod reload_dir;
mod rename_file;
mod search;
mod selection;
mod set_mode;
mod show_hidden;
mod tab_operations;
mod tab_switch;

pub use self::change_directory::ChangeDirectory;
pub use self::cursor_move::{
    CursorMove, CursorMoveEnd, CursorMoveHome, CursorMovePageDown, CursorMovePageUp,
};
pub use self::delete_files::DeleteFiles;
pub use self::file_operations::{CopyFiles, CutFiles, FileOperationThread, PasteFiles};
pub use self::new_directory::NewDirectory;
pub use self::open_file::{OpenFile, OpenFileWith};
pub use self::parent_directory::ParentDirectory;
pub use self::quit::ForceQuit;
pub use self::quit::Quit;
pub use self::reload_dir::ReloadDirList;
pub use self::rename_file::{RenameFile, RenameFileMethod};
pub use self::search::Search;
pub use self::selection::SelectFiles;
pub use self::set_mode::SetMode;
pub use self::show_hidden::ToggleHiddenFiles;
pub use self::tab_operations::{CloseTab, NewTab};
pub use self::tab_switch::TabSwitch;

use std::collections::HashMap;
use std::fmt;
use std::path::PathBuf;

use crate::context::JoshutoContext;
use crate::structs;

#[derive(Debug)]
pub enum CommandKeybind {
    SimpleKeybind(Box<JoshutoCommand>),
    CompositeKeybind(HashMap<i32, CommandKeybind>),
}

pub trait JoshutoRunnable {
    fn execute(&self, context: &mut JoshutoContext);
}

pub trait JoshutoCommand: JoshutoRunnable + std::fmt::Display + std::fmt::Debug {}

impl std::fmt::Display for CommandKeybind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CommandKeybind::SimpleKeybind(s) => write!(f, "{}", s),
            CommandKeybind::CompositeKeybind(_) => write!(f, "..."),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ProgressInfo {
    pub bytes_finished: u64,
    pub total_bytes: u64,
}

pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<JoshutoCommand>> {
    match command {
        "cd" => {
            if let Some(args) = args {
                match wordexp::wordexp(args[0].as_str(), 0) {
                    Ok(exp_strs) => {
                        if let Some(exp_str) = exp_strs.into_iter().next() {
                            let path = PathBuf::from(exp_str);
                            return Some(Box::new(self::ChangeDirectory::new(path)));
                        }
                    }
                    Err(_) => {
                        eprintln!("Failed to parse: {:?}", args[0]);
                    }
                }
            }
            None
        }
        "close_tab" => Some(Box::new(self::CloseTab::new())),
        "copy_files" => Some(Box::new(self::CopyFiles::new())),
        "cursor_move" => {
            if let Some(args) = args {
                if !args.is_empty() {
                    match args[0].parse::<i32>() {
                        Ok(s) => {
                            return Some(Box::new(self::CursorMove::new(s)));
                        }
                        Err(e) => {
                            eprintln!("{}", e);
                        }
                    }
                }
            }
            None
        }
        "cursor_move_home" => Some(Box::new(self::CursorMoveHome::new())),
        "cursor_move_end" => Some(Box::new(self::CursorMoveEnd::new())),
        "cursor_move_page_up" => Some(Box::new(self::CursorMovePageUp::new())),
        "cursor_move_page_down" => Some(Box::new(self::CursorMovePageDown::new())),
        "cut_files" => Some(Box::new(self::CutFiles::new())),
        "delete_files" => Some(Box::new(self::DeleteFiles::new())),
        "force_quit" => Some(Box::new(self::ForceQuit::new())),
        "mkdir" => Some(Box::new(self::NewDirectory::new())),
        "new_tab" => Some(Box::new(self::NewTab::new())),
        "open_file" => Some(Box::new(self::OpenFile::new())),
        "open_file_with" => Some(Box::new(self::OpenFileWith::new())),
        "parent_directory" => Some(Box::new(self::ParentDirectory::new())),
        "paste_files" => {
            let mut options = fs_extra::dir::CopyOptions::new();
            if let Some(args) = args {
                for arg in args {
                    let splitarg: Vec<&str> = arg.split('=').collect();
                    if splitarg.len() == 2 {
                        match splitarg[0] {
                            "overwrite" => {
                                if let Ok(s) = splitarg[1].parse::<bool>() {
                                    options.overwrite = s;
                                } else {
                                    eprintln!("Failed to parse: {}", arg);
                                }
                            }
                            "skip_exist" => {
                                if let Ok(s) = splitarg[1].parse::<bool>() {
                                    options.skip_exist = s;
                                } else {
                                    eprintln!("Failed to parse: {}", arg);
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }
            let paste = self::PasteFiles::new(options);
            Some(Box::new(paste))
        }
        "quit" => Some(Box::new(self::Quit::new())),
        "reload_dir_list" => Some(Box::new(self::ReloadDirList::new())),
        "rename_file" => {
            let method: RenameFileMethod;
            if let Some(args) = args {
                if !args.is_empty() {
                    method = match args[0].as_str() {
                        "prepend" => self::RenameFileMethod::Prepend,
                        "overwrite" => self::RenameFileMethod::Overwrite,
                        "append" => self::RenameFileMethod::Append,
                        _ => self::RenameFileMethod::Append,
                    };
                } else {
                    method = self::RenameFileMethod::Append;
                }
            } else {
                method = self::RenameFileMethod::Append;
            }
            Some(Box::new(self::RenameFile::new(method)))
        }
        "search" => Some(Box::new(self::Search::new())),
        "select_files" => {
            let mut toggle = false;
            let mut all = false;
            if let Some(args) = args {
                for arg in args {
                    let splitarg: Vec<&str> = arg.split('=').collect();
                    if splitarg.len() == 2 {
                        match splitarg[0] {
                            "toggle" => {
                                if let Ok(s) = splitarg[1].parse::<bool>() {
                                    toggle = s;
                                } else {
                                    eprintln!("Failed to parse: {}", arg);
                                }
                            }
                            "all" => {
                                if let Ok(s) = splitarg[1].parse::<bool>() {
                                    all = s;
                                } else {
                                    eprintln!("Failed to parse: {}", arg);
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }
            Some(Box::new(self::SelectFiles::new(toggle, all)))
        }
        "set_mode" => Some(Box::new(self::SetMode::new())),
        "tab_switch" => {
            if let Some(args) = args {
                if !args.is_empty() {
                    match args[0].parse::<i32>() {
                        Ok(s) => {
                            return Some(Box::new(self::TabSwitch::new(s)));
                        }
                        Err(e) => {
                            eprintln!("{}", e);
                        }
                    }
                }
            }
            None
        }
        "toggle_hidden" => Some(Box::new(self::ToggleHiddenFiles::new())),
        _ => None,
    }
}

pub fn collect_selected_paths(dirlist: &structs::JoshutoDirList) -> Option<Vec<PathBuf>></