Merge pull request #14 from untitaker/refactor-read-passwd
Refactor read_passwd
This commit is contained in:
commit
b5c4f2512d
26
src/input.rs
26
src/input.rs
|
@ -80,13 +80,23 @@ pub trait TermRead {
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
fn keys(self) -> Keys<Chars<Self>> where Self: Sized;
|
fn keys(self) -> Keys<Chars<Self>> where Self: Sized;
|
||||||
|
|
||||||
|
/// Read a line.
|
||||||
|
///
|
||||||
|
/// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
|
||||||
|
/// complete the input.
|
||||||
|
fn read_line(&mut self) -> io::Result<Option<String>>;
|
||||||
|
|
||||||
/// Read a password.
|
/// Read a password.
|
||||||
///
|
///
|
||||||
/// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
|
/// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
|
||||||
/// complete the password input.
|
/// complete the input.
|
||||||
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> io::Result<Option<String>>;
|
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> io::Result<Option<String>> {
|
||||||
|
let _raw = try!(writer.into_raw_mode());
|
||||||
|
self.read_line()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<R: Read> TermRead for R {
|
impl<R: Read> TermRead for R {
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
fn keys(self) -> Keys<Chars<R>> {
|
fn keys(self) -> Keys<Chars<R>> {
|
||||||
|
@ -95,9 +105,8 @@ impl<R: Read> TermRead for R {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> io::Result<Option<String>> {
|
fn read_line(&mut self) -> io::Result<Option<String>> {
|
||||||
let _raw = try!(writer.into_raw_mode());
|
let mut buf = Vec::with_capacity(30);
|
||||||
let mut passbuf = Vec::with_capacity(30);
|
|
||||||
|
|
||||||
for c in self.bytes() {
|
for c in self.bytes() {
|
||||||
match c {
|
match c {
|
||||||
|
@ -105,13 +114,12 @@ impl<R: Read> TermRead for R {
|
||||||
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
|
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
|
||||||
Ok(0x7f) => { passbuf.pop(); },
|
Ok(0x7f) => { passbuf.pop(); },
|
||||||
Ok(b'\n') | Ok(b'\r') => break,
|
Ok(b'\n') | Ok(b'\r') => break,
|
||||||
Ok(c) => passbuf.push(c),
|
Ok(c) => buf.push(c),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let passwd = try!(String::from_utf8(passbuf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)));
|
let string = try!(String::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)));
|
||||||
|
Ok(Some(string))
|
||||||
Ok(Some(passwd))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue