fast_files 0.1.1

File directory storage UI for fast access and sorting
Documentation
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use std::io::{self, Write, BufRead};
use std::collections::HashMap;
use regex::Regex;
use std::{thread, time};
use std::fs::{self, OpenOptions, File};
use std::path::Path;
use std::process::Command;

/// This program uses state-based design. On startup, a blank state is declared and is manipulated
/// by the functions of the library to, for example, change the menu status, change possible input
/// commands, and make changes to local storage hashmaps.

pub struct State {
    menu: String,
    commands: Vec<String>,
    comment: String,
    directories: HashMap<String, String>, // saved in order of (filepath, endpoint)
    launch_command: HashMap<String, String>, // saved in order of (extension, command)
}

pub fn start_splash() {
    println!("Fast Files - File directory storage UI for fast access and sorting\n");
}

fn poll_commands(state: &mut State) -> () { // poll_command() should return type fn()
    // initialize text input variables
    let mut text_entry = String::new();
    let mut commands: Vec<String> = Vec::new();

    // preparing IO
    print!("> ");
    io::stdout().flush().expect("Failed to flush");
    io::stdin()
        .read_line(&mut text_entry)
        .expect("Failed to read line");   

    // tokenizing commands
    for word in text_entry.split_whitespace() {
        commands.push(String::from(word));
    }        

    // making sure first command is only a single character, else try polling again
    if commands[0].len() != 1 {
        println!("Invalid command at index 0, character length should be 1");
        poll_commands(state);
    }

    // creating list of valid arguments depending on the menu state
    let valid_args = &state.commands;

    // command list: level1 checks if the command is available in the menu state, level2 executes the command.
    let _command_level1 = match &commands[0] {
        x if valid_args.contains(x) => {
            let _command_level2 = match commands[0].as_str() {
                "l" => return state.directories(),
                "o" => return state.open_directory(),
                "n" => if &state.menu == "Directories" {
                    return state.new_directory()
                } else if &state.menu == "LaunchCommands" {
                    return state.new_launchcommand() 
                },
                "r" => if &state.menu == "MainMenu" {
                    return state.refresh_list()
                } else {
                    return state.main_menu()
                },
                "d" => if &state.menu == "MainMenu" {
                    return state.launch_commands()
                } else if &state.menu == "Directories" {
                    return state.delete_directory()
                } else if &state.menu == "LaunchCommands" {
                    return state.delete_launchcommand()
                },
                "q" => std::process::exit(0),
                &_ => poll_commands(state),
            };
        },
        &_ => poll_commands(state), 
    };
    // ToDo: Check to see if second command which should be a directory exists in the directory storage file.

}

impl State {
    
    // On program startup, create a new state which dictates what menu and options are displayed
    pub fn new() -> State {
        State {
            menu: String::new(),
            commands: Vec::new(),
            comment: String::new(),
            directories: HashMap::new(),
            launch_command: HashMap::new(),
        }
    }

    // On a blank state, populate directories and launch_command hashmaps from saved file
    pub fn build(&mut self, directories: String, launchcommands: String) {
        // populate from directories file into state.directories
        if let Ok(lines) = read_lines(directories) {
            for line in lines.flatten() {
                let mut kv_pair: Vec<String> = Vec::new();
                for keyvalue in line.split_whitespace() {
                    kv_pair.push(keyvalue.to_string());
                }
                self.directories.insert(String::from(&kv_pair[0]), String::from(&kv_pair[1]));
            }
        }
        // populate from launchcommands file into state.launch_command
        if let Ok(lines) = read_lines(launchcommands) {
            for line in lines.flatten() {
                let mut kv_pair: Vec<String> = Vec::new();
                for keyvalue in line.split_whitespace() {
                    kv_pair.push(keyvalue.to_string());
                }
                self.launch_command.insert(String::from(&kv_pair[0]), String::from(&kv_pair[1]));
            }
        }

        let _ = File::options().write(true).create(true).open("ff_directories.txt");
        let _ = File::options().write(true).create(true).open("ff_launchcommands.txt");
    }

    // Update state with corresponding options: main_menu(), directories().
    // called from the aforementioned functions.
    fn update(&mut self, status: &str) {
        self.menu = String::from(status);
        self.update_commands()
    }

