pull/6/head
dvkt 5 years ago
parent e3ef6f789d
commit 30e8640fa1

@ -7,6 +7,7 @@ mod gopher;
mod menu; mod menu;
mod types; mod types;
mod ui; mod ui;
use ui::UI;
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
@ -26,8 +27,8 @@ fn main() {
return; return;
} }
let mut ui = ui::UI::new(); let mut ui = UI::new();
ui.load(url); ui.open(url);
ui.run(); ui.run();
} }

@ -1,4 +1,3 @@
use gopher;
use gopher::Type; use gopher::Type;
use std::io; use std::io;
use ui::{Action, Key, View}; use ui::{Action, Key, View};
@ -21,9 +20,7 @@ pub struct Menu {
#[derive(Debug)] #[derive(Debug)]
pub struct Line { pub struct Line {
name: String, name: String,
host: String, url: String,
port: String,
selector: String,
typ: Type, typ: Type,
} }
@ -36,8 +33,45 @@ impl View for MenuView {
} }
fn process_input(&mut self, key: Key) -> Action { fn process_input(&mut self, key: Key) -> Action {
match self.process_key(key) {
a @ Action::Unknown => return a,
a => a,
}
}
}
impl MenuView {
pub fn from(url: String, response: String) -> MenuView {
MenuView {
menu: Menu::from(url, response),
input: String::new(),
line: 0,
scroll: 0,
}
}
fn action_page_down(&self) {}
fn action_page_up(&self) {}
fn action_up(&self) {}
fn action_down(&self) {}
fn process_key(&mut self, key: Key) -> Action {
match key { match key {
Key::Char('\n') => Action::Open, Key::Char('\n') => {
if let Some(line) = self.menu.lines.get(self.line) {
Action::Open(line.url.to_string())
} else {
Action::None
}
}
Key::Up | Key::Ctrl('p') => {
self.action_up();
Action::None
}
Key::Down | Key::Ctrl('n') => {
self.action_down();
Action::None
}
Key::Backspace => { Key::Backspace => {
if self.input.is_empty() { if self.input.is_empty() {
Action::Back Action::Back
@ -60,7 +94,8 @@ impl View for MenuView {
} }
Key::Char('-') => { Key::Char('-') => {
if self.input.is_empty() { if self.input.is_empty() {
Action::PageUp self.action_page_up();
Action::None
} else { } else {
self.input.push('-'); self.input.push('-');
Action::Input Action::Input
@ -68,7 +103,8 @@ impl View for MenuView {
} }
Key::Char(' ') => { Key::Char(' ') => {
if self.input.is_empty() { if self.input.is_empty() {
return Action::PageDown; self.action_page_down();
Action::None
} else { } else {
self.input.push(' '); self.input.push(' ');
Action::Input Action::Input
@ -118,17 +154,6 @@ impl View for MenuView {
} }
} }
impl MenuView {
pub fn from(url: String, response: String) -> MenuView {
MenuView {
menu: Menu::from(url, response),
input: String::new(),
line: 0,
scroll: 0,
}
}
}
impl Menu { impl Menu {
pub fn from(url: String, gopher_response: String) -> Menu { pub fn from(url: String, gopher_response: String) -> Menu {
Self::parse(url, gopher_response) Self::parse(url, gopher_response)
@ -164,13 +189,20 @@ impl Menu {
}; };
let parts: Vec<&str> = line.split_terminator("\t").collect(); let parts: Vec<&str> = line.split_terminator("\t").collect();
lines.push(Line { let mut url = String::from("gopher://");
name: parts[0][1..].to_string(), if parts.len() > 2 {
selector: parts[1].to_string(), url.push_str(parts[2]); // host
host: parts[2].to_string(), }
port: parts[3].trim_end_matches('\r').to_string(), if parts.len() > 3 {
typ, url.push(':');
}); url.push_str(parts[3].trim_end_matches('\r')); // port
}
if parts.len() > 1 {
url.push_str(parts[1]); // selector
}
let name = parts[0][1..].to_string();
lines.push(Line { name, url, typ });
} }
} }

@ -5,7 +5,7 @@ use termion::raw::IntoRawMode;
use gopher; use gopher;
use gopher::Type; use gopher::Type;
use menu::{Menu, MenuView}; use menu::MenuView;
pub type Key = termion::event::Key; pub type Key = termion::event::Key;
pub type Error = io::Error; pub type Error = io::Error;
@ -18,16 +18,12 @@ pub struct UI {
#[derive(Debug)] #[derive(Debug)]
pub enum Action { pub enum Action {
None, None,
Up,
Down,
PageUp,
PageDown,
Back, Back,
Forward, Forward,
Open, Open(String), // url
Input, // redraw the input bar
Quit, Quit,
Unknown, Unknown,
Input, // redraw the input bar
FollowLink(usize), FollowLink(usize),
} }
@ -46,12 +42,12 @@ impl UI {
pub fn run(&mut self) { pub fn run(&mut self) {
loop { loop {
self.print(); self.draw();
self.respond_to_user(); self.update();
} }
} }
pub fn print(&self) { pub fn draw(&self) {
print!("{}", self.render()); print!("{}", self.render());
// print!("{:#?}", self); // print!("{:#?}", self);
} }
@ -66,7 +62,7 @@ impl UI {
String::from("N/A") String::from("N/A")
} }
pub fn load(&mut self, url: &str) { pub fn open(&mut self, url: &str) {
let (typ, host, port, sel) = gopher::parse_url(url); let (typ, host, port, sel) = gopher::parse_url(url);
let response = gopher::fetch(host, port, sel) let response = gopher::fetch(host, port, sel)
.map_err(|e| { .map_err(|e| {
@ -76,20 +72,20 @@ impl UI {
.unwrap(); .unwrap();
match typ { match typ {
Type::Menu => self.add_view(MenuView::from(url.to_string(), response)), Type::Menu => self.add_page(MenuView::from(url.to_string(), response)),
// Type::Text => self.add_view(TextView::from(url, response)), // Type::Text => self.add_page(TextView::from(url, response)),
_ => panic!("unknown type: {:?}", typ), _ => panic!("unknown type: {:?}", typ),
} }
} }
fn add_view<T: View + 'static>(&mut self, view: T) { fn add_page<T: View + 'static>(&mut self, view: T) {
self.pages.push(Box::from(view)); self.pages.push(Box::from(view));
if self.pages.len() > 1 { if self.pages.len() > 1 {
self.page += 1; self.page += 1;
} }
} }
fn respond_to_user(&mut self) { fn update(&mut self) {
match self.process_input() { match self.process_input() {
Action::Quit => std::process::exit(1), Action::Quit => std::process::exit(1),
_ => {} _ => {}
@ -107,8 +103,6 @@ impl UI {
match page.process_input(key) { match page.process_input(key) {
Action::Unknown => match key { Action::Unknown => match key {
Key::Ctrl('q') => return Action::Quit, Key::Ctrl('q') => return Action::Quit,
Key::Up | Key::Ctrl('p') => return Action::Up,
Key::Down | Key::Ctrl('n') => return Action::Down,
Key::Left => return Action::Back, Key::Left => return Action::Back,
Key::Right => return Action::Forward, Key::Right => return Action::Forward,
_ => {} _ => {}

Loading…
Cancel
Save