2
0
mirror of https://github.com/xvxx/phetch synced 2024-11-10 13:10:54 +00:00

cache links

This commit is contained in:
dvkt 2019-12-18 14:00:34 -08:00
parent bc9f5eb9f7
commit bab87a3243

View File

@ -13,9 +13,10 @@ pub struct MenuView {
} }
pub struct Menu { pub struct Menu {
url: String, // gopher url url: String, // gopher url
lines: Vec<Line>, // lines lines: Vec<Line>, // lines
longest: usize, // size of the longest line links: Vec<usize>, // links (index of line in lines vec)
longest: usize, // size of the longest line
} }
#[derive(Debug)] #[derive(Debug)]
@ -59,8 +60,16 @@ impl MenuView {
&self.menu.lines &self.menu.lines
} }
fn links(&self) -> impl Iterator<Item = &Line> { fn links(&self) -> &Vec<usize> {
self.menu.links() &self.menu.links
}
fn link(&self, i: usize) -> Option<&Line> {
if let Some(line) = self.menu.links.get(i) {
self.menu.lines.get(*line)
} else {
None
}
} }
fn render_lines(&self) -> String { fn render_lines(&self) -> String {
@ -179,7 +188,7 @@ impl MenuView {
} }
fn action_down(&mut self) -> Action { fn action_down(&mut self) -> Action {
let count = self.links().count(); let count = self.links().len();
if count > 0 && self.link < count - 1 { if count > 0 && self.link < count - 1 {
self.link += 1; self.link += 1;
Action::Redraw Action::Redraw
@ -189,7 +198,7 @@ impl MenuView {
} }
fn action_select_link(&mut self, line: usize) -> Action { fn action_select_link(&mut self, line: usize) -> Action {
if line < self.links().count() { if line < self.links().len() {
self.link = line; self.link = line;
Action::Redraw Action::Redraw
} else { } else {
@ -199,7 +208,7 @@ impl MenuView {
fn action_follow_link(&mut self, link: usize) -> Action { fn action_follow_link(&mut self, link: usize) -> Action {
self.input.clear(); self.input.clear();
if let Some(line) = self.links().nth(link) { if let Some(line) = self.link(link) {
let url = line.url.to_string(); let url = line.url.to_string();
Action::Open(url) Action::Open(url)
} else { } else {
@ -211,7 +220,7 @@ impl MenuView {
match key { match key {
Key::Char('\n') => { Key::Char('\n') => {
self.input.clear(); self.input.clear();
if let Some(line) = self.links().nth(self.link) { if let Some(line) = self.link(self.link) {
let url = line.url.to_string(); let url = line.url.to_string();
Action::Open(url) Action::Open(url)
} else { } else {
@ -264,7 +273,7 @@ impl MenuView {
} }
Key::Char(c) => { Key::Char(c) => {
self.input.push(c); self.input.push(c);
let count = self.links().count(); let count = self.links().len();
let input = &self.input; let input = &self.input;
// jump to <10 number // jump to <10 number
@ -309,7 +318,7 @@ impl MenuView {
for i in 0..count { for i in 0..count {
// check for name match // check for name match
let name = if let Some(link) = self.links().nth(i) { let name = if let Some(link) = self.link(i) {
link.name.to_ascii_lowercase() link.name.to_ascii_lowercase()
} else { } else {
"".to_string() "".to_string()
@ -333,12 +342,9 @@ impl Menu {
Self::parse(url, gopher_response) Self::parse(url, gopher_response)
} }
pub fn links(&self) -> impl Iterator<Item = &Line> {
self.lines.iter().filter(|&line| line.link > 0)
}
fn parse(url: String, raw: String) -> Menu { fn parse(url: String, raw: String) -> Menu {
let mut lines = vec![]; let mut lines = vec![];
let mut links = vec![];
let mut link = 0; let mut link = 0;
let mut longest = 0; let mut longest = 0;
for line in raw.split_terminator("\n") { for line in raw.split_terminator("\n") {
@ -385,6 +391,10 @@ impl Menu {
longest = name.len(); longest = name.len();
} }
if link > 0 {
links.push(lines.len());
}
lines.push(Line { lines.push(Line {
name, name,
url, url,
@ -397,6 +407,7 @@ impl Menu {
Menu { Menu {
url, url,
lines, lines,
links,
longest, longest,
} }
} }