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
|
mod search;
mod state;
use colored::*;
use search::{ResultsOfSearch, SearchWorker};
use serde_json;
use state::{refresh_directory, FsEntry, State, CURRENT_SEARCH_TERM};
use std::{cmp::min, time::Instant};
use zellij_tile::prelude::*;
register_plugin!(State);
register_worker!(SearchWorker, search_worker);
impl ZellijPlugin for State {
fn load(&mut self) {
refresh_directory(self);
self.loading = true;
subscribe(&[
EventType::Key,
EventType::Mouse,
EventType::CustomMessage,
EventType::Timer,
]);
post_message_to("search", String::from("scan_folder"), String::new());
set_timeout(0.5); // for displaying loading animation
}
fn update(&mut self, event: Event) -> bool {
let mut should_render = false;
let prev_event = if self.ev_history.len() == 2 {
self.ev_history.pop_front()
} else {
None
};
self.ev_history.push_back((event.clone(), Instant::now()));
match event {
Event::Timer(_elapsed) => {
should_render = true;
if self.loading {
set_timeout(0.5);
if self.loading_animation_offset == u8::MAX {
self.loading_animation_offset = 0;
} else {
self.loading_animation_offset =
self.loading_animation_offset.saturating_add(1);
}
}
},
Event::CustomMessage(message, payload) => match message.as_str() {
"update_search_results" => {
if let Ok(mut results_of_search) =
serde_json::from_str::<ResultsOfSearch>(&payload)
{
if Some(results_of_search.search_term) == self.search_term {
self.search_results =
results_of_search.search_results.drain(..).collect();
should_render = true;
}
}
},
"done_scanning_folder" => {
self.loading = false;
should_render = true;
},
_ => {},
},
Event::Key(key) => match key {
// modes:
// 1. typing_search_term
// 2. exploring_search_results
// 3. normal
Key::Esc | Key::Char('\n') if self.typing_search_term() => {
self.accept_search_term();
},
_ if self.typing_search_term() => {
self.append_to_search_term(key);
if let Some(search_term) = self.search_term.as_ref() {
std::fs::write(CURRENT_SEARCH_TERM, search_term.as_bytes()).unwrap();
post_message_to(
"search",
String::from("search"),
String::from(&self.search_term.clone().unwrap()),
);
}
should_render = true;
},
Key::Esc if self.exploring_search_results() => {
self.stop_exploring_search_results();
should_render = true;
},
Key::Char('/') => {
self.start_typing_search_term();
should_render = true;
},
Key::Esc => {
self.stop_typing_search_term();
should_render = true;
},
Key::Up | Key::Char('k') => {
if self.exploring_search_results() {
self.move_search_selection_up();
should_render = true;
} else {
let currently_selected = self.selected();
*self.selected_mut() = self.selected().saturating_sub(1);
if currently_selected != self.selected() {
should_render = true;
}
}
},
Key::Down | Key::Char('j') => {
if self.exploring_search_results() {
self.move_search_selection_down();
should_render = true;
} else {
let currently_selected = self.selected();
let next = self.selected().saturating_add(1);
*self.selected_mut() = min(self.files.len().saturating_sub(1), next);
if currently_selected != self.selected() {
should_render = true;
}
}
},
Key::Right | Key::Char('\n') | Key::Char('l') if !self.files.is_empty() => {
if self.exploring_search_results() {
self.open_search_result();
} else {
self.traverse_dir_or_open_file();
self.ev_history.clear();
}
should_render = true;
},
Key::Left | Key::Char('h') => {
if self.path.components().count() > 2 {
// don't descend into /host
// the reason this is a hard-coded number (2) and not "== ROOT"
// or some such is that there are certain cases in which self.path
// is empty and this will work then too
should_render = true;
self.path.pop();
refresh_directory(self);
}
},
Key::Char('.') => {
should_render = true;
self.toggle_hidden_files();
refresh_directory(self);
},
_ => (),
},
Event::Mouse(mouse_event) => match mouse_event {
Mouse::ScrollDown(_) => {
let currently_selected = self.selected();
let next = self.selected().saturating_add(1);
*self.selected_mut() = min(self.files.len().saturating_sub(1), next);
if currently_selected != self.selected() {
should_render = true;
|