    // Update command list display following change to State's 'menu' field.
    // called from update()
    fn update_commands(&mut self) {
        match self.menu.as_str() {
            "MainMenu" => {
                self.comment = String::from("Select Action\n[l] - List saved directories\n[d] - List default opening process\n[r] - Refresh saved directories\n[q] - Quit\n\n");
                self.commands = Vec::from(["l".to_string(), "d".to_string(), "r".to_string(), "q".to_string()]);
            },
            "Directories" => {
                self.comment = String::from("Select Action\n[o] - Open file endpoint/number\n[s] - Change sort (current: last modified)\n[n] - New Directory\n[d] - Delete directory\n[r] - Return to main menu\n[q] - Quit\n\n");
                self.commands = vec!["o".to_string(), "s".to_string(), "n".to_string(),  "d".to_string(), "r".to_string(), "q".to_string()];
            },
            "LaunchCommands" => {
                self.comment = String::from("Select Action\n[n] - New Launch Command\n[d] - Delete Launch Command\n[r] - Return to main menu\n[q] - Quit\n\n");
                self.commands = vec!["n".to_string(), "d".to_string(), "r".to_string(), "q".to_string()];
            },
            &_ => ()
        };
    }

    // returns list of available commands following a call from State change functions
    fn print_commands(&self) -> &String {
        &self.comment
    }

    // adds a new directory and endpoint to the hashmap of status.directories
    fn new_directory(&mut self) {
        // initialize text input variables
        let mut text_entry = String::new();
        let mut commands: Vec<String> = Vec::new();

        // preparing IO
        print!("\nPlease enter a new filepath...\n> ");
        io::stdout().flush().expect("Failed to flush");
        io::stdin()
            .read_line(&mut text_entry)
            .expect("Failed to read line");

        // tokenizing commands
        for word in text_entry.split_whitespace() {
            commands.push(String::from(word));
        }

        // declaring regex for use in the for loop, documentation said its 'expensive' to declare
        // so its only done once.       
        let re_endpoint = Regex::new(r"(?<endpoint>[[:word:]]+\.[[:word:]]+)").unwrap();
        let re_ext = Regex::new(r"(?<ext>\.[[:word:]]+\w$)").unwrap();
        
        // this loop captures the endpoint and extension for every inputted directory, then adds
        // them to their respective hashmaps in state.directories and state.launch_command
        for directory in commands.iter() {

            // endpoints are captured and taken out of their Option() state.
            let cap = re_endpoint.captures(directory).and_then(|cap| {
                cap.name("endpoint").map(|endpoint| endpoint.as_str())
            });
            let endpoint = match cap {
                Some(endp) => endp,
                None => "",
            };

            // extensions are captured and taken out of their Option() state
            let cap = re_ext.captures(directory).and_then(|cap| {
                cap.name("ext").map(|ext| ext.as_str())
            });
            let extension = match cap {
                Some(ex) => ex,
                None => "",
            };
            
            // directory:endpoint key-value pairs are inserted to the hashmap
            self.directories.insert(String::from(directory), String::from(endpoint));
            println!("\n{} added and saved to directories...", endpoint);

            // creating vector list of known (registered) extensions for checking later
            let mut registered_extensions: Vec<&str> = Vec::new();

            for (reg_ext, _process) in &self.launch_command {
                registered_extensions.push(reg_ext);
            }

            // if the extension is not registered, register with a command and add to hashmap
            if !registered_extensions.contains(&extension) {
                println!("New filetype discovered, what process would you like to open '{}' files with?", extension);

                // initialize text input variables
                let mut launch_command = String::new();

                // preparing IO
                print!("> ");
                io::stdout().flush().expect("Failed to flush");
                io::stdin()
                    .read_line(&mut launch_command)
                    .expect("Failed to read line");

                let launch_command = launch_command.trim_end();

                println!("\n'{}' file will now be opened with '{}' by default, use 'd' from the default opening process menu to change.", extension, launch_command);
                self.launch_command.insert(String::from(extension), String::from(launch_command));

            }

        }

        // write changes to file "ff_directories.txt" which allows for cross-instance usage
        fs::remove_file("ff_directories.txt").unwrap();
        let mut file = OpenOptions::new()
                .write(true)
                .create(true)
                .open("ff_directories.txt")
                .expect("Unable to open file");
        
        for (directory, endpoint) in &self.directories { // issue: currently rewrites entire storage when saving as a redundancy against overwrites
            let data = format!("{directory} {endpoint}\n");
            file.write(data.as_bytes()).expect("Unable to write to directories");
        }

        // write changes to file "ff_launchcommands.txt" which allows for cross-instance usage
        fs::remove_file("ff_launchcommands.txt").unwrap();
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .open("ff_launchcommands.txt")
            .expect("Unable to open file");

        for (extension, commands) in &self.launch_command { // same issue
            let data = format!("{extension} {commands}\n");
            file.write(data.as_bytes()).expect("Unable to write to launchcommands");
        }

        // after waiting, return to main menu
        thread::sleep(time::Duration::from_secs(2));
        std::process::Command::new("clear").status().unwrap();
        self.directories();
    }

