confirm() download

pull/6/head
dvkt 5 years ago
parent 782e156625
commit 2e57d4051b

@ -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

@ -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<String> {
let (_cols, rows) = terminal_size().unwrap();

Loading…
Cancel
Save