diff --git a/README.md b/README.md index 877cdb0..d7e34cc 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,9 @@ Just unzip/untar the `phetch` program into your $PATH and get going! ## todo -- [ ] confirm() helper (downloads, other questions) - [ ] tests for scrolling - gopher://zaibatsu.circumlunar.space/1/~cardboard64/ - [ ] telnet - [ ] ipv6 -- [ ] cancel download - [ ] flesh out help - [ ] new screenshots - [ ] little GIF screencast in README diff --git a/src/ui.rs b/src/ui.rs index 2b6070d..2fd757b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -120,7 +120,12 @@ impl UI { // binary downloads let (typ, _, _, _) = gopher::parse_url(url); if typ.is_download() { - return self.download(url); + self.dirty = true; + return if confirm(&format!("Download {}?", url)) { + self.download(url) + } else { + Ok(()) + }; } self.fetch(url).and_then(|page| { @@ -426,6 +431,31 @@ fn open_external(url: &str) -> Result<()> { .and_then(|_| Ok(())) } +/// Ask user to confirm action with ENTER or Y. +pub fn confirm(question: &str) -> bool { + let (_cols, rows) = terminal_size().unwrap(); + + print!( + "{}{}{}{} [Y/n]: {}", + color::Fg(color::Reset), + termion::cursor::Goto(1, rows), + termion::clear::CurrentLine, + question, + termion::cursor::Show, + ); + stdout().flush(); + + if let Some(Ok(key)) = stdin().keys().next() { + match key { + Key::Char('\n') => true, + Key::Char('y') | Key::Char('Y') => true, + _ => false, + } + } else { + false + } +} + /// Prompt user for input and return what was entered, if anything. pub fn prompt(prompt: &str) -> Option { let (_cols, rows) = terminal_size().unwrap();