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
#![feature(never_type)]
#![feature(exit_status_error)]
use std::collections::HashSet;
use datatypes::Bread;
pub mod args;
pub mod config;
pub mod datatypes;
pub mod fs_operation;
use datatypes::*;
pub fn prompt(mut conf: config::Config) -> Result<Option<HashSet<String>>, String> {
if conf.delete {
// only delete is true gonna triger the prompt
conf.delete = false;
let yes = conf.yes;
let breads = fs_operation::handle_files(conf).collect::<Vec<_>>();
let mut files_changed = None;
if yes {
let mut cache = HashSet::new();
for b in breads {
cache.insert(
fs_operation::delete_the_crumbs(b).map_err(|e| e.to_string())?,
);
}
if !cache.is_empty() {
files_changed = Some(cache)
};
} else {
let mut rl = rustyline::Editor::<()>::new();
loop {
breads.iter().for_each(|b| println!("{}", b));
match rl.readline("Are you sure you want to delete all crumbs? (y/n/s/i): ") {
Ok(s) => match s.as_str() {
"y" => {
let mut cache = HashSet::new();
for b in breads {
cache.insert(
fs_operation::delete_the_crumbs(b).map_err(|e| e.to_string())?,
);
}
if !cache.is_empty() {
files_changed = Some(cache)
};
}
"n" => (), // do nothing
"s" => continue,
"i" => {
files_changed = Some(prompt_bread(breads.into_iter(), &mut rl, "delete")?)
}
_ => return Err("I don't understand, please give y/n/s/i".to_string()),
},
Err(e) => return Err(format!("error in prompt readline {}", e.to_string())),
}
break;
}
}
Ok(files_changed)
} else if conf.restore {
let yes = conf.yes;
let breads = fs_operation::handle_files(conf).collect::<Vec<_>>();
let mut files_changed = None;
if yes {
let mut cache = HashSet::new();
for b in breads {
cache.insert(
fs_operation::restore_the_crumb(b).map_err(|e| e.to_string())?,
);
}
if !cache.is_empty() {
files_changed = Some(cache)
};
} else {
let mut rl = rustyline::Editor::<()>::new();
loop {
breads.iter().for_each(|b| println!("{}", b));
match rl.readline("Are you sure you want to restore all crumbs? (y/n/s/i): ") {
Ok(s) => match s.as_str() {
"y" => {
let mut cache = HashSet::new();
for b in breads {
cache.insert(
fs_operation::restore_the_crumb(b).map_err(|e| e.to_string())?,
);
}
if !cache.is_empty() {
files_changed = Some(cache)
};
}
"n" => (), // do nothing
"s" => continue,
"i" => {
files_changed = Some(prompt_bread(breads.into_iter(), &mut rl, "restore")?)
}
_ => return Err("I don't understand, please give y/n/s/i".to_string()),
},
Err(e) => return Err(format!("error in prompt readline {}", e.to_string())),
}
break;
}
}
Ok(files_changed)
} else {
match conf.output {
config::OutputFormat::None => {
fs_operation::handle_files(conf).for_each(|b| println!("{}", b))
}
config::OutputFormat::Json => {
println!(
"{}",
serde_json::to_string(&fs_operation::handle_files(conf).collect::<Vec<_>>())
.map_err(|e| e.to_string())?
)
}
config::OutputFormat::List => fs_operation::handle_files(conf).for_each(|b| {
b.crumbs
.iter()
.for_each(|crumb| println!("{}:{}", b.file_path, crumb.list_format()))
}),
config::OutputFormat::Range => fs_operation::handle_files(conf).for_each(|b| {
b.crumbs.iter().for_each(|crumb| {
println!(
r#"File path: {}
The mark:
{}
Context around:
{}
=======================
"#,
b.file_path,
crumb,
crumb.range_format()
)
})
}),
}
Ok(None)
}
}
fn prompt_bread(
breads: impl Iterator<Item = Bread>,
rl: &mut rustyline::Editor<()>,
op: &str,
) -> Result<HashSet<String>, String> {
let mut files_changed = HashSet::new();
for b in breads {
loop {
// incase need show again
println!("{}", b);
match rl.readline(&format!(
"Are you sure you want to {} this bread {}? (y/n/s/i): ",
op, b.file_path
)) {
Ok(s) => match s.as_str() {
"y" => match op {
"delete" => {
files_changed.insert(
fs_operation::delete_the_crumbs(b).map_err(|e| e.to_string())?,
);
}
"restore" => {
files_changed.insert(
fs_operation::restore_the_crumb(b).map_err(|e| e.to_string())?,
);
}
_ => (),
},
"n" => {}
"s" => {
continue;
}
"i" => {
let go_to_handle = prompt_crumbs(b.crumbs.iter(), rl, op)?;
if go_to_handle.len() != 0 {
match op {
"delete" => {
files_changed.insert(
fs_operation::delete_the_crumbs_on_special_index(
b,
go_to_handle,
)
.map_err(|e| e.to_string())?,
);
}
"restore" => {
files_changed.insert(
fs_operation::restore_the_crumb_on_special_index(
b,
go_to_handle,
)
.map_err(|e| e.to_string())?,
);
}
_ => (),
}
}
}
_ => {
println!("I don't understand, please give y/n/s/i");
}
},
Err(e) => return Err(e.to_string()),
}
break;
}
}
Ok(files_changed)
}
fn prompt_crumbs<'a>(
crumbs: impl Iterator<Item = &'a Crumb>,
rl: &mut rustyline::Editor<()>,
op: &str,
) -> Result<HashSet<usize>, String> {
let mut going_to_handle_crumbs_indexes = HashSet::new();
for (ind, c) in crumbs.enumerate() {
loop {
println!("{}", c);
match rl.readline(&format!(
"Are you sure you want to {} this crumb? (y/n/s): ",
op
)) {
Ok(s) => match s.as_str() {
"y" => {
going_to_handle_crumbs_indexes.insert(ind);
}
"n" => {}
"s" => {
continue;
}
_ => {
println!("I don't understand, please give y/n/s");
}
},
Err(e) => return Err(e.to_string()),
}
break;
}
}
Ok(going_to_handle_crumbs_indexes)
}