forked from cadey/maj
majc: move show to its own function
This commit is contained in:
parent
1cd7b99fea
commit
3a27e2860b
|
@ -18,6 +18,7 @@ The main homepage for majc is on tulpa.dev:
|
|||
## Important Keys
|
||||
|
||||
<esc>: opens the menubar
|
||||
c: closes the active window
|
||||
o: prompts to open a URL
|
||||
q: quits majc
|
||||
?: shows this screen
|
||||
|
|
|
@ -5,12 +5,17 @@ use cursive::{
|
|||
views::{Dialog, EditView, Panel, ResizedView, TextView},
|
||||
Cursive,
|
||||
};
|
||||
use maj::{self, Response, StatusCode};
|
||||
use std::str;
|
||||
|
||||
fn main() {
|
||||
cursive::logger::init();
|
||||
|
||||
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('~', cursive::Cursive::toggle_debug_console);
|
||||
siv.add_global_callback('o', open_prompt);
|
||||
|
@ -33,6 +38,7 @@ fn main() {
|
|||
)
|
||||
.add_leaf("Open", |s| open_prompt(s));
|
||||
|
||||
siv.set_autohide_menu(false);
|
||||
siv.add_global_callback(Key::Esc, |s| s.select_menubar());
|
||||
|
||||
help(&mut siv);
|
||||
|
@ -72,25 +78,12 @@ fn open_prompt(siv: &mut Cursive) {
|
|||
}
|
||||
|
||||
fn open(siv: &mut Cursive, url: &str) {
|
||||
use maj::{get, StatusCode};
|
||||
use std::str;
|
||||
|
||||
siv.pop_layer();
|
||||
log::debug!("got URL: {}", url);
|
||||
|
||||
match get(url.to_string()) {
|
||||
match maj::get(url.to_string()) {
|
||||
Ok(resp) => {
|
||||
if resp.status != StatusCode::Success {
|
||||
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)),
|
||||
));
|
||||
show(siv, url, resp);
|
||||
}
|
||||
Err(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
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue