aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/strider/src/state.rs
blob: a2c03a76b28a281ae3630cc7987aacd1b4d941f3 (plain) (blame)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use crate::file_list_view::{FileListView, FsEntry};
use crate::platform::Platform;
use crate::search_view::SearchView;
use crate::shared::{calculate_list_bounds, render_list_tip};
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
};
use unicode_width::UnicodeWidthStr;
use zellij_tile::prelude::*;

#[derive(Default)]
pub struct State {
    pub file_list_view: FileListView,
    pub search_view: SearchView,
    pub hide_hidden_files: bool,
    pub current_rows: Option<usize>,
    pub handling_filepick_request_from: Option<(PipeSource, BTreeMap<String, String>)>,
    pub initial_cwd: PathBuf,
    pub is_searching: bool,
    pub search_term: String,
    pub close_on_selection: bool,
    pub platform: Platform,
    pub is_in_virtual_root: bool,
    pub virtual_root_entries: Vec<FsEntry>,
    pub virtual_root_selected: usize,
}

impl State {
    pub fn update_search_term(&mut self, character: char) {
        if self.is_in_virtual_root {
            if character == '/' {
                self.descend_to_root_path();
            }
            return;
        }
        self.search_term.push(character);
        if &self.search_term == ".." {
            self.descend_to_previous_path();
        } else if &self.search_term == "/" {
            self.descend_to_root_path();
        } else {
            self.is_searching = true;
            self.search_view
                .update_search_results(&self.search_term, &self.file_list_view.files);
        }
    }
    pub fn handle_backspace(&mut self) {
        if self.is_in_virtual_root {
            return;
        }
        if self.search_term.is_empty() {
            self.descend_to_previous_path();
        } else {
            self.search_term.pop();
            if self.search_term.is_empty() {
                self.is_searching = false;
            }
            self.search_view
                .update_search_results(&self.search_term, &self.file_list_view.files);
        }
    }
    pub fn clear_search_term(&mut self) {
        self.search_term.clear();
        self.search_view
            .update_search_results(&self.search_term, &self.file_list_view.files);
        self.is_searching = false;
    }
    pub fn clear_search_term_or_descend(&mut self) {
        if self.is_in_virtual_root {
            return;
        }
        if self.search_term.is_empty() {
            self.descend_to_previous_path();
        } else {
            self.search_term.clear();
            self.search_view
                .update_search_results(&self.search_term, &self.file_list_view.files);
            self.is_searching = false;
        }
    }
    pub fn move_selection_up(&mut self) {
        if self.is_in_virtual_root {
            self.virtual_root_selected = self.virtual_root_selected.saturating_sub(1);
        } else if self.is_searching {
            self.search_view.move_selection_up();
        } else {
            self.file_list_view.move_selection_up();
        }
    }
    pub fn move_selection_down(&mut self) {
        if self.is_in_virtual_root {
            if !self.virtual_root_entries.is_empty() {
                self.virtual_root_selected = std::cmp::min(
                    self.virtual_root_selected.saturating_add(1),
                    self.virtual_root_entries.len().saturating_sub(1),
                );
            }
        } else if self.is_searching {
            self.search_view.move_selection_down();
        } else {
            self.file_list_view.move_selection_down();
        }
    }
    pub fn handle_left_click(&mut self, line: isize) {
        if let Some(current_rows) = self.current_rows {
            let rows_for_list = current_rows.saturating_sub(5);
            if self.is_in_virtual_root {
                let (start_index, _selected_index_in_range, _end_index) = calculate_list_bounds(
                    self.virtual_root_entries.len(),
                    rows_for_list,
                    Some(self.virtual_root_selected),
                );
                let prev_selected = self.virtual_root_selected;
                self.virtual_root_selected = (line as usize).saturating_sub(4) + start_index;
                if prev_selected == self.virtual_root_selected {
                    self.traverse_dir();
                }
            } else if self.is_searching {
                let (start_index, _selected_index_in_range, _end_index) = calculate_list_bounds(
                    self.search_view.search_result_count(),
                    rows_for_list,
                    Some(self.search_view.selected_search_result),
                );
                let prev_selected = self.search_view.selected_search_result;
                self.search_view.selected_search_result =
                    (line as usize).saturating_sub(4) + start_index;
                if prev_selected == self.search_view.selected_search_result {
                    self.traverse_dir();
                }
            } else {
                let (start_index, _selected_index_in_range, _end_index) = calculate_list_bounds(
                    self.file_list_view.files.len(),
                    rows_for_list,
                    self.file_list_view.selected(),
                );
                let prev_selected = self.file_list_view.selected();
                *self.file_list_view.selected_mut() =
                    (line as usize).saturating_sub(4) + start_index;
                if prev_selected == self.file_list_view.selected() {
                    self.traverse_dir();
                }
            }
        }
    }
    pub fn handle_mouse_hover(&mut self, line: isize) {
        if let Some(current_rows) = self.current_rows {
            let rows_for_list = current_rows.saturating_sub(5);
            if self.is_in_virtual_root {
                let (start_index, _selected_index_in_range, _end_index) = calculate_list_bounds(
                    self.virtual_root_entries.len(),
                    rows_for_list,
                    Some(self.virtual_root_selected),
                );
                self.virtual_root_selected = (line as usize).saturating_sub(4) + start_index;
            } else if self.is_searching {
                let (start_index, _selected_index_in_range, _end_index) = calculate_list_bounds(
                    self.search_view.search_result_count(),
                    rows_for_list,
                    Some(self.search_view.selected_search_result),
                );
                self.search_view.selected_search_result =
                    (line as usize).saturating_sub(4) + start_index;
            } else {
                let (start_index, _selected_index_in_range, _end_index) = calculate_list_bounds(
                    self.file_list_view.files.len(),
                    rows_for_list,
                    self.file_list_view.selected(),
                );
                *self.file_list_view.selected_mut() =
                    (line as usize).saturating_sub(4) + start_index;
            }
        }
    }
    pub fn descend_to_previous_path(&mut self) {
        if self.is_in_virtual_root {
            return;
        }
        self.search_term.clear();
        self.search_view.clear_and_reset_selection();
        if self.platform == Platform::Windows
            && Platform::is_root(&self.file_list_view.path, self.platform)
        {
            self.enter_virtual_root();
        } else {
            self.file_list_view.descend_to_previous_path();
        }
    }
    pub fn descend_to_root_path(&mut self) {
        self.is_in_virtual_root = false;
        self.search_term.clear();
        self.search_view.clear_and_reset_selection();
        self.file_list_view.descend_to_root_path(&self.initial_cwd);
        refresh_directory(&self.file_list_view.path);
    }
    pub fn toggle_hidden_files(&mut self) {
        self.hide_hidden_files = !self.hide_hidden_files;
    }
    pub fn traverse_dir(&mut self) {
        if self.is_in_virtual_root {
            if let Some(entry) = self.virtual_root_entries.get(self.virtual_root_selected) {
                let path = entry.get_full_pathbuf();
                self.is_in_virtual_root = false;
                self.file_list_view.path = path.clone();
                self.file_list_view.path_is_dir = true;
                self.file_list_view.files.clear();
                self.file_list_view.clear_selected();
                change_host_folder(path);
            }
            return;
        }
        let entry = if self.is_searching {
            self.search_view.get_selected_entry()
        } else {
            self.file_list_view.get_selected_entry()
        };
        if let Some(entry) = entry {
            match &entry {
                FsEntry::Dir(_p) => {
                    self.file_list_view.enter_dir(&entry);
                    self.search_view.clear_and_reset_selection();
                    refresh_directory(&self.file_list_view.path);
                },
                FsEntry::File(_p, _) => {
                    self.file_list_view.enter_dir(&entry);
                    self.search_view.clear_and_reset_selection();
                    if self.handling_filepick_request_from.is_some() {
                        self.send_filepick_response();
                    } else {
                        self.open_selected_path();
                    }
                },
            }
        } else if self.handling_filepick_request_from.is_some() {
            self.send_filepick_response();
        } else {
            self.open_selected_path();
        }
        self.is_searching = false;
        self.search_term.clear();
        self.search_view.clear_and_reset_selection();
    }
    pub fn update_files(&mut self, paths: Vec<(PathBuf, Option<FileMetadata>)>) {
        if self.is_in_virtual_root {
            self.update_virtual_root_entries(paths);
        } else {
            self.file_list_view
                .update_files(paths, self.hide_hidden_files);
        }
    }
    pub fn open_selected_path(&mut self) {
        if self.file_list_view.path_is_dir {
            if self.close_on_selection {
                open_terminal_in_place_of_plugin(&self.file_list_view.path, true);
            } else {
                open_terminal(&self.file_list_view.path);
            }
        } else {
            if let Some(parent_folder) = self.file_list_view.path.parent() {
                if self.close_on_selection {
                    open_file_in_place_of_plugin(
                        FileToOpen::new(&self.file_list_view.path).with_cwd(parent_folder.into()),
                        true,
                        BTreeMap::new(),
                    );
                } else {
                    open_file(
                        FileToOpen::new(&self.file_list_view.path).with_cwd(parent_folder.into()),
                        BTreeMap::new(),
                    );
                }
            } else {
                if self.close_on_selection {
                    open_file_in_place_of_plugin(
                        FileToOpen::new(&self.file_list_view.path),
                        true,
                        BTreeMap::new(),
                    );
                } else {
                    open_file(FileToOpen::new(&self.file_list_view.path), BTreeMap::new());
                }
            }
        }
    }
    pub fn enter_virtual_root(&mut self) {
        self.is_in_virtual_root = true;
        self.virtual_root_entries.clear();
        self.virtual_root_selected = 0;
        self.search_term.clear();
        self.is_searching = false;
        list_windows_volumes();
    }
    pub fn exit_virtual_root(&mut self) {
        self.is_in_virtual_root = false;
        refresh_directory(&self.file_list_view.path);
    }
    fn update_virtual_root_entries(&mut self, paths: Vec<(PathBuf, Option<FileMetadata>)>) {
        let mut entries = vec![];
        for (path, _metadata) in paths {
            let path = Platform::normalize(&path);
            entries.push(FsEntry::Dir(path));
        }
        // Drive letters first (e.g. "C:/"), then WSL distros (e.g. "//wsl.localhost/...")
        entries.sort_unstable_by(|a, b| {
            let a_is_drive = !a.get_full_pathbuf().to_string_lossy().starts_with("//");
            let b_is_drive = !b.get_full_pathbuf().to_string_lossy().starts_with("//");
            b_is_drive.cmp(&a_is_drive).then_with(|| a.cmp(b))
        });
        self.virtual_root_entries = entries;
        if self.virtual_root_selected >= self.virtual_root_entries.len() {
            self.virtual_root_selected = 0;
        }
    }
    pub fn render_virtual_root(&self, rows: usize, cols: usize) {
        let (start_index, selected_index_in_range, end_index) = calculate_list_bounds(
            self.virtual_root_entries.len(),
            rows.saturating_sub(1),
            Some(self.virtual_root_selected),
        );
        render_list_tip(3, cols);
        for i in start_index..end_index {
            if let Some(entry) = self.virtual_root_entries.get(i) {
                let is_selected = Some(i) == selected_index_in_range;
                let display_name =
                    Platform::virtual_root_display_name(&entry.get_full_pathbuf(), self.platform);
                let display_name_width = display_name.width();
                let text = if display_name_width < cols {
                    let padding = " ".repeat(cols.saturating_sub(display_name_width));
                    format!("{}{}", display_name, padding)
                } else {
                    display_name
                };
                let mut text_element = if is_selected {
                    Text::new(text).selected()
                } else {
                    Text::new(text)
                };
                text_element = text_element.color_range(0, ..);
                print_text_with_coordinates(
                    text_element,
                    0,
                    4 + i.saturating_sub(start_index),
                    Some(cols),
                    None,
                );
            }
        }
    }
    pub fn send_filepick_response(&mut self) {
        let selected_path = &self.file_list_view.path;
        let host_path = Platform::to_host_display(selected_path, self.platform);
        match &self.handling_filepick_request_from {
            Some((PipeSource::Plugin(plugin_id), args)) => {
                pipe_message_to_plugin(
                    MessageToPlugin::new("filepicker_result")
                        .with_destination_plugin_id(*plugin_id)
                        .with_args(args.clone())
                        .with_payload(host_path),
                );
                #[cfg(target_family = "wasm")]
                close_self();
            },
            #[allow(unused_variables)]
            // pipe_id is used inside #[cfg(target_family = "wasm")] blocks
            Some((PipeSource::Cli(pipe_id), _args)) => {
                #[cfg(target_family = "wasm")]
                cli_pipe_output(pipe_id, &host_path);
                #[cfg(target_family = "wasm")]
                unblock_cli_pipe_input(pipe_id);
                #[cfg(target_family = "wasm")]
                close_self();
            },
            _ => {},
        }
    }
}

pub(crate) fn refresh_directory(full_path: &Path) {
    change_host_folder(PathBuf::from(full_path));
}