majc: move show to its own function

This commit is contained in:
Cadey Ratio 2020-07-25 12:50:34 -04:00
parent 1cd7b99fea
commit 3a27e2860b
2 changed files with 31 additions and 15 deletions

View File

@ -18,6 +18,7 @@ The main homepage for majc is on tulpa.dev:
## Important Keys ## Important Keys
<esc>: opens the menubar <esc>: opens the menubar
c: closes the active window
o: prompts to open a URL o: prompts to open a URL
q: quits majc q: quits majc
?: shows this screen ?: shows this screen

View File

@ -5,12 +5,17 @@ use cursive::{
views::{Dialog, EditView, Panel, ResizedView, TextView}, views::{Dialog, EditView, Panel, ResizedView, TextView},
Cursive, Cursive,
}; };
use maj::{self, Response, StatusCode};
use std::str;
fn main() { fn main() {
cursive::logger::init(); cursive::logger::init();
let mut siv = cursive::default(); let mut siv = cursive::default();
siv.add_global_callback('c', |s| {
s.pop_layer();
});
siv.add_global_callback('q', cursive::Cursive::quit); siv.add_global_callback('q', cursive::Cursive::quit);
siv.add_global_callback('~', cursive::Cursive::toggle_debug_console); siv.add_global_callback('~', cursive::Cursive::toggle_debug_console);
siv.add_global_callback('o', open_prompt); siv.add_global_callback('o', open_prompt);
@ -33,6 +38,7 @@ fn main() {
) )
.add_leaf("Open", |s| open_prompt(s)); .add_leaf("Open", |s| open_prompt(s));
siv.set_autohide_menu(false);
siv.add_global_callback(Key::Esc, |s| s.select_menubar()); siv.add_global_callback(Key::Esc, |s| s.select_menubar());
help(&mut siv); help(&mut siv);
@ -72,25 +78,12 @@ fn open_prompt(siv: &mut Cursive) {
} }
fn open(siv: &mut Cursive, url: &str) { fn open(siv: &mut Cursive, url: &str) {
use maj::{get, StatusCode};
use std::str;
siv.pop_layer(); siv.pop_layer();
log::debug!("got URL: {}", url); log::debug!("got URL: {}", url);
match get(url.to_string()) { match maj::get(url.to_string()) {
Ok(resp) => { Ok(resp) => {
if resp.status != StatusCode::Success { show(siv, url, resp);
siv.add_layer(Dialog::info(format!("{:?}: {}", resp.status, resp.meta)));
return;
}
siv.add_fullscreen_layer(ResizedView::with_full_screen(
Dialog::around(Panel::new(
TextView::new(str::from_utf8(&resp.body).unwrap()).scrollable(),
))
.title(format!("{}: {}", url, resp.meta)),
));
} }
Err(why) => { Err(why) => {
log::error!("got response error: {:?}", why); log::error!("got response error: {:?}", why);
@ -98,3 +91,25 @@ fn open(siv: &mut Cursive, url: &str) {
} }
} }
} }
fn show(siv: &mut Cursive, url: &str, resp: Response) {
if resp.status != StatusCode::Success {
siv.add_layer(Dialog::info(format!("{:?}: {}", resp.status, resp.meta)));
return;
}
match str::from_utf8(&resp.body) {
Ok(content) => {
siv.add_fullscreen_layer(ResizedView::with_full_screen(
Dialog::around(TextView::new(content).scrollable())
.title(format!("{}: {}", url, resp.meta)),
));
}
Err(why) => {
siv.add_layer(Dialog::info(format!(
"UTF/8 decoding error for {}: {:?}",
url, why
)));
}
}
}