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
use crate::conf::Conf;
use crate::permissions;
use crate::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 key
/// - if only one verb key 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, key: &str, shortcut: Option<String>, description: &str) {
self.verbs
.push(Verb::create_builtin(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,
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,
"revert to the previous state (mapped to `<esc>`)",
);
self.verbs.push(
Verb::create_external(
"cd",
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,
"/bin/cp -r {file} {parent}/{newpath}".to_string(),
None,
false,
false,
false,
)
.unwrap(),
);
self.add_builtin(
"focus",
Some("goto".to_string()),
"display the directory (mapped to `<enter>` in tree)",
);
self.add_builtin("help", Some("?".to_string()), "display broot's help");
self.verbs.push(
Verb::create_external(
"mkdir {subpath}",
Some("md".to_string()),
"/bin/mkdir -p {directory}/{subpath}".to_string(),
None,
false,
false, // doesn't leave broot
false,
)
.unwrap(),
);
self.verbs.push(
Verb::create_external(
"mv {newpath}",
None,
"/bin/mv {file} {parent}/{newpath}".to_string(),
None,
false,
false, // doesn't leave broot
false,
)
.unwrap(),
);
self.add_builtin(
"open",
None,
"open file according to OS settings (mapped to `<enter>`)",
);
self.add_builtin("parent", None, "move to the parent directory");
self.add_builtin(
"print_path",
Some("pp".to_string()),
"print path and leaves broot",
);
self.add_builtin(
"print_tree",
Some("pt".to_string()),
"print tree and leaves broot",
);
self.add_builtin("quit", Some("q".to_string()), "quit the application");
self.verbs.push(
Verb::create_external(
"rm",
None,
"/bin/rm -rf {file}".to_string(),
None,
false,
false, // doesn't leave broot
false,
)
.unwrap(),
);
self.add_builtin(
"toggle_files",
Some("files".to_string()),
"toggle showing files (or just folders)",
);
self.add_builtin(
"toggle_git_ignore",
Some("gi".to_string()),
"toggle use of .gitignore",
);
self.add_builtin(
"toggle_hidden",
Some("h".to_string()),
"toggle showing hidden files",
);
if permissions::supported() {
self.add_builtin(
"toggle_perm",
Some("perm".to_string()),
"toggle showing file permissions",
);
}
self.add_builtin(
"toggle_sizes",
Some("sizes".to_string()),
"toggle showing sizes",
);
self.add_builtin(
"toggle_trim_root",
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 key. 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, key: &str) -> usize {
for i in 0..self.verbs.len() {
if self.verbs[i].invocation.key == key {
return i;
}
}
panic!("invalid verb search");
}
}