kaa_file_manager 1.0.0

CLI file manager. Very simple. By KAA
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! # KAA File Manager
//! My first public crate

use inquire::{Select, Text};
use colored::*;
use std::fs::{File, read_dir, read_to_string, write, remove_file, create_dir, remove_dir};
use std::env;

pub fn run() {
    println!("{}", "Welcome to File manager".purple().bold());
    home(None);
}

// Functions for user interaction

pub fn home(folder: Option<String>) {
    let main_dir = folder.clone().unwrap_or(get_main_dir());

    let mut options = vec![
        "Open folder".to_string(),
        "Find folder".to_string(),
        "Find file".to_string(),
        "Create file".to_string(),
        "Create folder".to_string(),
        "Read file".to_string(),
        "Write to file".to_string(),
        "Remove file".to_string(),
        "Remove folder".to_string(),
    ];
    
    if !folder.is_none() && folder.unwrap() != get_main_dir() {
        options.push("Return to the main dir".blue().bold().to_string());
    }
    options.push("Exit".red().bold().to_string());

    let option = Select::new(&format!("[{}] {}", main_dir, "Select option:".white().bold()), options)
        .prompt().unwrap();

    if option == "Return to the main dir".blue().bold().to_string() {
        home(None)
    } else if option == "Create file" {
        let dir = select_dir(&main_dir, Some("Create here"));
        
        if dir == "Exit" {
            return;
        } else if dir == "Return" {
            return home(Some(main_dir));
        } else {
            let mut filename = Text::new("Enter file name: ").prompt().unwrap();
            filename.insert_str(0, "/");
            filename.insert_str(0, &dir);
            
            match File::create(&filename) {
                Ok(_) => {
                    if Text::new("File successfully created! Return? [y/n]")
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                },
                Err(e) => {
                    if Text::new(&format!("Error while creating file: {}. Return? [y/n]", e))
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                }
            }
        }
    } else if option == "Find file" {
        let filename = Text::new("Enter name of file to find (example: file.txt):").prompt().unwrap();
        let mut filepaths: Vec<String> = Vec::new();

        find_file(&filename, &main_dir, &mut filepaths);
        if filepaths.len() == 0 {
            if Text::new("No such file. Return? [y/n]")
                .prompt().unwrap().to_lowercase() == "y" {
                    home(Some(main_dir));
                } else {
                    println!("{}", "Exit".red().bold());
            }
        } else {
            let mut fp_cnt = 1u32;
            for fp in &filepaths {
                if fp_cnt == 1u32 {
                    println!("Full file path: {}", fp);
                } else {
                    println!("One more file path ({}): {}", fp_cnt, fp);
                }
                fp_cnt += 1;
            }
            if Text::new("Return? [y/n]")
                .prompt().unwrap().to_lowercase() == "y" {
                    home(Some(main_dir));
                } else {
                    println!("{}", "Exit".red().bold());
            }
        }
    } else if option == "Read file" {
        let file = select_file(&main_dir, None);

        if file == "Exit".to_string() {
            return;
        } else if file == "Return".to_string() {
            return home(Some(main_dir));
        } else {
            match read_to_string(file) {
                Ok(content) => {
                    println!("{}\n{}", "File content:".cyan().bold(), content);
                    if Text::new("Return? [y/n]")
                                .prompt().unwrap().to_lowercase() == "y" {
                                    home(Some(main_dir));
                                } else {
                                    println!("{}", "Exit".red().bold());
                            }
                },
                Err(e) => {
                    if Text::new(&format!("Error while reading file: {}. Return? [y/n]", e))
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                },
            }
        }
    } else if option == "Write to file" {
        let file = select_file(&main_dir, None);

        if file == "Exit".to_string() {
            return;
        } else if file == "Return".to_string() {
            return home(Some(main_dir));
        } else {
            match write(file, Text::new("Enter new file content:").prompt().unwrap()) {
                Ok(_) => {
                    if Text::new("New content has been read. Return? [y/n]")
                                .prompt().unwrap().to_lowercase() == "y" {
                                    home(Some(main_dir));
                                } else {
                                    println!("{}", "Exit".red().bold());
                            }
                },
                Err(e) => {
                    if Text::new(&format!("Error while writing to file: {}. Return? [y/n]", e))
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                },
            }
        }
    } else if option == "Open folder" {
        let dir = select_dir(&main_dir, Some("Open here"));
        if dir != "Exit".to_string() {
            if dir == "Return".to_string() {
                home(Some(main_dir));
            } else {
                home(Some(dir));
            }
        }
    } else if option == "Remove file" {
        let file = select_file(&main_dir, None);

        if file == "Exit".to_string() {
            return;
        } else if file == "Return".to_string() {
            return home(Some(main_dir));
        } else {
            if Select::new("Are you sure?", vec![
                    "Remove this file".green().bold().to_string(),
                    "Cancel".red().bold().to_string(),
                ])
                .prompt().unwrap() != "Remove this file".green().bold().to_string() {
                    println!("{}", "Operation canceled".red().bold());
                    return home(Some(main_dir));
                }
            match remove_file(file) {
                Ok(_) => {
                    if Text::new("The file has been successfully removed. Return? [y/n]")
                                .prompt().unwrap().to_lowercase() == "y" {
                                    home(Some(main_dir));
                                } else {
                                    println!("{}", "Exit".red().bold());
                            }
                },
                Err(e) => {
                    if Text::new(&format!("Error while removing file: {}. Return? [y/n]", e))
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                },
            }
        }
    } else if option == "Find folder" {
        let dirname = Text::new("Enter name of folder to find:").prompt().unwrap();
        let mut dirpaths: Vec<String> = Vec::new();

        find_folder(&dirname, &main_dir, &mut dirpaths);
        if dirpaths.len() == 0 {
            if Text::new("No such folder. Return? [y/n]")
                .prompt().unwrap().to_lowercase() == "y" {
                    home(Some(main_dir));
                } else {
                    println!("{}", "Exit".red().bold());
            }
        } else {
            dirpaths.push("No, return".red().bold().to_string());
            let selected_dir = Select::new(
                &format!("Found {} dir(s). Open it?", dirpaths.len() - 1).white().bold().to_string(), dirpaths
            ).prompt().unwrap();
            if selected_dir == "No, return".red().bold().to_string() {
                home(Some(main_dir));
            } else {
                home(Some(selected_dir));
            }
        }
    } else if option == "Create folder" {
        let dir = select_dir(&main_dir, Some("Create here"));
        
        if dir == "Exit" {
            return;
        } else if dir == "Return" {
            return home(Some(main_dir));
        } else {
            let mut filename = Text::new("Enter folder name:").prompt().unwrap();
            filename.insert_str(0, "/");
            filename.insert_str(0, &dir);
            
            match create_dir(&filename) {
                Ok(_) => {
                    if Text::new("Directory successfully created! Return? [y/n]")
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                },
                Err(e) => {
                    if Text::new(&format!("Error while creating folder: {}. Return? [y/n]", e))
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                }
            }
        }
    }
    else if option == "Remove folder" {
        let dir = select_dir(&main_dir, Some("Remove this"));
        
        if dir == "Exit" {
            return;
        } else if dir == "Return" {
            return home(Some(main_dir));
        } else {
            if Select::new(
                &"Are you sure? All files in this dir will also be removed".white().bold().to_string(),
                vec![
                    "Remove this folder".green().bold().to_string(),
                    "Cancel".red().bold().to_string(),
                ]
            ).prompt().unwrap() != "Remove this folder".green().bold().to_string() {
                    println!("{}", "Operation canceled".red().bold());
                    return home(Some(main_dir));
                }
            if let Ok(entries) = read_dir(&dir) {
                for entry in entries.flatten() {
                    if let Ok(file_type) = entry.file_type() {
                        if file_type.is_file() {
                            match remove_file(entry.path().to_str().unwrap()) {
                                Ok(_) => {
                                    println!("{}", &format!(
                                        "The file ({}) has been successfully removed",
                                        entry.file_name().to_str().unwrap()
                                    ))},
                                Err(e) => {
                                    if Text::new(&format!(
                                        "Error while removing file ({}) in the folder: {}. Return? [y/n]",
                                        entry.file_name().to_str().unwrap(), e
                                    )).prompt().unwrap().to_lowercase() == "y" {
                                            home(Some(main_dir.clone()));
                                        } else {
                                            println!("{}", "Exit".red().bold());
                                    }
                                },
                            }
                        }
                    }
                }
            }
            match remove_dir(&dir) {
                Ok(_) => {
                    if Text::new("Directory successfully removed! Return? [y/n]")
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                },
                Err(e) => {
                    if Text::new(&format!("Error while removing folder: {}. Return? [y/n]", e))
                        .prompt().unwrap().to_lowercase() == "y" {
                            home(Some(main_dir));
                        } else {
                            println!("{}", "Exit".red().bold());
                    }
                }
            }
        }
    }
}

