From a5c6f2afbe16d0328750f83db66b93b89111313b Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 10:27:42 +0800 Subject: Simplify response with json --- src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 85d25e7..a5c10e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,10 +123,7 @@ fn on_submit(s: &mut Cursive, name: &str) { let mut link_vec: Vec = vec![]; let mut res = reqwest::get(&url).unwrap(); - let v: Value = match serde_json::from_str(&res.text().unwrap()) { - Ok(x) => x, - Err(x) => panic!("Failed to parse json\nReceived error {}", x), - }; + let v: Value = res.json().expect("Failed to parse json"); match get_extract(&v) { Ok(x) => extract = x, -- cgit v1.2.3 From f0cb4fafbe4f3f1ecb23b4dd9d7357a269c1317e Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 10:31:14 +0800 Subject: Replace title in content --- src/content.rs | 2 +- src/main.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/content.rs b/src/content.rs index 0f81149..30d8f22 100644 --- a/src/content.rs +++ b/src/content.rs @@ -21,7 +21,7 @@ pub fn query_url_gen(title: &str) -> String { url.push_str("prop=extracts%7Clinks&"); url.push_str("indexpageids=1&"); url.push_str("titles="); - url.push_str(&urlencoding::encode(title)); + url.push_str(&urlencoding::encode(&title.replace(" ", "_"))); url.push_str("&"); url.push_str("redirects=1&"); url.push_str("pllimit=100&"); diff --git a/src/main.rs b/src/main.rs index a5c10e1..982550f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,8 +117,7 @@ fn search(s: &mut Cursive) { fn on_submit(s: &mut Cursive, name: &str) { // get article data - let heading: String = name.to_string(); - let url = query_url_gen(&name.replace(" ", "_")); + let url = query_url_gen(name); let mut extract = String::new(); let mut link_vec: Vec = vec![]; @@ -153,6 +152,6 @@ fn on_submit(s: &mut Cursive, name: &str) { ).on_event('t', |s| match s.pop_layer() { _ => (), }), - ).title(heading), + ).title(name), ); } -- cgit v1.2.3 From 25988fc5978f6c7dcdfae2ad36fdfd2bab965ed7 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 10:33:41 +0800 Subject: Format with clippy --- src/main.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 982550f..c6249c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,9 @@ extern crate serde_json; extern crate lazy_static; use cursive::traits::*; -use cursive::views::{Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView}; +use cursive::views::{ + Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView, +}; use cursive::Cursive; use serde_json::Value; @@ -104,7 +106,8 @@ fn search(s: &mut Cursive) { Dialog::around(EditView::new().on_submit(go).with_id("search")) .title("Search for a page") .button("Go", |s| { - let search_txt = s.call_on_id("search", |v: &mut EditView| v.get_content()) + let search_txt = s + .call_on_id("search", |v: &mut EditView| v.get_content()) .unwrap(); go(s, &search_txt); }) -- cgit v1.2.3 From 3a022c1bc9b8b92df0d2ef9977a7df5c1af1036b Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 14:01:42 +0800 Subject: Use reqwest::Url to generate params --- src/content.rs | 51 +++++++++++++++++++++++++-------------------------- src/main.rs | 2 +- 2 files changed, 26 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/content.rs b/src/content.rs index 30d8f22..ed1d8c5 100644 --- a/src/content.rs +++ b/src/content.rs @@ -9,37 +9,36 @@ use cursive::theme::Effect; use cursive::utils::markup::StyledString; use cursive::views::Dialog; use cursive::Cursive; +use reqwest::Url; use serde_json::Value; use CONFIGURATION; -pub fn query_url_gen(title: &str) -> String { - // query config - let mut url = CONFIGURATION.wiki_url.clone(); - url.push_str("/w/api.php?"); - url.push_str("action=query&"); - url.push_str("format=json&"); - url.push_str("prop=extracts%7Clinks&"); - url.push_str("indexpageids=1&"); - url.push_str("titles="); - url.push_str(&urlencoding::encode(&title.replace(" ", "_"))); - url.push_str("&"); - url.push_str("redirects=1&"); - url.push_str("pllimit=100&"); - url.push_str("explaintext=1"); - url +pub fn query_url_gen(title: &str) -> Url { + Url::parse_with_params( + &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), + &[ + ("action", "query"), + ("format", "json"), + ("prop", "extracts|links"), + ("indexpageids", "1"), + ("titles", &urlencoding::encode(&title.replace(" ", "_"))), + ("redirects", "1"), + ("pllimit", "100"), + ("explaintext", "1"), + ], + ).unwrap() } -pub fn search_url_gen(search: &str) -> String { - // search config - let mut url = CONFIGURATION.wiki_url.clone(); - url.push_str("/w/api.php?"); - url.push_str("action=opensearch&"); - url.push_str("format=json&"); - url.push_str("search="); - url.push_str(&urlencoding::encode(&search)); - url.push_str("&"); - url.push_str("limit=20"); - url +pub fn search_url_gen(search: &str) -> Url { + Url::parse_with_params( + &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), + &[ + ("action", "opensearch"), + ("format", "json"), + ("search", &urlencoding::encode(search)), + ("limit", "20"), + ], + ).unwrap() } pub fn get_extract(v: &Value) -> Result { diff --git a/src/main.rs b/src/main.rs index c6249c4..5057433 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,7 +124,7 @@ fn on_submit(s: &mut Cursive, name: &str) { let mut extract = String::new(); let mut link_vec: Vec = vec![]; - let mut res = reqwest::get(&url).unwrap(); + let mut res = reqwest::get(url).unwrap(); let v: Value = res.json().expect("Failed to parse json"); match get_extract(&v) { -- cgit v1.2.3 From 970bda5dd25060fea65da97c3bcbce11e48fc71a Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 14:02:21 +0800 Subject: Remove unneeded parse_arguments --- src/main.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 5057433..47efe6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,8 +30,6 @@ lazy_static! { } fn main() { - parse_arguments(); - // Initial setup let mut main = Cursive::default(); -- cgit v1.2.3 From 89b811bbadca81a386080f294c5e9cd811b3eac0 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 14:27:14 +0800 Subject: Add instructions in default layer --- src/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 47efe6f..90eed78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,12 @@ fn main() { main.add_global_callback('q', |s| s.quit()); main.add_global_callback('s', |s| search(s)); + main.add_layer(TextView::new( + "Hit s to search +Hit q to quit +Hit t to pop layer", + )); + main.run(); } -- cgit v1.2.3 From 8bf57f64b930b65367a5ddb072a13b1e9354c8f0 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 14:41:59 +0800 Subject: Check LANG before default to en --- src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 90eed78..25e6e32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,10 @@ fn parse_arguments() -> Configuration { std::process::exit(0); } - let lang = matches.value_of("lang").unwrap_or("en").to_string(); + let lang = matches + .value_of("lang") + .or(option_env!("LANG").map(|s| s.split_at(2).0)) + .unwrap_or("en"); let wiki_url = matches .value_of("URL") .unwrap_or(&format!("https://{}.wikipedia.org", lang)) -- cgit v1.2.3