termion/src/extra.rs

27 lines
824 B
Rust
Raw Normal View History

2016-03-07 21:19:35 +00:00
use std::io::{Read, Write};
2016-03-08 07:51:34 +00:00
use {IntoRawMode, TerminalError};
/// Extension to `Read` trait.
pub trait ReadExt {
/// Read a password.
2016-03-08 07:51:34 +00:00
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError>;
}
impl<R: Read> ReadExt for R {
2016-03-08 07:51:34 +00:00
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError> {
let _raw = try!(writer.into_raw_mode());
let mut string = String::with_capacity(30);
for c in self.chars() {
2016-03-08 07:51:34 +00:00
match c {
Err(_) => return Err(TerminalError::StdinError),
Ok('\0') | Ok('\x03') | Ok('\x04') => return Ok(None),
Ok('\n') | Ok('\r') => return Ok(Some(string)),
Ok(c) => string.push(c),
}
}
2016-03-08 07:51:34 +00:00
Ok(Some(string))
}
}