diff options
Diffstat (limited to '')
| -rw-r--r-- | src/ui.rs | 18 |
1 files changed, 17 insertions, 1 deletions
| @@ -14,7 +14,13 @@ pub fn stdin_is_tty() -> bool { | |||
| 14 | 14 | ||
| 15 | fn color() -> bool { | 15 | fn color() -> bool { |
| 16 | static COLOR: OnceLock<bool> = OnceLock::new(); | 16 | static COLOR: OnceLock<bool> = OnceLock::new(); |
| 17 | *COLOR.get_or_init(stdout_is_tty) | 17 | *COLOR.get_or_init(|| { |
| 18 | stdout_is_tty() | ||
| 19 | && std::env::var_os("NO_COLOR").is_none() | ||
| 20 | && std::env::var("TERM") | ||
| 21 | .map(|term| term != "dumb") | ||
| 22 | .unwrap_or(true) | ||
| 23 | }) | ||
| 18 | } | 24 | } |
| 19 | 25 | ||
| 20 | fn paint(code: &str, text: &str) -> String { | 26 | fn paint(code: &str, text: &str) -> String { |
| @@ -177,10 +183,20 @@ impl Approver { | |||
| 177 | print!("\n{} {} ", question, paint("2", hint)); | 183 | print!("\n{} {} ", question, paint("2", hint)); |
| 178 | let _ = std::io::stdout().flush(); | 184 | let _ = std::io::stdout().flush(); |
| 179 | 185 | ||
| 186 | // Typeahead from before the prompt appeared must not answer it: a "y" | ||
| 187 | // typed during streaming would otherwise approve a command unseen. | ||
| 188 | unsafe { | ||
| 189 | libc::tcflush(libc::STDIN_FILENO, libc::TCIFLUSH); | ||
| 190 | } | ||
| 191 | |||
| 180 | let mut line = String::new(); | 192 | let mut line = String::new(); |
| 181 | if std::io::stdin().read_line(&mut line).unwrap_or(0) == 0 { | 193 | if std::io::stdin().read_line(&mut line).unwrap_or(0) == 0 { |
| 182 | return Answer::No; | 194 | return Answer::No; |
| 183 | } | 195 | } |
| 196 | // Ctrl-C during the prompt arrives as a signal, not a keystroke. | ||
| 197 | if crate::signal::CANCEL.load(std::sync::atomic::Ordering::Relaxed) { | ||
| 198 | return Answer::No; | ||
| 199 | } | ||
| 184 | match line.trim().to_ascii_lowercase().as_str() { | 200 | match line.trim().to_ascii_lowercase().as_str() { |
| 185 | "y" | "yes" => Answer::Yes, | 201 | "y" | "yes" => Answer::Yes, |
| 186 | "a" | "always" if allow_always => Answer::Always, | 202 | "a" | "always" if allow_always => Answer::Always, |
