diff options
Diffstat (limited to 'src/content.rs')
-rw-r--r-- | src/content.rs | 59 |
1 files changed, 27 insertions, 32 deletions
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 @@ | |||
1 | extern crate reqwest; | ||
2 | extern crate serde_json; | ||
3 | extern crate cursive; | 1 | extern crate cursive; |
4 | extern crate regex; | 2 | extern crate regex; |
3 | extern crate reqwest; | ||
4 | extern crate serde_json; | ||
5 | extern crate urlencoding; | 5 | extern crate urlencoding; |
6 | 6 | ||
7 | use self::regex::Regex; | ||
7 | use cursive::theme::Effect; | 8 | use cursive::theme::Effect; |
8 | use cursive::utils::markup::StyledString; | 9 | use cursive::utils::markup::StyledString; |
9 | use cursive::Cursive; | ||
10 | use cursive::views::Dialog; | 10 | use cursive::views::Dialog; |
11 | use cursive::Cursive; | ||
11 | use serde_json::Value; | 12 | use serde_json::Value; |
12 | use self::regex::Regex; | ||
13 | use CONFIGURATION; | 13 | use CONFIGURATION; |
14 | 14 | ||
15 | pub fn query_url_gen(title: &str) -> String { | 15 | pub fn query_url_gen(title: &str) -> String { |
@@ -57,36 +57,33 @@ pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> { | |||
57 | Ok(format!("{}", extract)) | 57 | Ok(format!("{}", extract)) |
58 | } | 58 | } |
59 | // ignore non strings | 59 | // ignore non strings |
60 | _ => Ok(format!("This page does not exist anymore")) | 60 | _ => Ok(format!("This page does not exist anymore")), |
61 | } | 61 | } |
62 | } | 62 | } |
63 | 63 | ||
64 | pub fn extract_formatter(extract: String) -> StyledString { | 64 | pub fn extract_formatter(extract: String) -> StyledString { |
65 | let mut formatted = StyledString::new(); | 65 | let mut formatted = StyledString::new(); |
66 | 66 | ||
67 | let heading= Regex::new(r"^== (?P<d>.*) ==$").unwrap(); | 67 | let heading = Regex::new(r"^== (?P<d>.*) ==$").unwrap(); |
68 | let subheading= Regex::new(r"^=== (?P<d>.*) ===$").unwrap(); | 68 | let subheading = Regex::new(r"^=== (?P<d>.*) ===$").unwrap(); |
69 | let subsubheading= Regex::new(r"^==== (?P<d>.*) ====$").unwrap(); | 69 | let subsubheading = Regex::new(r"^==== (?P<d>.*) ====$").unwrap(); |
70 | 70 | ||
71 | for line in extract.lines() { | 71 | for line in extract.lines() { |
72 | if heading.is_match(line) { | 72 | if heading.is_match(line) { |
73 | formatted.append( | 73 | formatted.append(StyledString::styled( |
74 | StyledString::styled( | 74 | heading.replace(line, "$d"), |
75 | heading.replace(line, "$d"), Effect::Bold | 75 | Effect::Bold, |
76 | ) | 76 | )); |
77 | ); | ||
78 | } else if subheading.is_match(line) { | 77 | } else if subheading.is_match(line) { |
79 | formatted.append( | 78 | formatted.append(StyledString::styled( |
80 | StyledString::styled( | 79 | subheading.replace(line, "$d"), |
81 | subheading.replace(line, "$d"), Effect::Italic | 80 | Effect::Italic, |
82 | ) | 81 | )); |
83 | ); | ||
84 | } else if subsubheading.is_match(line) { | 82 | } else if subsubheading.is_match(line) { |
85 | formatted.append( | 83 | formatted.append(StyledString::styled( |
86 | StyledString::styled( | 84 | subsubheading.replace(line, "$d"), |
87 | subsubheading.replace(line, "$d"), Effect::Underline | 85 | Effect::Underline, |
88 | ) | 86 | )); |
89 | ); | ||
90 | } else { | 87 | } else { |
91 | formatted.append(StyledString::plain(line)); | 88 | formatted.append(StyledString::plain(line)); |
92 | } | 89 | } |
@@ -100,10 +97,9 @@ pub fn extract_formatter(extract: String) -> StyledString { | |||
100 | pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { | 97 | pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { |
101 | let url = search_url_gen(search); | 98 | let url = search_url_gen(search); |
102 | let mut res = reqwest::get(&url[..])?; | 99 | let mut res = reqwest::get(&url[..])?; |
103 | let v: Value = serde_json::from_str(&res.text()?) | 100 | let v: Value = serde_json::from_str(&res.text()?).unwrap_or_else(|e| { |
104 | .unwrap_or_else( |e| { | 101 | panic!("Recieved error {:?}", e); |
105 | panic!("Recieved error {:?}", e); | 102 | }); |
106 | } ); | ||
107 | 103 | ||
108 | let mut results: Vec<String> = vec![]; | 104 | let mut results: Vec<String> = vec![]; |
109 | for item in v[1].as_array().unwrap() { | 105 | for item in v[1].as_array().unwrap() { |
@@ -129,19 +125,18 @@ pub fn get_links(v: &Value) -> Result<Vec<String>, reqwest::Error> { | |||
129 | for item in arr { | 125 | for item in arr { |
130 | match item["title"] { | 126 | match item["title"] { |
131 | Value::String(ref title) => links.push(title.to_string()), | 127 | Value::String(ref title) => links.push(title.to_string()), |
132 | _ => links.push(String::from("lol")) | 128 | _ => links.push(String::from("lol")), |
133 | } | 129 | } |
134 | } | 130 | } |
135 | }, | 131 | } |
136 | _ => links.push(String::from("lol")) | 132 | _ => links.push(String::from("lol")), |
137 | }; | 133 | }; |
138 | 134 | ||
139 | Ok(links) | 135 | Ok(links) |
140 | } | 136 | } |
141 | 137 | ||
142 | pub fn pop_error(s: &mut Cursive, msg: String) { | 138 | pub fn pop_error(s: &mut Cursive, msg: String) { |
143 | s.add_layer(Dialog::text(format!("{}", msg)) | 139 | s.add_layer(Dialog::text(format!("{}", msg)).button("Ok", |s| s.quit())); |
144 | .button("Ok", |s| s.quit())); | ||
145 | } | 140 | } |
146 | 141 | ||
147 | pub fn handler(e: reqwest::Error) -> String { | 142 | pub fn handler(e: reqwest::Error) -> String { |