From c417c1efe7a93b6e871f2b2c85296a3a3008af54 Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Mon, 30 Jul 2018 09:10:37 +0300 Subject: Rustfmt-ed source files --- src/content.rs | 59 ++++++++++++++--------------- src/main.rs | 115 +++++++++++++++++++++++++++++---------------------------- 2 files changed, 86 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/content.rs b/src/content.rs index d767f9c..ff01342 100644 --- a/src/content.rs +++ b/src/content.rs @@ -1,15 +1,15 @@ -extern crate reqwest; -extern crate serde_json; extern crate cursive; extern crate regex; +extern crate reqwest; +extern crate serde_json; extern crate urlencoding; +use self::regex::Regex; use cursive::theme::Effect; use cursive::utils::markup::StyledString; -use cursive::Cursive; use cursive::views::Dialog; +use cursive::Cursive; use serde_json::Value; -use self::regex::Regex; use CONFIGURATION; pub fn query_url_gen(title: &str) -> String { @@ -57,36 +57,33 @@ pub fn get_extract(v: &Value) -> Result { Ok(format!("{}", extract)) } // ignore non strings - _ => Ok(format!("This page does not exist anymore")) + _ => Ok(format!("This page does not exist anymore")), } } pub fn extract_formatter(extract: String) -> StyledString { let mut formatted = StyledString::new(); - let heading= Regex::new(r"^== (?P.*) ==$").unwrap(); - let subheading= Regex::new(r"^=== (?P.*) ===$").unwrap(); - let subsubheading= Regex::new(r"^==== (?P.*) ====$").unwrap(); + let heading = Regex::new(r"^== (?P.*) ==$").unwrap(); + let subheading = Regex::new(r"^=== (?P.*) ===$").unwrap(); + let subsubheading = Regex::new(r"^==== (?P.*) ====$").unwrap(); for line in extract.lines() { if heading.is_match(line) { - formatted.append( - StyledString::styled( - heading.replace(line, "$d"), Effect::Bold - ) - ); + formatted.append(StyledString::styled( + heading.replace(line, "$d"), + Effect::Bold, + )); } else if subheading.is_match(line) { - formatted.append( - StyledString::styled( - subheading.replace(line, "$d"), Effect::Italic - ) - ); + formatted.append(StyledString::styled( + subheading.replace(line, "$d"), + Effect::Italic, + )); } else if subsubheading.is_match(line) { - formatted.append( - StyledString::styled( - subsubheading.replace(line, "$d"), Effect::Underline - ) - ); + formatted.append(StyledString::styled( + subsubheading.replace(line, "$d"), + Effect::Underline, + )); } else { formatted.append(StyledString::plain(line)); } @@ -100,10 +97,9 @@ pub fn extract_formatter(extract: String) -> StyledString { pub fn get_search_results(search: &str) -> Result, reqwest::Error> { let url = search_url_gen(search); let mut res = reqwest::get(&url[..])?; - let v: Value = serde_json::from_str(&res.text()?) - .unwrap_or_else( |e| { - panic!("Recieved error {:?}", e); - } ); + let v: Value = serde_json::from_str(&res.text()?).unwrap_or_else(|e| { + panic!("Recieved error {:?}", e); + }); let mut results: Vec = vec![]; for item in v[1].as_array().unwrap() { @@ -129,19 +125,18 @@ pub fn get_links(v: &Value) -> Result, reqwest::Error> { for item in arr { match item["title"] { Value::String(ref title) => links.push(title.to_string()), - _ => links.push(String::from("lol")) + _ => links.push(String::from("lol")), } } - }, - _ => links.push(String::from("lol")) + } + _ => links.push(String::from("lol")), }; Ok(links) } pub fn pop_error(s: &mut Cursive, msg: String) { - s.add_layer(Dialog::text(format!("{}", msg)) - .button("Ok", |s| s.quit())); + s.add_layer(Dialog::text(format!("{}", msg)).button("Ok", |s| s.quit())); } pub fn handler(e: reqwest::Error) -> String { diff --git a/src/main.rs b/src/main.rs index ab35f2d..8b7fdca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,19 @@ +extern crate clap; +extern crate cursive; extern crate reqwest; extern crate serde_json; -extern crate cursive; -extern crate clap; #[macro_use] extern crate lazy_static; -use cursive::Cursive; use cursive::traits::*; -use cursive::views::{ - TextView, Dialog, EditView, - SelectView, OnEventView, LinearLayout, - DummyView +use cursive::views::{ + Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView, }; +use cursive::Cursive; use serde_json::Value; -use clap::{Arg, App}; +use clap::{App, Arg}; pub mod content; use content::*; @@ -24,7 +22,7 @@ pub mod theme; use theme::*; struct Configuration { - wiki_url: String + wiki_url: String, } lazy_static! { @@ -33,14 +31,13 @@ lazy_static! { fn main() { parse_arguments(); - + // Initial setup let mut main = Cursive::default(); // Set theme main.set_theme(theme_gen()); - main.add_global_callback('q', |s| s.quit()); main.add_global_callback('s', |s| search(s)); @@ -52,15 +49,19 @@ fn parse_arguments() -> Configuration { .version("0.1.0") .author("NerdyPepper") .about("TUI MediaWiki browser") - .arg(Arg::with_name("URL") - .help("The URL of the wiki to be viewed") - .index(1)) - .arg(Arg::with_name("lang") - .short("l") - .long("lang") - .value_name("CODE") - .help("Choose the language for Wikipedia") - .takes_value(true)) + .arg( + Arg::with_name("URL") + .help("The URL of the wiki to be viewed") + .index(1), + ) + .arg( + Arg::with_name("lang") + .short("l") + .long("lang") + .value_name("CODE") + .help("Choose the language for Wikipedia") + .takes_value(true), + ) .get_matches(); if matches.is_present("HELP") { @@ -68,50 +69,52 @@ fn parse_arguments() -> Configuration { } let lang = matches.value_of("lang").unwrap_or("en").to_string(); - let wiki_url = matches.value_of("URL").unwrap_or(&format!("https://{}.wikipedia.org", lang)).to_string(); + let wiki_url = matches + .value_of("URL") + .unwrap_or(&format!("https://{}.wikipedia.org", lang)) + .to_string(); - Configuration { - wiki_url - } + Configuration { wiki_url } } 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)), + Err(e) => pop_error(s, handler(e)), }; let choose_result = SelectView::::new() .with_all_str(result) - .on_submit(|s, name|{ + .on_submit(|s, name| { s.pop_layer(); on_submit(s, name); }) .scrollable(); - s.add_layer(Dialog::around(choose_result) - .title("Search Results") - .button("Cancel", |s| match s.pop_layer() { _ => () }) - .fixed_size(( 45,10 ))); + s.add_layer( + Dialog::around(choose_result) + .title("Search Results") + .button("Cancel", |s| match s.pop_layer() { + _ => (), + }) + .fixed_size((45, 10)), + ); } - s.add_layer(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() - }).unwrap(); - go(s, &search_txt); - }) - .button("Cancel", |s| match s.pop_layer(){ - _ => () - }) - .fixed_size(( 35, 5 ))); + s.add_layer( + 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()) + .unwrap(); + go(s, &search_txt); + }) + .button("Cancel", |s| match s.pop_layer() { + _ => (), + }) + .fixed_size((35, 5)), + ); } fn on_submit(s: &mut Cursive, name: &str) { @@ -129,11 +132,11 @@ fn on_submit(s: &mut Cursive, name: &str) { match get_extract(&v) { Ok(x) => extract = x, - Err(e) => pop_error(s, handler(e)) + Err(e) => pop_error(s, handler(e)), }; match get_links(&v) { Ok(x) => link_vec = x, - Err(e) => pop_error(s, handler(e)) + Err(e) => pop_error(s, handler(e)), }; // get the act together @@ -149,12 +152,12 @@ fn on_submit(s: &mut Cursive, name: &str) { Dialog::around( OnEventView::new( LinearLayout::horizontal() - .child(article_content.fixed_width(72)) - .child(DummyView) - .child(links) - ) - .on_event('t', |s| match s.pop_layer() { _ => () }) - ) - .title(heading) - ); + .child(article_content.fixed_width(72)) + .child(DummyView) + .child(links), + ).on_event('t', |s| match s.pop_layer() { + _ => (), + }), + ).title(heading), + ); } -- cgit v1.2.3 From 1a776d7c872b99be3e5aa4576e710b85dca84f9e Mon Sep 17 00:00:00 2001 From: ZapAnton Date: Mon, 30 Jul 2018 09:14:21 +0300 Subject: Getting app name, version, author and description from Cargo.toml --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 8b7fdca..fd01af8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,10 +45,10 @@ fn main() { } fn parse_arguments() -> Configuration { - let matches = App::new("Taizen") - .version("0.1.0") - .author("NerdyPepper") - .about("TUI MediaWiki browser") + let matches = App::new(env!("CARGO_PKG_NAME")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .about(env!("CARGO_PKG_DESCRIPTION")) .arg( Arg::with_name("URL") .help("The URL of the wiki to be viewed") -- cgit v1.2.3