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
|
use std::fs;
use std::path;
use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::ui;
use crate::util::event::Event;
use crate::window::JoshutoView;
use crate::KEYMAP_T;
#[derive(Clone, Debug)]
pub struct DeleteFiles;
impl DeleteFiles {
pub fn new() -> Self {
DeleteFiles
}
pub const fn command() -> &'static str {
"delete_files"
}
pub fn remove_files(paths: &[&path::PathBuf]) -> std::io::Result<()> {
for path in paths {
if let Ok(metadata) = fs::symlink_metadata(path) {
if metadata.is_dir() {
fs::remove_dir_all(&path)?;
} else {
fs::remove_file(&path)?;
}
}
}
Ok(())
}
fn delete_files(context: &mut JoshutoContext, view: &JoshutoView) -> std::io::Result<()> {
ui::wprint_msg(&view.bot_win, "Delete selected files? (Y/n)");
ncurses::doupdate();
let curr_tab = &mut context.tabs[context.curr_tab_index];
let paths = curr_tab.curr_list.get_selected_paths();
if paths.is_empty() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"no files selected",
));
}
let mut ch = termion::event::Key::Char('n');
while let Ok(evt) = context.events.next() {
match evt {
Event::Input(key) => {
if key == termion::event::Key::Char('y') ||
key == termion::event::Key::Char('\n') {
if paths.len() > 1 {
ui::wprint_msg(&view.bot_win, "Are you sure? (y/N)");
ncurses::doupdate();
while let Ok(evt) = context.events.next() {
match evt {
Event::Input(key) => {
ch = key;
break;
}
_ => {}
}
}
} else {
ch = termion::event::Key::Char('y');
}
}
break;
}
_ => {}
}
}
if ch == termion::event::Key::Char('y') {
Self::remove_files(&paths)?;
ui::wprint_msg(&view.bot_win, "Deleted files");
ReloadDirList::reload(context.curr_tab_index, context)?;
}
Ok(())
}
}
impl JoshutoCommand for DeleteFiles {}
impl std::fmt::Display for DeleteFiles {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(Self::command())
}
}
impl JoshutoRunnable for DeleteFiles {
fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) -> JoshutoResult<()> {
Self::delete_files(context, view)?;
let curr_tab = &mut context.tabs[context.curr_tab_index];
curr_tab.refresh_curr(&view.mid_win, &context.config_t);
if context.config_t.show_preview {
curr_tab.refresh_preview(&view.right_win, &context.config_t);
}
curr_tab.refresh_path_status(&view.top_win, &context.config_t);
curr_tab.refresh_file_status(&view.bot_win);
ncurses::doupdate();
Ok(())
}
}
|