diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 123 |
1 files changed, 63 insertions, 60 deletions
diff --git a/src/main.rs b/src/main.rs index ab35f2d..fd01af8 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,21 +1,19 @@ | |||
1 | extern crate clap; | ||
2 | extern crate cursive; | ||
1 | extern crate reqwest; | 3 | extern crate reqwest; |
2 | extern crate serde_json; | 4 | extern crate serde_json; |
3 | extern crate cursive; | ||
4 | extern crate clap; | ||
5 | #[macro_use] | 5 | #[macro_use] |
6 | extern crate lazy_static; | 6 | extern crate lazy_static; |
7 | 7 | ||
8 | use cursive::Cursive; | ||
9 | use cursive::traits::*; | 8 | use cursive::traits::*; |
10 | use cursive::views::{ | 9 | use cursive::views::{ |
11 | TextView, Dialog, EditView, | 10 | Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView, |
12 | SelectView, OnEventView, LinearLayout, | ||
13 | DummyView | ||
14 | }; | 11 | }; |
12 | use cursive::Cursive; | ||
15 | 13 | ||
16 | use serde_json::Value; | 14 | use serde_json::Value; |
17 | 15 | ||
18 | use clap::{Arg, App}; | 16 | use clap::{App, Arg}; |
19 | 17 | ||
20 | pub mod content; | 18 | pub mod content; |
21 | use content::*; | 19 | use content::*; |
@@ -24,7 +22,7 @@ pub mod theme; | |||
24 | use theme::*; | 22 | use theme::*; |
25 | 23 | ||
26 | struct Configuration { | 24 | struct Configuration { |
27 | wiki_url: String | 25 | wiki_url: String, |
28 | } | 26 | } |
29 | 27 | ||
30 | lazy_static! { | 28 | lazy_static! { |
@@ -33,14 +31,13 @@ lazy_static! { | |||
33 | 31 | ||
34 | fn main() { | 32 | fn main() { |
35 | parse_arguments(); | 33 | parse_arguments(); |
36 | 34 | ||
37 | // Initial setup | 35 | // Initial setup |
38 | let mut main = Cursive::default(); | 36 | let mut main = Cursive::default(); |
39 | 37 | ||
40 | // Set theme | 38 | // Set theme |
41 | main.set_theme(theme_gen()); | 39 | main.set_theme(theme_gen()); |
42 | 40 | ||
43 | |||
44 | main.add_global_callback('q', |s| s.quit()); | 41 | main.add_global_callback('q', |s| s.quit()); |
45 | main.add_global_callback('s', |s| search(s)); | 42 | main.add_global_callback('s', |s| search(s)); |
46 | 43 | ||
@@ -48,19 +45,23 @@ fn main() { | |||
48 | } | 45 | } |
49 | 46 | ||
50 | fn parse_arguments() -> Configuration { | 47 | fn parse_arguments() -> Configuration { |
51 | let matches = App::new("Taizen") | 48 | let matches = App::new(env!("CARGO_PKG_NAME")) |
52 | .version("0.1.0") | 49 | .version(env!("CARGO_PKG_VERSION")) |
53 | .author("NerdyPepper") | 50 | .author(env!("CARGO_PKG_AUTHORS")) |
54 | .about("TUI MediaWiki browser") | 51 | .about(env!("CARGO_PKG_DESCRIPTION")) |
55 | .arg(Arg::with_name("URL") | 52 | .arg( |
56 | .help("The URL of the wiki to be viewed") | 53 | Arg::with_name("URL") |
57 | .index(1)) | 54 | .help("The URL of the wiki to be viewed") |
58 | .arg(Arg::with_name("lang") | 55 | .index(1), |
59 | .short("l") | 56 | ) |
60 | .long("lang") | 57 | .arg( |
61 | .value_name("CODE") | 58 | Arg::with_name("lang") |
62 | .help("Choose the language for Wikipedia") | 59 | .short("l") |
63 | .takes_value(true)) | 60 | .long("lang") |
61 | .value_name("CODE") | ||
62 | .help("Choose the language for Wikipedia") | ||
63 | .takes_value(true), | ||
64 | ) | ||
64 | .get_matches(); | 65 | .get_matches(); |
65 | 66 | ||
66 | if matches.is_present("HELP") { | 67 | if matches.is_present("HELP") { |
@@ -68,50 +69,52 @@ fn parse_arguments() -> Configuration { | |||
68 | } | 69 | } |
69 | 70 | ||
70 | let lang = matches.value_of("lang").unwrap_or("en").to_string(); | 71 | let lang = matches.value_of("lang").unwrap_or("en").to_string(); |
71 | let wiki_url = matches.value_of("URL").unwrap_or(&format!("https://{}.wikipedia.org", lang)).to_string(); | 72 | let wiki_url = matches |
73 | .value_of("URL") | ||
74 | .unwrap_or(&format!("https://{}.wikipedia.org", lang)) | ||
75 | .to_string(); | ||
72 | 76 | ||
73 | Configuration { | 77 | Configuration { wiki_url } |
74 | wiki_url | ||
75 | } | ||
76 | } | 78 | } |
77 | 79 | ||
78 | fn search(s: &mut Cursive) { | 80 | fn search(s: &mut Cursive) { |
79 | |||
80 | fn go(s: &mut Cursive, search: &str) { | 81 | fn go(s: &mut Cursive, search: &str) { |
81 | s.pop_layer(); | 82 | s.pop_layer(); |
82 | let mut result = vec![]; | 83 | let mut result = vec![]; |
83 | match get_search_results(&search) { | 84 | match get_search_results(&search) { |
84 | Ok(x) => result = x, | 85 | Ok(x) => result = x, |
85 | Err(e) => pop_error(s,handler(e)), | 86 | Err(e) => pop_error(s, handler(e)), |
86 | }; | 87 | }; |
87 | let choose_result = SelectView::<String>::new() | 88 | let choose_result = SelectView::<String>::new() |
88 | .with_all_str(result) | 89 | .with_all_str(result) |
89 | .on_submit(|s, name|{ | 90 | .on_submit(|s, name| { |
90 | s.pop_layer(); | 91 | s.pop_layer(); |
91 | on_submit(s, name); | 92 | on_submit(s, name); |
92 | }) | 93 | }) |
93 | .scrollable(); | 94 | .scrollable(); |
94 | s.add_layer(Dialog::around(choose_result) | 95 | s.add_layer( |
95 | .title("Search Results") | 96 | Dialog::around(choose_result) |
96 | .button("Cancel", |s| match s.pop_layer() { _ => () }) | 97 | .title("Search Results") |
97 | .fixed_size(( 45,10 ))); | 98 | .button("Cancel", |s| match s.pop_layer() { |
99 | _ => (), | ||
100 | }) | ||
101 | .fixed_size((45, 10)), | ||
102 | ); | ||
98 | } | 103 | } |
99 | 104 | ||
100 | s.add_layer(Dialog::around(EditView::new() | 105 | s.add_layer( |
101 | .on_submit(go) | 106 | Dialog::around(EditView::new().on_submit(go).with_id("search")) |
102 | .with_id("search") | 107 | .title("Search for a page") |
103 | ) | 108 | .button("Go", |s| { |
104 | .title("Search for a page") | 109 | let search_txt = s.call_on_id("search", |v: &mut EditView| v.get_content()) |
105 | .button("Go", |s| { | 110 | .unwrap(); |
106 | let search_txt = s.call_on_id( "search", |v: &mut EditView| { | 111 | go(s, &search_txt); |
107 | v.get_content() | 112 | }) |
108 | }).unwrap(); | 113 | .button("Cancel", |s| match s.pop_layer() { |
109 | go(s, &search_txt); | 114 | _ => (), |
110 | }) | 115 | }) |
111 | .button("Cancel", |s| match s.pop_layer(){ | 116 | .fixed_size((35, 5)), |
112 | _ => () | 117 | ); |
113 | }) | ||
114 | .fixed_size(( 35, 5 ))); | ||
115 | } | 118 | } |
116 | 119 | ||
117 | fn on_submit(s: &mut Cursive, name: &str) { | 120 | fn on_submit(s: &mut Cursive, name: &str) { |
@@ -129,11 +132,11 @@ fn on_submit(s: &mut Cursive, name: &str) { | |||
129 | 132 | ||
130 | match get_extract(&v) { | 133 | match get_extract(&v) { |
131 | Ok(x) => extract = x, | 134 | Ok(x) => extract = x, |
132 | Err(e) => pop_error(s, handler(e)) | 135 | Err(e) => pop_error(s, handler(e)), |
133 | }; | 136 | }; |
134 | match get_links(&v) { | 137 | match get_links(&v) { |
135 | Ok(x) => link_vec = x, | 138 | Ok(x) => link_vec = x, |
136 | Err(e) => pop_error(s, handler(e)) | 139 | Err(e) => pop_error(s, handler(e)), |
137 | }; | 140 | }; |
138 | 141 | ||
139 | // get the act together | 142 | // get the act together |
@@ -149,12 +152,12 @@ fn on_submit(s: &mut Cursive, name: &str) { | |||
149 | Dialog::around( | 152 | Dialog::around( |
150 | OnEventView::new( | 153 | OnEventView::new( |
151 | LinearLayout::horizontal() | 154 | LinearLayout::horizontal() |
152 | .child(article_content.fixed_width(72)) | 155 | .child(article_content.fixed_width(72)) |
153 | .child(DummyView) | 156 | .child(DummyView) |
154 | .child(links) | 157 | .child(links), |
155 | ) | 158 | ).on_event('t', |s| match s.pop_layer() { |
156 | .on_event('t', |s| match s.pop_layer() { _ => () }) | 159 | _ => (), |
157 | ) | 160 | }), |
158 | .title(heading) | 161 | ).title(heading), |
159 | ); | 162 | ); |
160 | } | 163 | } |