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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::sync::Arc;
use log::info;
use regex::Regex;
use skim::SkimItem;
use sysinfo::{Disk, DiskExt, RefreshKind, System, SystemExt};
use tuikit::term::Term;
use users::UsersCache;
use crate::args::Args;
use crate::bulkrename::Bulk;
use crate::compress::Compresser;
use crate::config::Colors;
use crate::constant_strings_paths::{OPENER_PATH, TUIS_PATH};
use crate::copy_move::{copy_move, CopyMove};
use crate::cryptsetup::DeviceOpener;
use crate::flagged::Flagged;
use crate::fm_error::{FmError, FmResult};
use crate::marks::Marks;
use crate::opener::{load_opener, Opener};
use crate::preview::{Directory, Preview};
use crate::shell_menu::{load_shell_menu, ShellMenu};
use crate::skim::Skimer;
use crate::tab::Tab;
use crate::term_manager::MIN_WIDTH_FOR_DUAL_PANE;
use crate::trash::Trash;
use crate::utils::{disk_space, filename_from_path};
pub struct Status {
pub tabs: [Tab; 2],
pub index: usize,
pub flagged: Flagged,
pub marks: Marks,
term: Arc<Term>,
skimer: Skimer,
pub dual_pane: bool,
pub system_info: System,
pub display_full: bool,
pub preview_second: bool,
pub opener: Opener,
pub help: String,
pub trash: Trash,
pub encrypted_devices: DeviceOpener,
pub compression: Compresser,
pub nvim_server: String,
pub force_clear: bool,
pub bulk: Bulk,
pub shell_menu: ShellMenu,
}
impl Status {
pub const MAX_PERMISSIONS: u32 = 0o777;
pub fn new(
args: Args,
height: usize,
term: Arc<Term>,
help: String,
terminal: &str,
) -> FmResult<Self> {
let opener = load_opener(OPENER_PATH, terminal).unwrap_or_else(|_| {
eprintln!("Couldn't read the opener config file at {OPENER_PATH}. See https://raw.githubusercontent.com/qkzk/fm/master/config_files/fm/opener.yaml for an example. Using default.");
info!("Couldn't read opener file at {OPENER_PATH}. Using default.");
Opener::new(terminal)
});
let Ok(shell_menu) = load_shell_menu(TUIS_PATH) else {
eprintln!("Couldn't load the TUIs config file at {TUIS_PATH}. See https://raw.githubusercontent.com/qkzk/fm/master/config_files/fm/tuis.yaml for an example");
info!("Couldn't read tuis file at {TUIS_PATH}. Exiting");
std::process::exit(1);
};
let sys = System::new_with_specifics(RefreshKind::new().with_disks());
let nvim_server = args.server.clone();
let encrypted_devices = DeviceOpener::default();
let trash = Trash::new()?;
let compression = Compresser::default();
let force_clear = false;
let bulk = Bulk::default();
let users_cache = unsafe { UsersCache::with_all_users() };
let mut right_tab = Tab::new(args.clone(), height, users_cache)?;
right_tab
.shortcut
.extend_with_mount_points(&Self::disks_mounts(sys.disks()));
let users_cache2 = unsafe { UsersCache::with_all_users() };
let mut left_tab = Tab::new(args, height, users_cache2)?;
left_tab
.shortcut
.extend_with_mount_points(&Self::disks_mounts(sys.disks()));
Ok(Self {
tabs: [left_tab, right_tab],
index: 0,
flagged: Flagged::default(),
marks: Marks::read_from_config_file(),
skimer: Skimer::new(term.clone()),
term,
dual_pane: true,
preview_second: false,
system_info: sys,
display_full: true,
opener,
help,
trash,
encrypted_devices,
compression,
nvim_server,
force_clear,
bulk,
shell_menu,
})
}
pub fn next(&mut self) {
if !self.dual_pane {
return;
}
self.index = 1 - self.index
}
pub fn prev(&mut self) {
self.next()
}
pub fn selected(&mut self) -> &mut Tab {
&mut self.tabs[self.index]
}
pub fn selected_non_mut(&self) -> &Tab {
&self.tabs[self.index]
}
pub fn reset_tabs_view(&mut self) -> FmResult<()> {
for tab in self.tabs.iter_mut() {
tab.refresh_view()?
}
Ok(())
}
pub fn toggle_flag_on_path(&mut self, path: &Path) {
self.flagged.toggle(path)
}
pub fn skim_output_to_tab(&mut self) -> FmResult<()> {
let skim = self.skimer.search_filename(
self.selected_non_mut()
.selected()
.ok_or_else(|| FmError::custom("skim", "no selected file"))?
.path
.to_str()
.ok_or_else(|| FmError::custom("skim", "skim error"))?,
);
let Some(output) = skim.first() else {return Ok(())};
self._update_tab_from_skim_output(output)
}
pub fn skim_line_output_to_tab(&mut self) -> FmResult<()> {
let skim = self.skimer.search_line_in_file();
let Some(output) = skim.first() else {return Ok(())};
self._update_tab_from_skim_line_output(output)
}
fn _update_tab_from_skim_line_output(
&mut self,
skim_output: &Arc<dyn SkimItem>,
) -> FmResult<()> {
let output_str = skim_output.output().to_string();
let Some(filename) = output_str.split(':').next() else { return Ok(());};
let path = fs::canonicalize(filename)?;
self._replace_path_by_skim_output(path)
}
fn _update_tab_from_skim_output(&mut self, skim_output: &Arc<dyn SkimItem>) -> FmResult<()> {
let path = fs::canonicalize(skim_output.output().to_string())?;
self._replace_path_by_skim_output(path)
}
fn _replace_path_by_skim_output(&mut self, path: std::path::PathBuf) -> FmResult<()> {
let tab = self.selected();
if path.is_file() {
let Some(parent) = path.parent() else { return Ok(()) };
tab.set_pathcontent(parent)?;
let filename = filename_from_path(&path)?;
tab.search_from(filename, 0);
} else if path.is_dir() {
tab.set_pathcontent(&path)?;
}
Ok(())
}
pub fn filtered_flagged_files(&self) -> Vec<&Path> {
self.flagged
.filtered(&self.selected_non_mut().path_content.path)
}
pub fn cut_or_copy_flagged_files(&mut self, cut_or_copy: CopyMove) -> FmResult<()> {
let sources = self.flagged.content.clone();
let dest = self
.selected_non_mut()
.path_content_str()
.ok_or_else(|| FmError::custom("cut or copy", "unreadable path"))?;
copy_move(cut_or_copy, sources, dest, self.term.clone())?;
self.clear_flags_and_reset_view()
}
pub fn clear_flags_and_reset_view(&mut self) -> FmResult<()> {
self.flagged.clear();
self.reset_tabs_view()
}
pub fn set_permissions<P>(path: P, permissions: u32) -> FmResult<()>
where
P: AsRef<Path>,
{
Ok(std::fs::set_permissions(
path,
std::fs::Permissions::from_mode(permissions),
)?)
}
pub fn select_from_regex(&mut self) -> Result<(), regex::Error> {
if self.selected_non_mut().input.string().is_empty() {
return Ok(());
}
self.flagged.clear();
let re = Regex::new(&self.selected_non_mut().input.string())?;
for file in self.tabs[self.index].path_content.content.iter() {
if re.is_match(&file.path.to_string_lossy()) {
self.flagged.push(file.path.clone());
}
}
Ok(())
}
pub fn select_tab(&mut self, index: usize) -> FmResult<()> {
if index >= self.tabs.len() {
Err(FmError::custom(
"select tab",
&format!("Only {} tabs. Can't select tab {}", self.tabs.len(), index),
))
} else {
self.index = index;
Ok(())
}
}
pub fn refresh_disks(&mut self) {
self.system_info.refresh_disks_list();
let disks = self.system_info.disks();
let mounts = Self::disks_mounts(disks);
self.tabs[0].refresh_shortcuts(&mounts);
self.tabs[1].refresh_shortcuts(&mounts);
}
pub fn disks(&self) -> &[Disk] {
self.system_info.disks()
}
pub fn disk_spaces_per_tab(&self) -> (String, String) {
let disks = self.disks();
(
disk_space(disks, &self.tabs[0].path_content.path),
disk_space(disks, &self.tabs[1].path_content.path),
)
}
pub fn disks_mounts(disks: &[Disk]) -> Vec<&Path> {
disks.iter().map(|d| d.mount_point()).collect()
}
pub fn term_size(&self) -> FmResult<(usize, usize)> {
Ok(self.term.term_size()?)
}
pub fn selected_path_str(&self) -> &str {
self.selected_non_mut()
.path_content_str()
.unwrap_or_default()
}
pub fn refresh_users(&mut self) -> FmResult<()> {
for tab in self.tabs.iter_mut() {
let users_cache = unsafe { UsersCache::with_all_users() };
tab.refresh_users(users_cache)?;
}
Ok(())
}
pub fn remove_tree(&mut self) -> FmResult<()> {
let path = self.selected_non_mut().path_content.path.clone();
let users_cache = &self.selected_non_mut().path_content.users_cache;
self.selected().directory = Directory::empty(&path, users_cache)?;
Ok(())
}
pub fn read_encrypted_devices(&mut self) -> FmResult<()> {
self.encrypted_devices.update()?;
Ok(())
}
pub fn force_preview(&mut self, colors: &Colors) -> FmResult<()> {
let fileinfo = &self.tabs[0]
.selected()
.ok_or_else(|| FmError::custom("force preview", "No file to select"))?;
let users_cache = &self.tabs[0].path_content.users_cache;
self.tabs[0].preview =
Preview::new(fileinfo, users_cache, self, colors).unwrap_or_default();
Ok(())
}
pub fn set_dual_pane_if_wide_enough(&mut self, width: usize) -> FmResult<()> {
if width < MIN_WIDTH_FOR_DUAL_PANE {
self.select_tab(0)?;
self.dual_pane = false;
} else {
self.dual_pane = true;
}
Ok(())
}
pub fn must_quit(&self) -> bool {
self.selected_non_mut().must_quit()
}
pub fn force_clear(&mut self) {
self.force_clear = true;
}
}