fn select_dir(path: &str, return_name: Option<&str>) -> String {
    let mut dirs: Vec::<String> = Vec::new();
    match return_name {
        Some(name) => dirs.push(name.green().bold().to_string()),
        None => {},
    }

    if let Ok(entries) = read_dir(path) {
        for entry in entries.flatten() {
            if let Ok(file_type) = entry.file_type() {
                if file_type.is_dir() {
                    match entry.path().to_str() {
                        Some(dir) => dirs.push(dir.to_string()),
                        None => {},
                    }
                }
            }
        }
    }
    if dirs.len() == 0 {
        return path.to_string();
    } else {
        dirs.push("Return".blue().bold().to_string());
        dirs.push("Exit".red().bold().to_string());
        
        let dirnow = Select::new(&"Select folder:".white().bold().to_string(), dirs.clone())
            .prompt().unwrap();

        if dirnow == "Exit".red().bold().to_string() {
            return "Exit".to_string();
        } else if dirnow == "Return".blue().bold().to_string() {
            return "Return".to_string();
        } else if let Some(rn) = return_name {
            if dirnow == rn.green().bold().to_string() {
                return path.to_string();
            }
        }
        return select_dir(&dirnow, return_name);
    }
}

fn select_file(path: &str, return_name: Option<&str>) -> String {
    let mut dirs_files: Vec::<String> = Vec::new();
    match return_name {
        Some(name) => dirs_files.push(name.green().bold().to_string()),
        None => {},
    }

    if let Ok(entries) = read_dir(path) {
        for entry in entries.flatten() {
            if let Ok(file_type) = entry.file_type() {
                if file_type.is_dir() {
                    match entry.path().to_str() {
                        Some(obj) => dirs_files.push(format!("Dir: {}", obj)),
                        None => {},
                    }
                } else if file_type.is_file() {
                    match entry.path().to_str() {
                        Some(obj) => dirs_files.push(format!("File: {}", obj)),
                        None => {},
                    }
                }
            }
        }
    }
    if dirs_files.len() == 0 {
        return path.to_string();
    } else {
        dirs_files.push("Return".blue().bold().to_string());
        dirs_files.push("Exit".red().bold().to_string());
        
        let objnow = &Select::new(&"Select file or folder:".white().bold().to_string(), dirs_files.clone())
            .prompt().unwrap();

        if objnow == &"Exit".red().bold().to_string() {
            return "Exit".to_string();
        } else if objnow == &"Return".blue().bold().to_string() {
            return "Return".to_string();
        } else if let Some(rn) = return_name {
            if *objnow == rn.green().bold().to_string() {
                return path.to_string();
            }
        }
        if objnow.chars().take(3).collect::<String>() == "Dir".to_string() {
            return select_file(&objnow.chars().skip(5).collect::<String>(), return_name);
        } else {
            return objnow.chars().skip(6).collect::<String>();
        }
    }
}

