Ctrl+C handling (#75)

As stated [here](https://doc.rust-lang.org/std/io/trait.BufRead.html#errors-1), `read_until` (and `read_line`) ignore Ctrl+C, so Topgrade does not respond to Ctrl+C in the retry prompt, and instead will exit only when enter is pressed after Ctrl+C. This is undesirable, so this pull request is a WIP until we find a solution.
This commit is contained in:
Roey Darwish Dror
2018-10-17 14:07:58 +03:00
committed by GitHub
parent 41621bd08f
commit a6b6b7aa4e
7 changed files with 385 additions and 81 deletions

View File

@@ -1,5 +1,6 @@
use console::Term;
use std::cmp::{max, min};
use std::io::{stdin, Write};
use std::io::{self, Write};
use term_size;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
@@ -67,29 +68,27 @@ impl Terminal {
let _ = self.stdout.flush();
}
pub fn should_retry(&mut self) -> bool {
pub fn should_retry(&mut self, running: bool) -> Result<bool, io::Error> {
if self.width.is_none() {
return false;
return Ok(false);
}
println!();
loop {
let mut result = String::new();
let _ = self
.stdout
.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true));
let _ = write!(&mut self.stdout, "Retry? [y/N] ");
if !running {
write!(&mut self.stdout, "(Press Ctrl+C again to stop Topgrade) ");
}
let _ = self.stdout.reset();
let _ = self.stdout.flush();
if stdin().read_line(&mut result).is_ok() {
match result.as_str().trim() {
"y" | "Y" => return true,
"n" | "N" | "" => return false,
_ => (),
}
} else {
return false;
match Term::stdout().read_char()? {
'y' | 'Y' => return Ok(true),
'n' | 'N' | '\n' => return Ok(false),
_ => (),
}
}
}