aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: a3cc71359b4bd33d10cdb483dbf8f770dbd6e4d5 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
extern crate reqwest;
extern crate serde_json;
extern crate cursive;

use cursive::Cursive;
use cursive::align::HAlign;
use cursive::traits::*;
use cursive::views::{TextView, Dialog, EditView, SelectView, LinearLayout, DummyView};

pub mod content;
use content::*;

fn main() {
    // Initial setup
    let mut main = Cursive::default();
    let mut articles_vec = vec![];

    main.add_global_callback('q', |s| s.quit());
    main.add_global_callback('s', |s| search(s));
    main.add_global_callback('t', |s| match s.pop_layer() {
        Some(_) => (),
        None => s.add_layer( Dialog::text("Stack is empty!")
                  .title("Error")
                )});

    main.run();
}

fn search(s: &mut Cursive){

    fn go(s: &mut Cursive, search: &str) {
        s.pop_layer();
        let mut result = vec![];
        match get_search_results(search) {
            Ok(x) => result = x,
            Err(e) => pop_error(s,handler(e)),
        };
        let choose_result = SelectView::<String>::new()
            .with_all_str(result)
            .on_submit(on_submit);
        s.add_layer(Dialog::around(choose_result)
                    .title("Search Results"));
    }

    s.add_layer(Dialog::around(EditView::new()
                               .on_submit(go)
                               .with_id("search")
                               .fixed_size(( 15,2 )))
                .title("Search for a page")
                .button("Go", |s| {
                    let search_txt = s.call_on_id( "search", |v: &mut EditView| {
                        v.get_content()
                    }).unwrap();
                    go(s, &search_txt);
                })
                .button("Cancel", |s| match s.pop_layer(){
                    _ => ()
                }));
}

fn on_submit(s: &mut Cursive, name: &String) {
    s.pop_layer();

    let heading: String = name.clone();
    let url = query_url_gen(&name.replace(" ", "_"));
    let res = reqwest::get(&url).unwrap();

    let mut extract = String::new();

    match get_extract(res) {
        Ok(x) => extract = x,
        Err(e) => pop_error(s, handler(e))
    };

    let article_stack = LinearLayout::horizontal()
        .child(TextView::new("Stack"))
        .child(DummyView.fixed_height(1));

    s.add_layer(Dialog::around(
            LinearLayout::horizontal()
            .child(
                LinearLayout::vertical()
                .child(TextView::new(heading).h_align(HAlign::Center))
                .child(DummyView.fixed_height(1))
                .child(TextView::new(extract))
                )
            .child(article_stack)
            )
               );
}