Skip to main content

oneTenCubed_hey/
cli.rs

1use crate::{app::fatal, docs, editor, search, storage};
2use std::{
3    env,
4    io::{self, Write},
5};
6
7pub fn dispatcher() {
8    let args: Vec<String> = env::args().collect();
9
10    let _invoke = &args[0];
11    // Maybe needed in the future, like warnings for using deprecated invoke commands
12
13    if args.len() < 2 {
14        docs::help();
15        return;
16    }
17    let args: Vec<String> = args[1..].to_vec();
18
19    if args[0] == "." || args[0] == "--add" {
20        let title = if args.len() > 1 {
21            args[1..].join(" ")
22        } else {
23            get_title()
24        };
25
26        storage::new_article(title);
27    } else if &'-'
28        == match &args[0].chars().nth(0) {
29            Some(val) => val,
30            _ => &'+',
31        }
32    {
33        match args[0].as_str() {
34            "-h" | "--help" => {
35                docs::help();
36            }
37            "-v" | "--version" => {
38                docs::version();
39            }
40            "--help-verbose" => {
41                docs::help_verbose();
42            }
43            /*"-m" => {
44                todo!("Migrate functionality coming soon!");
45            }
46            "-l" => {
47                todo!("Link functionality coming soon!");
48            }
49            "-z" => {
50                todo!("Fuzzy searching coming soon!");
51            }
52            "-s" => {
53                todo!("Synonym/abbrevation searching coming soon!");
54            }*/
55            _ => {
56                println!("Invalid flag!\n");
57                docs::help();
58            }
59        }
60    } else {
61        search::search(args.join(" "));
62    }
63}
64
65// TODO: Improve title acceptance logic, make a format for title
66fn get_title() -> String {
67    print!("Enter a title: ");
68    io::stdout().flush().unwrap();
69
70    let mut title = String::new();
71    io::stdin().read_line(&mut title).expect("Error reading!");
72
73    let title: String = title.trim().parse().unwrap();
74    if title.is_empty() {
75        fatal("Invalid title!");
76    }
77    let title: String = title.to_lowercase();
78
79    title
80}
81
82pub fn search_result(result_arr: Vec<search::Field>) {
83    let mut input = String::new();
84    let title: String;
85
86    if result_arr.is_empty() {
87        println!("  No matches!");
88        return;
89    } else if result_arr.len() == 1 {
90        println!("  Exactly one match found: {}", result_arr[0].display_name);
91        title = result_arr[0].file.clone();
92    } else {
93        for (index, field) in result_arr.iter().enumerate() {
94            println!("  {}. {}", index + 1, field.display_name);
95        }
96
97        println!("");
98        loop {
99            print!(":: Enter file number to interact: ");
100            io::stdout().flush().unwrap();
101            input.clear();
102            match io::stdin().read_line(&mut input) {
103                Ok(_) => (),
104                Err(_) => {
105                    eprintln!("  Error reading!");
106                    continue;
107                }
108            };
109
110            input = input.trim().to_string();
111            if input == "" || input == "q" || input == "Q" {
112                return;
113            }
114
115            let n = match input.parse::<usize>() {
116                Ok(value) => value,
117                Err(_) => {
118                    eprintln!(
119                        "  Invalid input! Expected 1-{} (or press Enter to exit)",
120                        result_arr.len()
121                    );
122                    continue;
123                }
124            };
125
126            match n <= result_arr.len() {
127                true => {
128                    title = result_arr[n - 1].file.clone();
129                }
130                false => {
131                    eprintln!(
132                        "  Invalid input! Expected 1-{} (or press Enter to exit)",
133                        result_arr.len()
134                    );
135                    continue;
136                }
137            }
138
139            break;
140        }
141    }
142
143    input.clear();
144    print!(":: Display content (r) OR open in editor (w)? [R/w] ");
145    io::stdout().flush().unwrap();
146    io::stdin().read_line(&mut input).expect("  Error reading!");
147
148    match input.trim() {
149        "r" | "R" | "" => storage::read_article(title),
150        "w" | "W" => editor::open_editor(title),
151        _ => println!("  Invalid input!"),
152    }
153}