pull/6/head
dvkt 5 years ago
parent ba138993d6
commit d7052a2b3d

@ -13,15 +13,15 @@ use termion::raw::IntoRawMode;
#[derive(Debug)] #[derive(Debug)]
struct App { struct App {
pages: HashMap<String, Page>, pages: HashMap<String, Page>,
cursor: String,
history: Vec<String>, history: Vec<String>,
pos: usize,
} }
#[derive(Debug)] #[derive(Debug)]
struct Page { struct Page {
body: String, body: String,
cursor: usize,
url: String, url: String,
link: usize,
links: Vec<Link>, links: Vec<Link>,
} }
@ -56,43 +56,48 @@ impl App {
fn new() -> App { fn new() -> App {
App { App {
pages: HashMap::new(), pages: HashMap::new(),
cursor: String::new(), pos: 0,
history: Vec::new(), history: Vec::new(),
} }
} }
fn back(&mut self) { fn back(&mut self) {
if self.history.len() > 1 { if self.history.len() > 1 {
self.history.pop(); self.pos -= 1;
self.cursor = self.history.last().unwrap().to_string();
} }
} }
fn load(&mut self, host: &str, port: &str, selector: &str) { fn load(&mut self, host: &str, port: &str, selector: &str) {
let mut page = self.fetch(host, port, selector); let mut page = self.fetch(host, port, selector);
page.parse_links(); page.parse_links();
self.cursor = page.url.to_string(); if self.history.len() > 0 {
self.history.push(self.cursor.to_string()); self.pos = self.history.len() - 1;
}
self.history.push(page.url.to_string());
self.pages.insert(page.url.to_string(), page); self.pages.insert(page.url.to_string(), page);
} }
fn render(&self) { fn render(&self) {
if let Some(page) = self.pages.get(&self.cursor) { if let Some(url) = self.history.get(self.pos) {
print!("\x1B[2J\x1B[H{}", page.draw()); if let Some(page) = self.pages.get(url) {
print!("\x1B[2J\x1B[H{}", page.draw());
}
} }
} }
fn respond(&mut self) { fn respond(&mut self) {
let mut addr = (String::new(), String::new(), String::new()); let mut addr = (String::new(), String::new(), String::new());
match self.pages.get_mut(&self.cursor) { let url = self.history.get(self.pos).unwrap();
let page = self.pages.get_mut(url);
match page {
None => return, None => return,
Some(page) => match read_input() { Some(page) => match read_input() {
Action::Up => page.cursor_up(), Action::Up => page.cursor_up(),
Action::Down => page.cursor_down(), Action::Down => page.cursor_down(),
Action::Back => self.back(), Action::Back => self.back(),
Action::Open => { Action::Open => {
if page.cursor > 0 && page.cursor - 1 < page.links.len() { if page.link > 0 && page.link - 1 < page.links.len() {
let link = &page.links[page.cursor - 1]; let link = &page.links[page.link - 1];
addr.0 = link.host.to_string(); addr.0 = link.host.to_string();
addr.1 = link.port.to_string(); addr.1 = link.port.to_string();
addr.2 = link.selector.to_string(); addr.2 = link.selector.to_string();
@ -123,7 +128,7 @@ impl App {
}); });
Page { Page {
body: body, body: body,
cursor: 0, link: 0,
url: format!("{}:{}{}", host, port, selector), url: format!("{}:{}{}", host, port, selector),
links: Vec::new(), links: Vec::new(),
} }
@ -132,13 +137,13 @@ impl App {
impl Page { impl Page {
fn cursor_up(&mut self) { fn cursor_up(&mut self) {
if self.cursor > 0 { if self.link > 0 {
self.cursor -= 1; self.link -= 1;
} }
} }
fn cursor_down(&mut self) { fn cursor_down(&mut self) {
if self.cursor < self.links.len() { if self.link < self.links.len() {
self.cursor += 1; self.link += 1;
} }
} }
@ -221,7 +226,7 @@ impl Page {
'\n' => continue, '\n' => continue,
_ => prefix = "", _ => prefix = "",
} }
if is_link && self.cursor > 0 && self.cursor == links { if is_link && self.link > 0 && self.link == links {
out.push_str("\x1b[92;1m*\x1b[0m"); out.push_str("\x1b[92;1m*\x1b[0m");
} else { } else {
out.push(' '); out.push(' ');
@ -291,7 +296,7 @@ fn read_input() -> Action {
Key::Up | Key::Ctrl('p') => return Action::Up, Key::Up | Key::Ctrl('p') => return Action::Up,
Key::Down | Key::Ctrl('n') => return Action::Down, Key::Down | Key::Ctrl('n') => return Action::Down,
Key::Ctrl(c) => print!("Ctrl-{}", c), Key::Ctrl(c) => print!("Ctrl-{}", c),
Key::Left => return Action::Back,az Key::Left => return Action::Back,
Key::Right => print!("<right>"), Key::Right => print!("<right>"),
Key::Backspace | Key::Delete => { Key::Backspace | Key::Delete => {
input.pop(); input.pop();

Loading…
Cancel
Save