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

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

@ -1,4 +1,3 @@
use gopher;
use gopher::Type;
use std::io;
use ui::{Action, Key, View};
@ -21,9 +20,7 @@ pub struct Menu {
#[derive(Debug)]
pub struct Line {
name: String,
host: String,
port: String,
selector: String,
url: String,
typ: Type,
}
@ -36,8 +33,45 @@ impl View for MenuView {
}
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 {
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 => {
if self.input.is_empty() {
Action::Back
@ -60,7 +94,8 @@ impl View for MenuView {
}
Key::Char('-') => {
if self.input.is_empty() {
Action::PageUp
self.action_page_up();
Action::None
} else {
self.input.push('-');
Action::Input
@ -68,7 +103,8 @@ impl View for MenuView {
}
Key::Char(' ') => {
if self.input.is_empty() {
return Action::PageDown;
self.action_page_down();
Action::None
} else {
self.input.push(' ');
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 {
pub fn from(url: String, gopher_response: String) -> Menu {
Self::parse(url, gopher_response)
@ -164,13 +189,20 @@ impl Menu {
};
let parts: Vec<&str> = line.split_terminator("\t").collect();
lines.push(Line {
name: parts[0][1..].to_string(),
selector: parts[1].to_string(),
host: parts[2].to_string(),
port: parts[3].trim_end_matches('\r').to_string(),
typ,
});
let mut url = String::from("gopher://");
if parts.len() > 2 {
url.push_str(parts[2]); // host
}
if parts.len() > 3 {
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::Type;
use menu::{Menu, MenuView};
use menu::MenuView;
pub type Key = termion::event::Key;
pub type Error = io::Error;
@ -18,16 +18,12 @@ pub struct UI {
#[derive(Debug)]
pub enum Action {
None,
Up,
Down,
PageUp,
PageDown,
Back,
Forward,
Open,
Open(String), // url
Input, // redraw the input bar
Quit,
Unknown,
Input, // redraw the input bar
FollowLink(usize),
}
@ -46,12 +42,12 @@ impl UI {
pub fn run(&mut self) {
loop {
self.print();
self.respond_to_user();
self.draw();
self.update();
}
}
pub fn print(&self) {
pub fn draw(&self) {
print!("{}", self.render());
// print!("{:#?}", self);
}
@ -66,7 +62,7 @@ impl UI {
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 response = gopher::fetch(host, port, sel)
.map_err(|e| {
@ -76,20 +72,20 @@ impl UI {
.unwrap();
match typ {
Type::Menu => self.add_view(MenuView::from(url.to_string(), response)),
// Type::Text => self.add_view(TextView::from(url, response)),
Type::Menu => self.add_page(MenuView::from(url.to_string(), response)),
// Type::Text => self.add_page(TextView::from(url, response)),
_ => 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));
if self.pages.len() > 1 {
self.page += 1;
}
}
fn respond_to_user(&mut self) {
fn update(&mut self) {
match self.process_input() {
Action::Quit => std::process::exit(1),
_ => {}
@ -107,8 +103,6 @@ impl UI {
match page.process_input(key) {
Action::Unknown => match key {
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::Right => return Action::Forward,
_ => {}

Loading…
Cancel
Save