aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 22451ba988838afed4e6696914ddc9595758082b (plain)
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
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
pub mod vector;
pub mod text;
pub mod splitter;
pub mod filecache;
pub mod searchresult;
pub mod extractors;
pub mod index;
pub mod gui;

use index::Index;
use std::io::*;
use std::env;
use crate::index::GenState;

fn main() {
    let args: Vec<_> = env::args().collect();

    if args.len() > 1 {
        let cmd = args.get(1).unwrap();

        if cmd == "-g" {
            if args.len() != 4 {
                eprintln!("{} -g <input> <indexfile>", args.get(0).unwrap());
                return;
            }

            let input = args.get(2).unwrap();
            let file = args.get(3).unwrap();
            Index::generate(input, |t, p| {
                eprint!("\r\x1b[2K{}% ", p);
                match t {
                    GenState::Fetching => { eprint!("fetched") }
                    GenState::Parsing =>  { eprint!("parsed") }
                    GenState::Merging =>  { eprint!("merged") }
                };
                std::io::stdout().flush().ok();
            }).save(file.to_string());
        } else if cmd == "-s" {
            if args.len() < 4 {
                eprintln!("{} -s <indexfile> ...", args.get(0).unwrap());
                return;
            }

            let file = args.get(2).unwrap().clone();
            let v = args.get(3..(args.len())).unwrap();
            let search = v.join(" ");
            let searchvec = splitter::split_to_words(search);

            let idx = Index::from_file(&file);
            let results = idx.search(searchvec, |p| {
                eprint!("\r\x1b[2K{}% searched", p);
            });
            eprint!("\r\x1b[2K");
            for result in results {
                println!("{}", result.path);
            }
        } else if cmd == "-m" {
            if args.len() < 5 {
                eprintln!("{} -m <output> index...", args.get(0).unwrap());
                return;
            }

            let merged = args.get(2).unwrap().clone();
            let v : Vec<String> = args.get(3..(args.len())).unwrap().into();
            let indexes : Vec<Index> = v.iter().map(Index::from_file).collect();
            Index::merge(indexes,
                         |p| {
                             eprint!("\r\x1b[2K{}% merged", p);
                         }
                        ).save(merged);
        }
    } else {
        gui::run();
    }
}