follow link

pull/6/head
dvkt 5 years ago
parent 8dbb7b7550
commit 1655589eaf

@ -35,13 +35,6 @@ pub fn fetch(host: &str, port: &str, selector: &str) -> io::Result<String> {
})
.and_then(|mut stream| {
stream.read_to_string(&mut body);
let last: String = body.chars().rev().take(3).collect();
// Gopher responses should end .\r\n, but don't always.
if &last == ".\r\n" {
body.pop(); body.pop(); body.pop();
}
Ok(())
});

@ -72,7 +72,7 @@ impl MenuView {
s.push('\t');
match line.typ {
Type::Text => push!("90", &line.name),
Type::Text => push!("96", &line.name),
Type::Menu => push!("94", &line.name),
Type::Info => push!("93", &line.name),
Type::HTML => push!("92", &line.name),
@ -86,6 +86,14 @@ impl MenuView {
fn action_up(&self) {}
fn action_down(&self) {}
fn action_follow_link(&self, line: usize) -> Action {
if let Some(line) = self.lines().get(line) {
Action::Open(line.url.to_string())
} else {
Action::None
}
}
fn process_key(&mut self, key: Key) -> Action {
match key {
Key::Char('\n') => {
@ -147,34 +155,34 @@ impl MenuView {
// jump to number
let count = self.menu.lines.len();
if count < 10 && c == '1' && i == 0 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 20 && c == '2' && i == 1 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 30 && c == '3' && i == 2 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 40 && c == '4' && i == 3 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 50 && c == '5' && i == 4 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 60 && c == '6' && i == 5 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 70 && c == '7' && i == 6 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 80 && c == '8' && i == 7 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if count < 90 && c == '9' && i == 8 {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if self.input.len() > 1 && self.input == (i + 1).to_string() {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else if self.input.len() == 1 && self.input == (i + 1).to_string() {
return Action::FollowLink(i);
return self.action_follow_link(i);
} else {
if link
.name
.to_ascii_lowercase()
.contains(&self.input.to_ascii_lowercase())
{
return Action::FollowLink(i);
return self.action_follow_link(i);
}
}
}
@ -212,6 +220,7 @@ impl Menu {
'i' => Type::Info,
's' => panic!("Sound not supported"), // TODO
'd' => panic!("Document not supported"), // TODO
'.' => continue,
'\n' => continue,
_ => {
eprintln!("unknown line type: {}", c);

@ -13,18 +13,18 @@ pub type Error = io::Error;
pub struct UI {
pages: Vec<Box<dyn View>>,
page: usize,
dirty: bool, // redraw?
}
#[derive(Debug)]
pub enum Action {
None,
Back,
Forward,
None, // do nothing
Back, // back in history
Forward, // also history
Open(String), // url
Input, // redraw the input bar
Quit,
Unknown,
FollowLink(usize),
Quit, // yup
Unknown, // handler doesn't know what to do
}
pub trait View {
@ -37,6 +37,7 @@ impl UI {
UI {
pages: vec![],
page: 0,
dirty: true,
}
}
@ -47,8 +48,11 @@ impl UI {
}
}
pub fn draw(&self) {
print!("{}", self.render());
pub fn draw(&mut self) {
if self.dirty {
print!("{}", self.render());
self.dirty = false;
}
}
pub fn update(&mut self) {
@ -92,9 +96,20 @@ impl UI {
}
fn process_input(&mut self) -> Action {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
stdout.flush().unwrap();
match self.process_page_input() {
Action::Open(url) => {
self.open(&url);
Action::None
}
a => a,
}
}
fn process_page_input(&mut self) -> Action {
let stdin = stdin();
let page = self.pages.get_mut(self.page).expect("expected Page"); // TODO
for c in stdin.keys() {

Loading…
Cancel
Save