aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/content.rs51
-rw-r--r--src/main.rs32
2 files changed, 44 insertions, 39 deletions
diff --git a/src/content.rs b/src/content.rs
index 0f81149..ed1d8c5 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -9,37 +9,36 @@ use cursive::theme::Effect;
9use cursive::utils::markup::StyledString; 9use cursive::utils::markup::StyledString;
10use cursive::views::Dialog; 10use cursive::views::Dialog;
11use cursive::Cursive; 11use cursive::Cursive;
12use reqwest::Url;
12use serde_json::Value; 13use serde_json::Value;
13use CONFIGURATION; 14use CONFIGURATION;
14 15
15pub fn query_url_gen(title: &str) -> String { 16pub fn query_url_gen(title: &str) -> Url {
16 // query config 17 Url::parse_with_params(
17 let mut url = CONFIGURATION.wiki_url.clone(); 18 &(CONFIGURATION.wiki_url.clone() + "/w/api.php"),
18 url.push_str("/w/api.php?"); 19 &[
19 url.push_str("action=query&"); 20 ("action", "query"),
20 url.push_str("format=json&"); 21 ("format", "json"),
21 url.push_str("prop=extracts%7Clinks&"); 22 ("prop", "extracts|links"),
22 url.push_str("indexpageids=1&"); 23 ("indexpageids", "1"),
23 url.push_str("titles="); 24 ("titles", &urlencoding::encode(&title.replace(" ", "_"))),
24 url.push_str(&urlencoding::encode(title)); 25 ("redirects", "1"),
25 url.push_str("&"); 26 ("pllimit", "100"),
26 url.push_str("redirects=1&"); 27 ("explaintext", "1"),
27 url.push_str("pllimit=100&"); 28 ],
28 url.push_str("explaintext=1"); 29 ).unwrap()
29 url
30} 30}
31 31
32pub fn search_url_gen(search: &str) -> String { 32pub fn search_url_gen(search: &str) -> Url {
33 // search config 33 Url::parse_with_params(
34 let mut url = CONFIGURATION.wiki_url.clone(); 34 &(CONFIGURATION.wiki_url.clone() + "/w/api.php"),
35 url.push_str("/w/api.php?"); 35 &[
36 url.push_str("action=opensearch&"); 36 ("action", "opensearch"),
37 url.push_str("format=json&"); 37 ("format", "json"),
38 url.push_str("search="); 38 ("search", &urlencoding::encode(search)),
39 url.push_str(&urlencoding::encode(&search)); 39 ("limit", "20"),
40 url.push_str("&"); 40 ],
41 url.push_str("limit=20"); 41 ).unwrap()
42 url
43} 42}
44 43
45pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> { 44pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> {
diff --git a/src/main.rs b/src/main.rs
index 85d25e7..25e6e32 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,7 +6,9 @@ extern crate serde_json;
6extern crate lazy_static; 6extern crate lazy_static;
7 7
8use cursive::traits::*; 8use cursive::traits::*;
9use cursive::views::{Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView}; 9use cursive::views::{
10 Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView,
11};
10use cursive::Cursive; 12use cursive::Cursive;
11 13
12use serde_json::Value; 14use serde_json::Value;
@@ -28,8 +30,6 @@ lazy_static! {
28} 30}
29 31
30fn main() { 32fn main() {
31 parse_arguments();
32
33 // Initial setup 33 // Initial setup
34 let mut main = Cursive::default(); 34 let mut main = Cursive::default();
35 35
@@ -39,6 +39,12 @@ fn main() {
39 main.add_global_callback('q', |s| s.quit()); 39 main.add_global_callback('q', |s| s.quit());
40 main.add_global_callback('s', |s| search(s)); 40 main.add_global_callback('s', |s| search(s));
41 41
42 main.add_layer(TextView::new(
43 "Hit s to search
44Hit q to quit
45Hit t to pop layer",
46 ));
47
42 main.run(); 48 main.run();
43} 49}
44 50
@@ -66,7 +72,10 @@ fn parse_arguments() -> Configuration {
66 std::process::exit(0); 72 std::process::exit(0);
67 } 73 }
68 74
69 let lang = matches.value_of("lang").unwrap_or("en").to_string(); 75 let lang = matches
76 .value_of("lang")
77 .or(option_env!("LANG").map(|s| s.split_at(2).0))
78 .unwrap_or("en");
70 let wiki_url = matches 79 let wiki_url = matches
71 .value_of("URL") 80 .value_of("URL")
72 .unwrap_or(&format!("https://{}.wikipedia.org", lang)) 81 .unwrap_or(&format!("https://{}.wikipedia.org", lang))
@@ -104,7 +113,8 @@ fn search(s: &mut Cursive) {
104 Dialog::around(EditView::new().on_submit(go).with_id("search")) 113 Dialog::around(EditView::new().on_submit(go).with_id("search"))
105 .title("Search for a page") 114 .title("Search for a page")
106 .button("Go", |s| { 115 .button("Go", |s| {
107 let search_txt = s.call_on_id("search", |v: &mut EditView| v.get_content()) 116 let search_txt = s
117 .call_on_id("search", |v: &mut EditView| v.get_content())
108 .unwrap(); 118 .unwrap();
109 go(s, &search_txt); 119 go(s, &search_txt);
110 }) 120 })
@@ -117,16 +127,12 @@ fn search(s: &mut Cursive) {
117 127
118fn on_submit(s: &mut Cursive, name: &str) { 128fn on_submit(s: &mut Cursive, name: &str) {
119 // get article data 129 // get article data
120 let heading: String = name.to_string(); 130 let url = query_url_gen(name);
121 let url = query_url_gen(&name.replace(" ", "_"));
122 let mut extract = String::new(); 131 let mut extract = String::new();
123 let mut link_vec: Vec<String> = vec![]; 132 let mut link_vec: Vec<String> = vec![];
124 133
125 let mut res = reqwest::get(&url).unwrap(); 134 let mut res = reqwest::get(url).unwrap();
126 let v: Value = match serde_json::from_str(&res.text().unwrap()) { 135 let v: Value = res.json().expect("Failed to parse json");
127 Ok(x) => x,
128 Err(x) => panic!("Failed to parse json\nReceived error {}", x),
129 };
130 136
131 match get_extract(&v) { 137 match get_extract(&v) {
132 Ok(x) => extract = x, 138 Ok(x) => extract = x,
@@ -156,6 +162,6 @@ fn on_submit(s: &mut Cursive, name: &str) {
156 ).on_event('t', |s| match s.pop_layer() { 162 ).on_event('t', |s| match s.pop_layer() {
157 _ => (), 163 _ => (),
158 }), 164 }),
159 ).title(heading), 165 ).title(name),
160 ); 166 );
161} 167}