Skip to content

Commit

Permalink
read_char: Handle non-tty terminals explicitly (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalusai committed Jan 14, 2023
1 parent 357ca1d commit ac6f2e3
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/term.rs
Expand Up @@ -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<char> {
if !self.is_tty {
return Err(io::Error::new(
io::ErrorKind::NotConnected,
"Not a terminal",
));
}
loop {
match self.read_key()? {
Key::Char(c) => {
Expand All @@ -255,12 +262,6 @@ impl Term {
Key::Enter => {
return Ok('\n');
}
Key::Unknown => {
return Err(io::Error::new(
io::ErrorKind::NotConnected,
"Not a terminal",
))
}
_ => {}
}
}
Expand Down Expand Up @@ -324,12 +325,6 @@ impl Term {
self.write_line("")?;
break;
}
Key::Unknown => {
return Err(io::Error::new(
io::ErrorKind::NotConnected,
"Not a terminal",
))
}
_ => (),
}
}
Expand Down

0 comments on commit ac6f2e3

Please sign in to comment.