    fn new_launchcommand(&mut self) {
        // initialize text input variables
        let mut new_extension = String::new();
        let mut launch_command = String::new();

        // preparing IO
        print!("\nPlease enter a file extension...\n> ");
        io::stdout().flush().expect("Failed to flush");
        io::stdin()
            .read_line(&mut new_extension)
            .expect("Failed to read line");

        let new_extension = new_extension.trim_end();

        print!("\nWhat command would you like to open '{new_extension}' files with?\n> ");
        io::stdout().flush().expect("Failed to flush");
        io::stdin()
            .read_line(&mut launch_command)
            .expect("Failed to read line");

        let launch_command = launch_command.trim_end();

        println!("\n'{}' files will now be opened with '{}' by default, use 'd' from the main menu to change.", new_extension, launch_command);
        self.launch_command.insert(String::from(new_extension), String::from(launch_command));

        // write changes to file "ff_launchcommands.txt" which allows for cross-instance usage
        fs::remove_file("ff_launchcommands.txt").unwrap();
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .open("ff_launchcommands.txt")
            .expect("Unable to open file");

        for (extension, commands) in &self.launch_command { // same issue
            let data = format!("{extension} {commands}\n");
            file.write(data.as_bytes()).expect("Unable to write to launchcommands");
        }

        // after waiting, return to directory listing
        thread::sleep(time::Duration::from_secs(2));
        std::process::Command::new("clear").status().unwrap();
        self.launch_commands();

    }

    fn delete_directory(&mut self) {
        
        // initialize text input variables
        let mut text_entry = String::new();
        let mut commands: Vec<String> = Vec::new();

        // preparing IO
        print!("\nSelect directory number or file name to delete...\n");
        print!("> ");
        io::stdout().flush().expect("Failed to flush");
        io::stdin()
            .read_line(&mut text_entry)
            .expect("Failed to read line");


        // tokenizing commands
        for word in text_entry.split_whitespace() {
            commands.push(String::from(word));
        }

        // because of the borrow-checker, use this variable to queue up a directory to be deleted
        let mut tobe_deleted = String::new();

        // for every directory (command), check against the endpoint (value) and remove by directory (key). Once its found, break the loop
        for command in commands.iter() {
            for (key, value) in &self.directories {
                if value == command {
                    println!("\nDeleteing '{}' from registry... (fast_files does not truly delete files)", command);
                    tobe_deleted = key.to_string();
                    break;
                } 
            }
            if tobe_deleted == "" {
                println!("\nDirectory not found");
            }
            self.directories.remove(&tobe_deleted);
        }

        // write changes to file "ff_launchcommands.txt" which allows for cross-instance usage
        fs::remove_file("ff_directories.txt").unwrap();
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .open("ff_directories.txt")
            .expect("Unable to open file");

        for (directory, endpoint) in &self.directories { // same issue
            let data = format!("{directory} {endpoint}\n");
            file.write(data.as_bytes()).expect("Unable to write to directory");
        }

        // after waiting, return to directory listing
        thread::sleep(time::Duration::from_secs(2));
        std::process::Command::new("clear").status().unwrap();
        self.directories();
    }

    fn delete_launchcommand(&mut self) {

        // initialize text input variables
        let mut text_entry = String::new();
        let mut commands: Vec<String> = Vec::new();

        // preparing IO
        print!("\nSelect launch command to delete...\n");
        print!("> ");
        io::stdout().flush().expect("Failed to flush");
        io::stdin()
            .read_line(&mut text_entry)
            .expect("Failed to read line");

        // tokenizing commands
        for word in text_entry.split_whitespace() {
            commands.push(String::from(word));
        }

        // because of the borrow-checker, use this variable to queue up a directory to be deleted
        let mut tobe_deleted = String::new();

        // for every directory (command), check against the endpoint (value) and remove by directory (key). Once its found, break the loop
        for command in commands.iter() {
            for (key, _value) in &self.launch_command {
                if key  == command {
                    println!("\nDeleteing '{}' from registry... (fast_files does not truly delete files)", command);
                    tobe_deleted = key.to_string();
                    break;
                }
            }
            if tobe_deleted == "" {
                println!("\nDirectory not found");
            }
            self.launch_command.remove(&tobe_deleted);
        }

        // write changes to file "ff_directories.txt" which allows for cross-instance usage
        fs::remove_file("ff_launchcommands.txt").unwrap();
        let mut file = OpenOptions::new()
                .write(true)
                .create(true)
                .open("ff_launchcommands.txt")
                .expect("Unable to open file");

        for (extension, commands) in &self.launch_command { // same issue
            let data = format!("{extension} {commands}\n");
            file.write(data.as_bytes()).expect("Unable to write to launchcommands");
        }

        // after waiting, return to directory listing
        thread::sleep(time::Duration::from_secs(2));
        std::process::Command::new("clear").status().unwrap();
        self.launch_commands();
    }

