|
|
|
@ -128,12 +128,64 @@ impl UI {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn download(&mut self, url: &str) -> io::Result<Page> {
|
|
|
|
|
// request thread
|
|
|
|
|
let download_url = url.to_string();
|
|
|
|
|
let req = thread::spawn(move || match gopher::download_url(&download_url) {
|
|
|
|
|
Ok(res) => Ok(res),
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// spinner thead
|
|
|
|
|
let download_url = url.to_string();
|
|
|
|
|
let (spintx, spinrx) = mpsc::channel();
|
|
|
|
|
thread::spawn(move || loop {
|
|
|
|
|
for i in 0..=3 {
|
|
|
|
|
if spinrx.try_recv().is_ok() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
print!(
|
|
|
|
|
"\r{}Downloading {}{}{}{}",
|
|
|
|
|
termion::cursor::Hide,
|
|
|
|
|
color::Fg(color::LightBlack),
|
|
|
|
|
download_url,
|
|
|
|
|
".".repeat(i),
|
|
|
|
|
termion::clear::AfterCursor
|
|
|
|
|
);
|
|
|
|
|
stdout().flush();
|
|
|
|
|
thread::sleep(Duration::from_millis(350));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let work = req.join();
|
|
|
|
|
spintx.send(true); // stop spinner
|
|
|
|
|
self.dirty = true;
|
|
|
|
|
let res = match work {
|
|
|
|
|
Ok(opt) => match opt {
|
|
|
|
|
Ok(body) => body,
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
},
|
|
|
|
|
Err(_) => return Err(io_error("Connection error".into())),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(Box::new(Text::from(
|
|
|
|
|
url.into(),
|
|
|
|
|
format!("Download complete! {}", res),
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fetch(&mut self, url: &str) -> io::Result<Page> {
|
|
|
|
|
// on-line help
|
|
|
|
|
if url.starts_with("gopher://help/") {
|
|
|
|
|
return self.fetch_help(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// binary downloads
|
|
|
|
|
let (typ, _, _, _) = gopher::parse_url(url);
|
|
|
|
|
if typ.is_download() {
|
|
|
|
|
return self.download(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// request thread
|
|
|
|
|
let thread_url = url.to_string();
|
|
|
|
|
let req = thread::spawn(move || match gopher::fetch_url(&thread_url) {
|
|
|
|
@ -170,8 +222,6 @@ impl UI {
|
|
|
|
|
},
|
|
|
|
|
Err(_) => return Err(io_error("Connection error".into())),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (typ, _, _, _) = gopher::parse_url(url);
|
|
|
|
|
match typ {
|
|
|
|
|
Type::Menu | Type::Search => Ok(Box::new(Menu::from(url.to_string(), res))),
|
|
|
|
|
Type::Text | Type::HTML => Ok(Box::new(Text::from(url.to_string(), res))),
|
|
|
|
|