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
use std::fs;
use std::io;
use std::path::PathBuf;

fn search_term(args: &[String]) -> String {
    args.iter()
        .map(|x| x.to_lowercase())
        .collect::<Vec<String>>()
        .join("-")
}

pub fn list_notes(path: &PathBuf) -> io::Result<()> {
    read_notes_dir(path)
        .iter()
        .for_each(|entry| println!("{}", entry.path().display()));
    Ok(())
}

pub fn search_notes(path: &PathBuf, args: &[String]) -> io::Result<()> {
    let search = search_term(args);

    read_notes_dir(path)
        .iter()
        .filter(|entry| entry.file_name().into_string().unwrap().contains(&search))
        .for_each(|entry| println!("{}", entry.path().display()));

    Ok(())
}

pub fn print_notes(path: &PathBuf, args: &[String]) -> io::Result<()> {
    let search = search_term(args);
    read_notes_dir(path)
        .iter()
        .filter(|entry| entry.file_name().into_string().unwrap().contains(&search))
        .for_each(|entry| {
            let contents =
                fs::read_to_string(entry.path()).expect("Something went wrong reading the file");
            println!("{}", contents);
        });

    Ok(())
}

fn read_notes_dir(path: &PathBuf) -> Vec<fs::DirEntry> {
    fs::read_dir(path).unwrap().map(|e| e.unwrap()).collect()
}