From ac6f2e38b11b1bbe0f235b2de3b40f96f96e9ae5 Mon Sep 17 00:00:00 2001 From: Benjamin Fox Date: Sun, 15 Jan 2023 09:49:55 +1300 Subject: [PATCH] read_char: Handle non-tty terminals explicitly (#124) --- src/term.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/term.rs b/src/term.rs index 1715a2ff..0a402585 100644 --- a/src/term.rs +++ b/src/term.rs @@ -245,8 +245,15 @@ impl Term { /// Read a single character from the terminal. /// /// This does not echo the character and blocks until a single character - /// is entered. + /// or complete key chord is entered. If the terminal is not user attended + /// the return value will be an error. pub fn read_char(&self) -> io::Result { + if !self.is_tty { + return Err(io::Error::new( + io::ErrorKind::NotConnected, + "Not a terminal", + )); + } loop { match self.read_key()? { Key::Char(c) => { @@ -255,12 +262,6 @@ impl Term { Key::Enter => { return Ok('\n'); } - Key::Unknown => { - return Err(io::Error::new( - io::ErrorKind::NotConnected, - "Not a terminal", - )) - } _ => {} } } @@ -324,12 +325,6 @@ impl Term { self.write_line("")?; break; } - Key::Unknown => { - return Err(io::Error::new( - io::ErrorKind::NotConnected, - "Not a terminal", - )) - } _ => (), } }