From 3a27e2860b81b9a7db641b5b32fc7082f37ba5da Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 25 Jul 2020 12:50:34 -0400 Subject: [PATCH] majc: move show to its own function --- majc/src/help.gmi | 1 + majc/src/main.rs | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/majc/src/help.gmi b/majc/src/help.gmi index 8aa8f1c..7c0bdc4 100644 --- a/majc/src/help.gmi +++ b/majc/src/help.gmi @@ -18,6 +18,7 @@ The main homepage for majc is on tulpa.dev: ## Important Keys : opens the menubar +c: closes the active window o: prompts to open a URL q: quits majc ?: shows this screen diff --git a/majc/src/main.rs b/majc/src/main.rs index baa60fd..28895f4 100644 --- a/majc/src/main.rs +++ b/majc/src/main.rs @@ -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 + ))); + } + } +}