From 84f50935316c4e813e6b347dbdc4306658ab1d1c Mon Sep 17 00:00:00 2001 From: dvkt Date: Thu, 19 Dec 2019 17:26:53 -0800 Subject: [PATCH] more help system --- src/help.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/ui.rs | 29 ++++++++++++++-- 2 files changed, 118 insertions(+), 6 deletions(-) diff --git a/src/help.rs b/src/help.rs index f31334d..8ea8a1f 100644 --- a/src/help.rs +++ b/src/help.rs @@ -1,4 +1,14 @@ -pub const GOPHERMAP: &str = " +pub fn lookup(name: &str) -> Option<&str> { + match name { + "" | "/" | "help" => Some(HELP), + "types" => Some(TYPES), + "nav" => Some(NAV), + "keys" => Some(KEYS), + _ => None, + } +} + +pub const HELP: &str = " i i i / / / @@ -28,11 +38,90 @@ iPress the # of a link to visit ior select it. Use ENTER to open ithe selected link. i -iTo select a link, start typing. +iTo select a link by name, just +istart typing. i i ~ * ~ i -1about phetch /phlog help +1menu navigation /nav help +1gopher types /types help +1all keys /keys help hvisit homepage URL:https://github.com/dvkt/phetch i "; + +pub const NAV: &str = " +i +i +i / / / +i ___ (___ ___ (___ ___ (___ +i| )| )|___)| | | ) +i|__/ | / |__ |__ |__ | / +i| +i +i ** menu navigation ** +i +ithere are three ways to navigate +imenus in phetch: +i +1up & down arrows /nav help +i +iuse the up and down arrows or the +ictrl-p/ctrl-n combos to select menu +iitems. phetch will scroll for you, +ibut you can use page up & page down +ito jump by many lines quickly. +i +1number keys /nav help +i +iif there are few enough menu items, +ipressing a number key will open the +iitem immediately. otherwise, it'll +ibe selected. +i +1incremental search /nav help +i +ijust start typing. phetch will look +ifor the first case insensitive match +iand try to select it. +i +"; + +pub const TYPES: &str = " +i +i +i / / / +i ___ (___ ___ (___ ___ (___ +i| )| )|___)| | | ) +i|__/ | / |__ |__ |__ | / +i| +i +i ** gopher types ** +i +iphetch supports these links: +i +0text files +1menu items +3errors +hexternal URLs +7search servers +8telnet launching +i +iand these download types: +i +4binhex +5dosfiles +6uuencoded files +9binaries +gGIFs +Iimages downloads +ssound files +ddocuments +i +iphetch does not support: +i +2CSO Entries ++Mirrors +TTelnet3270 +i +"; diff --git a/src/ui.rs b/src/ui.rs index 031ea33..e6fa4f5 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -91,11 +91,23 @@ impl UI { } pub fn open(&mut self, url: &str) -> io::Result<()> { + // no open loops + if let Some(page) = self.pages.get(self.page) { + if &page.url() == url { + return Ok(()); + } + } + // non-gopher URL if !url.starts_with("gopher://") { return open_external(url); } + // on-line help + if url.starts_with("gopher://help/") { + return self.open_help(url); + } + // gopher URL status(&format!( "{}Loading...{}", @@ -114,6 +126,19 @@ impl UI { .map_err(|e| io_error(format!("Error loading {}: {} ({:?})", url, e, e.kind()))) } + // open a phetch on-line help url, ex: gopher://help/1/types + pub fn open_help(&mut self, url: &str) -> io::Result<()> { + if let Some(source) = help::lookup( + &url.trim_start_matches("gopher://help/") + .trim_start_matches("1/"), + ) { + self.add_page(Menu::from(url.to_string(), source.to_string())); + Ok(()) + } else { + Err(gopher::io_error(format!("Help file not found: {}", url))) + } + } + pub fn render(&mut self) -> String { if let Ok((cols, rows)) = termion::terminal_size() { self.term_size(cols as usize, rows as usize); @@ -199,9 +224,7 @@ impl UI { } } } - Action::Keypress(Key::Ctrl('h')) => { - self.add_page(Menu::from("help".into(), help::GOPHERMAP.into())); - } + Action::Keypress(Key::Ctrl('h')) => self.open("gopher://help/")?, Action::Keypress(Key::Ctrl('u')) => { if let Some(page) = self.pages.get(self.page) { status(&format!("Current URL: {}", page.url()));