diff --git a/src/main.rs b/src/main.rs index 3547439..4c254c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ mod menu; mod types; mod ui; +use gopher::Type; + fn main() { let args: Vec = std::env::args().collect(); if args.len() < 2 { @@ -23,9 +25,9 @@ fn main() { print_usage(); return; } - let mut ui = ui::UI::new(); - ui.load(gopher::Type::Menu, host, port, selector); + let url = format!("{}:{}/1/{}", host, port, selector); + ui.load(url); ui.run(); } diff --git a/src/menu.rs b/src/menu.rs index d196217..cd60c60 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -13,9 +13,9 @@ pub struct MenuView { #[derive(Debug)] pub struct Menu { + url: String, // gopher url lines: Vec, // lines raw: String, // raw gopher response - url: String, // gopher url } #[derive(Debug)] @@ -128,15 +128,6 @@ impl Menu { Self::parse(url, gopher_response) } - // Loads a Menu given a URL. - pub fn load(host: &str, port: &str, selector: &str) -> io::Result { - let url = format!("{}:{}{}", host, port, selector); - match gopher::fetch(host, port, selector) { - Ok(res) => Ok(Menu::from(url, res)), - Err(e) => Err(e), - } - } - // Parses the lines in a raw Gopher menu response. fn parse(url: String, raw: String) -> Menu { let mut lines = vec![]; @@ -149,18 +140,23 @@ impl Menu { if start { line.0 = i + 1; match c { - '0' => { - line.2 = Type::Text; - } - '1' => { - line.2 = Type::Menu; - } - 'h' => { - line.2 = Type::HTML; - } - 'i' => { - line.2 = Type::Info; - } + '0' => line.2 = Type::Text, + '1' => line.2 = Type::Menu, + '2' => panic!("CSOEntity not supported"), // TODO + '3' => line.2 = Type::Error, + '4' => panic!("Binhex not supported"), // TODO + '5' => panic!("DOSFile not supported"), // TODO + '6' => panic!("UUEncoded not supported"), // TODO + '7' => panic!("Search not supported"), // TODO + '8' => panic!("Telnet not supported"), // TODO + '9' => panic!("Binary not supported"), // TODO + '+' => panic!("Mirrors not supported"), // TODO + 'g' => panic!("GIF not supported"), // TODO + 'T' => panic!("Telnet3270 not supported"), // TODO + 'h' => line.2 = Type::HTML, + 'i' => line.2 = Type::Info, + 's' => panic!("Sound not supported"), // TODO + 'd' => panic!("Document not supported"), // TODO '\n' => continue, _ => { eprintln!("unknown line type: {}", c); diff --git a/src/ui.rs b/src/ui.rs index 9fa0444..92559fe 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -59,19 +59,15 @@ impl UI { String::new() } - pub fn load(&mut self, typ: Type, host: &str, port: &str, selector: &str) { - let response = gopher::fetch(host, port, selector) + pub fn load(&mut self, url: String) { + let (typ, host, port, sel) = gopher::parse_url(&url); + let response = gopher::fetch(host, port, sel) .map_err(|e| { - eprintln!( - "\x1B[91merror loading \x1b[93m{}:{}{}: \x1B[0m{}", - host, port, selector, e - ); + eprintln!("\x1B[91merror loading \x1b[93m{}: \x1B[0m{}", url, e); std::process::exit(1); }) .unwrap(); - let url = format!("{}:{}{}", host, port, selector); // TODO - match typ { Type::Menu => self.add_view(MenuView::from(url, response)), // Type::Text => self.add_view(TextView::from(url, response)),