// General functions

pub fn find_file(filename: &str, folder: &str, filepaths: &mut Vec<String>) -> () {
    if let Ok(entries) = read_dir(folder) {
        for entry in entries.flatten() {
            if let Ok(file_type) = entry.file_type() {
                if file_type.is_dir() {
                    find_file(filename, entry.path().to_str().unwrap(), filepaths)
                } else if file_type.is_file() {
                    match entry.path().to_str() {
                        Some(filepath) => {
                            if entry.file_name() == filename {
                                filepaths.push(filepath.to_string());
                            }
                        },
                        None => {},
                    }
                }
            }
        }
    }
}

pub fn find_folder(dirname: &str, folder: &str, dirpaths: &mut Vec<String>) -> () {
    if let Ok(entries) = read_dir(folder) {
        for entry in entries.flatten() {
            if let Ok(file_type) = entry.file_type() {
                if file_type.is_dir() {
                    match entry.path().to_str() {
                        Some(filepath) => {
                            if entry.file_name() == dirname {
                                dirpaths.push(filepath.to_string());
                            }
                            find_folder(dirname, entry.path().to_str().unwrap(), dirpaths)
                        },
                        None => {},
                    }
                }
            }
        }
    }
}

pub fn get_main_dir() -> String {
    let mut current_dirs: Vec<String> = Vec::new();
    let mut is = String::new();
    for i in String::from(env::current_dir().unwrap().to_str().unwrap()).chars() {
        if i != '/' {
            is.push_str(&i.to_string());
            is = String::new();
        } else {
            current_dirs.push(is.clone())
        }
    }
    
    if current_dirs[0] == "".to_string() {
        format!("/{}", current_dirs[1])
    } else {
        current_dirs[0].clone()
    }
}