summaryrefslogtreecommitdiffstats
path: root/src/joshuto/command.rs
blob: 35a199de1a2a6d254a8c72beb6de7e068bd26ad6 (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
extern crate fs_extra;
extern crate ncurses;

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

use joshuto;

mod quit;
pub use self::quit::Quit;

mod parent_directory;
pub use self::parent_directory::ParentDirectory;

mod open_file;
pub use self::open_file::OpenFile;
pub use self::open_file::OpenFileWith;

mod change_directory;
pub use self::change_directory::ChangeDirectory;

mod cursor_move;
pub use self::cursor_move::CursorMove;
pub use self::cursor_move::CursorMovePageUp;
pub use self::cursor_move::CursorMovePageDown;
pub use self::cursor_move::CursorMoveHome;
pub use self::cursor_move::CursorMoveEnd;

mod file_operation;
pub use self::file_operation::ProgressInfo;
pub use self::file_operation::CutFiles;
pub use self::file_operation::CopyFiles;
pub use self::file_operation::PasteFiles;
pub use self::file_operation::DeleteFiles;
pub use self::file_operation::RenameFile;
pub use self::file_operation::RenameFileMethod;

mod new_directory;
pub use self::new_directory::NewDirectory;

mod show_hidden;
pub use self::show_hidden::ToggleHiddenFiles;

mod selection;
pub use self::selection::SelectFiles;

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

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

pub trait JoshutoCommand: Runnable + 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, "..."),
        }
    }
}

pub fn split_shell_style(line: &String) -> Vec<&str>
{
    let mut args: Vec<&str> = Vec::new();
    let mut char_ind = line.char_indices();

    while let Some((i, ch)) = char_ind.next() {
        if ch.is_whitespace() {
            continue;
        }
        if ch == '\'' {
            while let Some((j, ch)) = char_ind.next() {
                if ch == '\'' {
                    args.push(&line[i+1..j]);
                    break;
                }
            }
        } else if ch == '"'{
            while let Some((j, ch)) = char_ind.next() {
                if ch == '"' {
                    args.push(&line[i+1..j]);
                    break;
                }
            }
        } else {
            while let Some((j, ch)) = char_ind.next() {
                if ch.is_whitespace() {
                    args.push(&line[i..j]);
                    break;
                }
            }
        }
    }
    args
}


pub fn from_args(args: &[&str]) -> Option<Box<dyn JoshutoCommand>>
{
    let args_len = args.len();

    if args_len == 0 {
        return None;
    }

    match args[0] {
        "quit" => Some(Box::new(self::Quit::new())),
        "parent_directory" => Some(Box::new(self::ParentDirectory::new())),

        "open_file" => Some(Box::new(self::OpenFile::new())),
        "open_file_with" => Some(Box::new(self::OpenFileWith::new())),
        "change_directory" => {
            if args_len > 1 {
                let path = path::PathBuf::from(args[1]);
                Some(Box::new(self::ChangeDirectory::new(path)))
            } else {
                None
            }
        },

        "cursor_move" => {
            if args_len > 1 {
                match args[1].parse::<i32>() {
                    Ok(s) => {
                        Some(Box::new(self::CursorMove::new(s)))
                    },
                    Err(e) => {
                        eprintln!("{}", e);
                        None
                    },
                }
            } else {
                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())),

        "toggle_hidden" => Some(Box::new(self::ToggleHiddenFiles::new())),

        "cut_files" => Some(Box::new(self::CutFiles::new())),
        "copy_files" => Some(Box::new(self::CopyFiles::new())),
        "paste_files" => {
            let mut options = fs_extra::dir::CopyOptions::new();
            for arg in &args[1..] {
                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))
        }
        "delete_files" => Some(Box::new(self::DeleteFiles::new())),
        "rename_file" => {
            let method: self::file_operation::RenameFileMethod;
            if args_len == 1 {
                method = self::RenameFileMethod::Append;
            } else {
                method = match args[1] {
                    "prepend" => self::RenameFileMethod::Prepend,
                    "overwrite" => self::RenameFileMethod::Overwrite,
                    "append" => self::RenameFileMethod::Append,
                    _ => self::RenameFileMethod::Append,
                }
            }
            Some(Box::new(self::RenameFile::new(method)))
        }
        "new_directory" => Some(Box::new(self::NewDirectory::new())),
        "select_files" => {
            let mut toggle = false;
            let mut all = false;
            for arg in &args[1..] {
                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)))
        }

        _ => None,
    }
}