ctrl+c to cancel download

pull/6/head
dvkt 5 years ago
parent ea0b8f529b
commit 06d5bf1b9d

@ -4,6 +4,7 @@ use std::net::TcpStream;
use std::net::ToSocketAddrs;
use std::os::unix::fs::OpenOptionsExt;
use std::time::Duration;
use termion::input::TermRead;
pub const TCP_TIMEOUT_IN_SECS: u64 = 8;
pub const TCP_TIMEOUT_DURATION: Duration = Duration::from_secs(TCP_TIMEOUT_IN_SECS);
@ -120,6 +121,8 @@ pub fn download_url(url: &str) -> Result<(String, usize)> {
.ok_or_else(|| error!("Bad download filename: {}", sel))?;
let mut path = std::path::PathBuf::from(".");
path.push(filename);
let stdin = termion::async_stdin();
let mut keys = stdin.keys();
format!("{}:{}", host, port)
.to_socket_addrs()
@ -148,6 +151,9 @@ pub fn download_url(url: &str) -> Result<(String, usize)> {
}
bytes += count;
file_buffer.write_all(&buf);
if let Some(Ok(termion::event::Key::Ctrl('c'))) = keys.next() {
return Err(error!("Download canceled"));
}
}
Ok((filename.to_string(), bytes))
})

@ -131,16 +131,18 @@ impl UI {
fn download(&mut self, url: &str) -> Result<()> {
let url = url.to_string();
self.spinner("", move || gopher::download_url(&url))
.and_then(|res| res)
.and_then(|(path, bytes)| {
self.set_status(format!(
"Download complete! {} saved to {}",
human_bytes(bytes),
path
));
Ok(())
})
self.spinner(&format!("Downloading {}", url), move || {
gopher::download_url(&url)
})
.and_then(|res| res)
.and_then(|(path, bytes)| {
self.set_status(format!(
"Download complete! {} saved to {}",
human_bytes(bytes),
path
));
Ok(())
})
}
fn fetch(&mut self, url: &str) -> Result<Page> {

Loading…
Cancel
Save