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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
use crossterm::input::KeyEvent;
use crate::{conf::Conf, permissions, verbs::Verb};
/// Provide access to the verbs:
/// - the built-in ones
/// - the user defined ones
/// A user defined verb can replace a built-in.
/// When the user types some keys, we select a verb
/// - if the input exactly matches a shortcut or the name
/// - if only one verb name starts with the input
pub struct VerbStore {
pub verbs: Vec<Verb>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PrefixSearchResult<T> {
NoMatch,
Match(T),
TooManyMatches,
}
impl VerbStore {
pub fn new() -> VerbStore {
VerbStore { verbs: Vec::new() }
}
fn add_builtin(
&mut self,
name: &str,
key: Option<KeyEvent>,
shortcut: Option<String>,
description: &str,
) {
self.verbs
.push(Verb::create_builtin(name, key, shortcut, description));
}
pub fn init(&mut self, conf: &Conf) {
// we first add the verbs coming from configuration, as
// we'll search in order. This way, a user can overload a
// standard verb.
for verb_conf in &conf.verbs {
match Verb::create_external(
&verb_conf.invocation,
// TODO remove the clone in the following line when crossterm's KeyEvent is Copy
verb_conf.key.clone(),
verb_conf.shortcut.clone(),
verb_conf.execution.clone(),
verb_conf.description.clone(),
verb_conf.from_shell.unwrap_or(false),
verb_conf.leave_broot.unwrap_or(true),
verb_conf.confirm.unwrap_or(false),
) {
Ok(v) => {
self.verbs.push(v);
}
Err(e) => {
eprintln!("Verb error: {:?}", e);
}
}
}
self.add_builtin(
"back",
None, // esc is mapped in commands.rs
None,
"revert to the previous state (mapped to `<esc>`)",
);
self.verbs.push(
Verb::create_external(
"cd",
None,
None, // no real need for a shortcut as it's mapped to alt-enter
"cd {directory}".to_string(),
Some("change directory and quit (mapped to `<alt><enter>`)".to_string()),
true, // needs to be launched from the parent shell
true, // leaves broot
false,
)
.unwrap(),
);
self.verbs.push(
Verb::create_external(
"cp {newpath}",
None,
None,
"/bin/cp -r {file} {newpath:path-from-parent}".to_string(),
None,
false,
false,
false,
)
.unwrap(),
);
self.add_builtin(
"focus",
None, // enter
Some("goto".to_string()),
"display the directory (mapped to `<enter>` in tree)",
);
self.add_builtin(
"help",
Some(KeyEvent::F(1)), // note: some terminals intercept the F1 key
Some("?".to_string()),
"display broot's help",
);
self.add_builtin(
"line_down",
Some(KeyEvent::Down),
None,
"move one line down",
);
self.add_builtin("line_up", Some(KeyEvent::Up), None, "move one line up");
self.verbs.push(
Verb::create_external(
"mkdir {subpath}",
None,
Some("md".to_string()),
"/bin/mkdir -p {subpath:path-from-directory}".to_string(),
None,
false,
false, // doesn't leave broot
false,
)
.unwrap(),
);
self.verbs.push(
Verb::create_external(
"mv {newpath}",
None,
None,
"/bin/mv {file} {newpath:path-from-parent}".to_string(),
None,
false,
false, // doesn't leave broot
false,
)
.unwrap(),
);
self.add_builtin(
"open_stay",
None, // default mapping directly handled in commands#add_event
None,
"open file or directory according to OS settings (stays in broot)",
);
self.add_builtin(
"open_leave",
None, // default mapping directly handled in commands#add_event
None,
"open file or directory according to OS settings (quit broot)",
);
self.add_builtin(
"page_down",
Some(KeyEvent::PageDown),
None,
"scroll one page down",
);
self.add_builtin(
"page_up",
Some(KeyEvent::PageUp),
None,
"scroll one page up",
);
self.add_builtin(
"parent",
None,
Some("p".to_string()),
"move to the parent directory",
);
self.add_builtin(
"print_path",
None,
Some("pp".to_string()),
"print path and leaves broot",
);
self.add_builtin(
"print_tree",
None,
Some("pt".to_string()),
"print tree and leaves broot",
);
self.add_builtin(
"quit",
Some(KeyEvent::Ctrl('q')),
Some("q".to_string()),
"quit the application",
);
self.add_builtin(
"refresh",
Some(KeyEvent::F(5)),
None,
"refresh tree and clear size cache",
);
self.verbs.push(
Verb::create_external(
"rm",
None, // the delete key is used in the input
None,
"/bin/rm -rf {file}".to_string(),
None,
false,
false, // doesn't leave broot
false,
)
.unwrap(),
);
self.add_builtin(
"toggle_dates",
None,
Some("dates".to_string()),
"toggle showing last modified dates",
);
self.add_builtin(
"toggle_files",
None,
Some("files".to_string()),
"toggle showing files (or just folders)",
);
self.add_builtin(
"toggle_git_ignore",
None,
Some("gi".to_string()),
"toggle use of .gitignore",
);
self.add_builtin(
"toggle_hidden",
None,
Some("h".to_string()),
"toggle showing hidden files",
);
if permissions::supported() {
self.add_builtin(
"toggle_perm",
None,
Some("perm".to_string()),
"toggle showing file permissions",
);
}
self.add_builtin(
"toggle_sizes",
None,
Some("sizes".to_string()),
"toggle showing sizes",
);
self.add_builtin(
"toggle_trim_root",
None,
Some("t".to_string()),
"toggle removing nodes at first level too (default)",
);
}
pub fn search(&self, prefix: &str) -> PrefixSearchResult<&Verb> {
let mut found_index = 0;
let mut nb_found = 0;
for (index, verb) in self.verbs.iter().enumerate() {
if let Some(shortcut) = &verb.shortcut {
if shortcut.starts_with(prefix) {
if shortcut == prefix {
return PrefixSearchResult::Match(&verb);
}
found_index = index;
nb_found += 1;
continue;
}
}
if verb.invocation.key.starts_with(prefix) {
if verb.invocation.key == prefix {
return PrefixSearchResult::Match(&verb);
}
found_index = index;
nb_found += 1;
}
}
match nb_found {
0 => PrefixSearchResult::NoMatch,
1 => PrefixSearchResult::Match(&self.verbs[found_index]),
_ => PrefixSearchResult::TooManyMatches,
}
}
/// return the index of the verb having the long name. This function is meant
/// for internal access when it's sure it can't failed (i.e. for a builtin)
/// It looks for verbs by key, starting from the builtins, to
/// ensure it hasn't been overriden.
pub fn index_of(&self, name: &str) -> usize {
for i in 0..self.verbs.len() {
if self.verbs[i].invocation.key == name {
return i;
}
}
panic!("invalid verb search");
}
/// return the index of the verb which is triggered by the given key, if any
pub fn index_of_key(&self, key: KeyEvent) -> Option<usize> {
for i in 0..self.verbs.len() {
// TODO remove the clone in the following line when crossterm's KeyEvent is Copy
if let Some(verb_key) = self.verbs[i].key.clone() {
if verb_key == key {
return Some(i);
}
}
}
None
}
}
|