aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--readme.md12
-rw-r--r--src/content.rs22
-rw-r--r--src/main.rs12
3 files changed, 21 insertions, 25 deletions
diff --git a/readme.md b/readme.md
index d6bfc3c..8d908d0 100644
--- a/readme.md
+++ b/readme.md
@@ -1,6 +1,6 @@
1![banner.png](https://0x0.st/sVMH.png) 1![banner.png](https://0x0.st/sVMH.png)
2 2
3Browse mediawiki pages from the command line. 3Browse mediawiki pages from the command line.
4 4
5## Installation 5## Installation
6This project uses [Cursive crate](https://github.com/gyscos/Cursive), so before installing `Taizen` 6This project uses [Cursive crate](https://github.com/gyscos/Cursive), so before installing `Taizen`
@@ -9,19 +9,19 @@ make sure you have installed necessary Cursive [dependencies](https://github.com
9```shell 9```shell
10git clone https://github.com/nerdypepper/taizen 10git clone https://github.com/nerdypepper/taizen
11cd taizen 11cd taizen
12cargo run 12cargo run --release
13``` 13```
14 14
15## Usage 15## Usage
16 16
17Taizen uses a **stack** like model. 17Taizen uses a **stack** like model.
18Articles are opened on new layers, pop a layer to go back. 18Articles are opened on new layers, pop a layer to go back.
19Hit `s` to search 19Hit `s` to search
20Hit `q` to quit 20Hit `q` to quit
21Hit `t` to pop a layer from the article stack 21Hit `t` to pop a layer from the article stack
22 22
23You can now view wikipedia pages in different languages, by passing the 23You can now view wikipedia pages in different languages, by passing the
24language code as a commandline arg. 24language code as a commandline arg.
25[List of language codes](https://en.wikipedia.org/wiki/List_of_Wikipedias#Detailed_list) 25[List of language codes](https://en.wikipedia.org/wiki/List_of_Wikipedias#Detailed_list)
26 26
27``` 27```
diff --git a/src/content.rs b/src/content.rs
index 8b48411..0f81149 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -54,14 +54,14 @@ pub fn get_extract(v: &Value) -> Result<String, reqwest::Error> {
54 // format to plain text 54 // format to plain text
55 let extract = extract.replace("\\\\", "\\"); 55 let extract = extract.replace("\\\\", "\\");
56 56
57 Ok(format!("{}", extract)) 57 Ok(extract.to_string())
58 } 58 }
59 // ignore non strings 59 // ignore non strings
60 _ => Ok(format!("This page does not exist anymore")), 60 _ => Ok("This page does not exist anymore".to_string()),
61 } 61 }
62} 62}
63 63
64pub fn extract_formatter(extract: String) -> StyledString { 64pub fn extract_formatter(extract: &str) -> 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();
@@ -103,10 +103,8 @@ pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> {
103 103
104 let mut results: Vec<String> = vec![]; 104 let mut results: Vec<String> = vec![];
105 for item in v[1].as_array().unwrap() { 105 for item in v[1].as_array().unwrap() {
106 match item { 106 if let Value::String(x) = item {
107 Value::String(x) => results.push(x.to_string()), 107 results.push(x.to_string())
108 // ignore non strings
109 _ => (),
110 } 108 }
111 } 109 }
112 Ok(results) 110 Ok(results)
@@ -135,21 +133,21 @@ pub fn get_links(v: &Value) -> Result<Vec<String>, reqwest::Error> {
135 Ok(links) 133 Ok(links)
136} 134}
137 135
138pub fn pop_error(s: &mut Cursive, msg: String) { 136pub fn pop_error(s: &mut Cursive, msg: &str) {
139 s.add_layer(Dialog::text(format!("{}", msg)).button("Ok", |s| s.quit())); 137 s.add_layer(Dialog::text(msg.to_string()).button("Ok", |s| s.quit()));
140} 138}
141 139
142pub fn handler(e: reqwest::Error) -> String { 140pub fn handler(e: &reqwest::Error) -> String {
143 let mut msg: String = String::new(); 141 let mut msg: String = String::new();
144 if e.is_http() { 142 if e.is_http() {
145 match e.url() { 143 match e.url() {
146 None => msg.push_str(&format!("No URL given")), 144 None => msg.push_str(&"No URL given"),
147 Some(url) => msg.push_str(&format!("Problem making request to: {}", url)), 145 Some(url) => msg.push_str(&format!("Problem making request to: {}", url)),
148 } 146 }
149 } 147 }
150 148
151 if e.is_redirect() { 149 if e.is_redirect() {
152 msg.push_str(&format!("server redirecting too many times or making loop")); 150 msg.push_str(&"server redirecting too many times or making loop");
153 } 151 }
154 152
155 msg 153 msg
diff --git a/src/main.rs b/src/main.rs
index fd01af8..85d25e7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,9 +6,7 @@ extern crate serde_json;
6extern crate lazy_static; 6extern crate lazy_static;
7 7
8use cursive::traits::*; 8use cursive::traits::*;
9use cursive::views::{ 9use cursive::views::{Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView};
10 Dialog, DummyView, EditView, LinearLayout, OnEventView, SelectView, TextView,
11};
12use cursive::Cursive; 10use cursive::Cursive;
13 11
14use serde_json::Value; 12use serde_json::Value;
@@ -83,7 +81,7 @@ fn search(s: &mut Cursive) {
83 let mut result = vec![]; 81 let mut result = vec![];
84 match get_search_results(&search) { 82 match get_search_results(&search) {
85 Ok(x) => result = x, 83 Ok(x) => result = x,
86 Err(e) => pop_error(s, handler(e)), 84 Err(e) => pop_error(s, &handler(&e)),
87 }; 85 };
88 let choose_result = SelectView::<String>::new() 86 let choose_result = SelectView::<String>::new()
89 .with_all_str(result) 87 .with_all_str(result)
@@ -132,15 +130,15 @@ fn on_submit(s: &mut Cursive, name: &str) {
132 130
133 match get_extract(&v) { 131 match get_extract(&v) {
134 Ok(x) => extract = x, 132 Ok(x) => extract = x,
135 Err(e) => pop_error(s, handler(e)), 133 Err(e) => pop_error(s, &handler(&e)),
136 }; 134 };
137 match get_links(&v) { 135 match get_links(&v) {
138 Ok(x) => link_vec = x, 136 Ok(x) => link_vec = x,
139 Err(e) => pop_error(s, handler(e)), 137 Err(e) => pop_error(s, &handler(&e)),
140 }; 138 };
141 139
142 // get the act together 140 // get the act together
143 let article_content = TextView::new(extract_formatter(extract)).scrollable(); 141 let article_content = TextView::new(extract_formatter(&extract)).scrollable();
144 142
145 let links = SelectView::<String>::new() 143 let links = SelectView::<String>::new()
146 .with_all_str(link_vec) 144 .with_all_str(link_vec)