    fn open_directory(&mut self) {
        // initialize text input variables
        let mut ex_endpoint = String::new();

        // initialize execution fields
        let mut ex_command = String::new(); 
        let mut ex_filepath = String::new();

        // preparing IO
        print!("\nPlease enter a file or file number to open...\n> ");
        io::stdout().flush().expect("Failed to flush");
        io::stdin()
            .read_line(&mut ex_endpoint)
            .expect("Failed to read line");

        let ex_endpoint = ex_endpoint.trim_end();

        // finding extension from ex_endpoint
        let re_ext = Regex::new(r"(?<ext>\.[[:word:]]+)").unwrap();

        let cap = re_ext.captures(ex_endpoint).and_then(|cap| {
            cap.name("ext").map(|ext| ext.as_str())
        });
        let ex_extension= match cap {
            Some(ex) => ex,
            None => "",
        };

        // finding launch command from extension
        for (extension, launch_command) in &self.launch_command {
            if extension == ex_extension {
                ex_command = launch_command.to_string();
            }
        }

        // finding filepath from endpoint
        for (filepath, endpoint) in &self.directories {
            if endpoint == ex_endpoint {
                ex_filepath = filepath.to_string();
            }
        }

        // exiting message
        println!("\nOpening {ex_endpoint} with {ex_command}...");
        thread::sleep(time::Duration::from_secs(2));

        // execute file opening
        Command::new(ex_command).arg(ex_filepath).status().expect("Couldn't open file");
    }

    fn refresh_list(&mut self) {
        // clears current directories and launch_command hashmaps
        self.directories = HashMap::new();
        self.launch_command = HashMap::new();

        // rebuilds from file
        // note: the use case of this is if the files are changed while the program is running
        self.build("ff_directories.txt".to_string(), "ff_launchcommands.txt".to_string());

        // after waiting, return to main menu
        println!("\nRefreshing directories and launch commands from file...");
        thread::sleep(time::Duration::from_secs(2));
        std::process::Command::new("clear").status().unwrap();
        print!("{}", self.print_commands());
        poll_commands(self);

    }

    // State change functions
    //
    // Changes State to main menu
    pub fn main_menu(&mut self) {
        self.update("MainMenu");
        std::process::Command::new("clear").status().unwrap();
        start_splash();
        print!("{}", self.print_commands());
        poll_commands(self);
    }

    // Changes State to directories
    pub fn directories(&mut self) {
        self.update("Directories");
        std::process::Command::new("clear").status().unwrap();

        // prints directories from state.directories hashmap
        println!("Listing Saved Directories...");

        let mut count: i32 = 1;
        for (_filepath, endpoint) in &self.directories {
            println!("{}. {}", count, endpoint);
            count += 1;
        }

        print!("\n{}", self.print_commands());
        poll_commands(self);
    }

    // Changes State to directories
    pub fn launch_commands(&mut self) {
        self.update("LaunchCommands");
        std::process::Command::new("clear").status().unwrap();

        // prints directories from state.directories hashmap
        println!("Listing Saved Opening Actions...");

        let mut count: i32 = 1;
        for (extension, launchcommand) in &self.launch_command{
            println!("{}. {} -> {}", count, extension, launchcommand);
            count += 1;
        }

        print!("\n{}", self.print_commands());
        poll_commands(self);
    }

}

fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
    let file = File::open(filename)?;
    Ok(io::BufReader::new(file).lines())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn generate_state() {
        let mut state = State::new();
        assert_eq!(state.menu, "", "testing menu State creation");
    }

    #[test]
    fn generate_commands() {
        let mut state = State::new();
        assert_eq!(state.commands, "", "testing commands State creation");
    }

    #[test]
    fn change_main_menu() {
        let mut state = State::new();
        state.directories();
        state.main_menu();
        assert_eq!(state.menu, "MainMenu", "testing if fn main_menu() acts correctly");
    }

    #[test]
    fn change_directories() {
        let mut state = State::new();
        state.main_menu();
        state.directories();
        assert_eq!(state.menu, "Directories", "testing if fn directories() acts correctly");
    }

    #[test]
    fn display_correct_commands() {
        let mut state = State::new();
        state.main_menu();
        assert_eq!(state.commands,
                   "[l] - List saved directories\n[o] - Open file\n[n] - New Directory\n[r] - Refresh saved directories\n[d] - Default opening process\n[q] - Quit\n",
                   "testing if commands are selected correctly")
    }
}