Don't crash when can't read a character from the terminal

This commit is contained in:
Roey Darwish Dror
2021-01-30 14:57:35 +02:00
parent ab8abea6be
commit 440ad06768

View File

@@ -4,7 +4,7 @@ use crate::utils::which;
use chrono::{Local, Timelike}; use chrono::{Local, Timelike};
use console::{style, Term}; use console::{style, Term};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::debug; use log::{debug, error};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use notify_rust::{Notification, Timeout}; use notify_rust::{Notification, Timeout};
use std::cmp::{max, min}; use std::cmp::{max, min};
@@ -221,15 +221,19 @@ impl Terminal {
.ok(); .ok();
let answer = loop { let answer = loop {
match self.term.read_char()? { match self.term.read_char() {
'y' | 'Y' => break Ok(true), Ok('y') | Ok('Y') => break Ok(true),
's' | 'S' => { Ok('s') | Ok('S') => {
println!("\n\nDropping you to shell. Fix what you need and then exit the shell.\n"); println!("\n\nDropping you to shell. Fix what you need and then exit the shell.\n");
run_shell(); run_shell();
break Ok(true); break Ok(true);
} }
'n' | 'N' | '\r' | '\n' => break Ok(false), Ok('n') | Ok('N') | Ok('\r') | Ok('\n') => break Ok(false),
'q' | 'Q' => return Err(io::Error::from(io::ErrorKind::Interrupted)), Err(e) => {
error!("Error reading from terminal: {}", e);
break Ok(false);
}
Ok('q') | Ok('Q') => return Err(io::Error::from(io::ErrorKind::Interrupted)),
_ => (), _ => (),
} }
}; };