Skip to content

Commit ac6f2e3

Browse files
authored
read_char: Handle non-tty terminals explicitly (#124)
1 parent 357ca1d commit ac6f2e3

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/term.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,15 @@ impl Term {
245245
/// Read a single character from the terminal.
246246
///
247247
/// This does not echo the character and blocks until a single character
248-
/// is entered.
248+
/// or complete key chord is entered. If the terminal is not user attended
249+
/// the return value will be an error.
249250
pub fn read_char(&self) -> io::Result<char> {
251+
if !self.is_tty {
252+
return Err(io::Error::new(
253+
io::ErrorKind::NotConnected,
254+
"Not a terminal",
255+
));
256+
}
250257
loop {
251258
match self.read_key()? {
252259
Key::Char(c) => {
@@ -255,12 +262,6 @@ impl Term {
255262
Key::Enter => {
256263
return Ok('\n');
257264
}
258-
Key::Unknown => {
259-
return Err(io::Error::new(
260-
io::ErrorKind::NotConnected,
261-
"Not a terminal",
262-
))
263-
}
264265
_ => {}
265266
}
266267
}
@@ -324,12 +325,6 @@ impl Term {
324325
self.write_line("")?;
325326
break;
326327
}
327-
Key::Unknown => {
328-
return Err(io::Error::new(
329-
io::ErrorKind::NotConnected,
330-
"Not a terminal",
331-
))
332-
}
333328
_ => (),
334329
}
335330
}

0 commit comments

Comments
 (0)