2016-03-13 10:55:24 +00:00
|
|
|
use std::io::{Read, Write, Error, ErrorKind, Result as IoResult};
|
|
|
|
use IntoRawMode;
|
2016-03-07 17:42:11 +00:00
|
|
|
|
2016-03-09 08:39:22 +00:00
|
|
|
#[cfg(feature = "nightly")]
|
2016-03-09 10:38:43 +00:00
|
|
|
use std::io::{Chars, CharsError};
|
2016-03-09 08:39:22 +00:00
|
|
|
|
|
|
|
/// A key.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum Key {
|
|
|
|
/// Backspace.
|
|
|
|
Backspace,
|
|
|
|
/// Left arrow.
|
|
|
|
Left,
|
|
|
|
/// Right arrow.
|
|
|
|
Right,
|
|
|
|
/// Up arrow.
|
|
|
|
Up,
|
|
|
|
/// Down arrow.
|
|
|
|
Down,
|
|
|
|
/// Normal character.
|
|
|
|
Char(char),
|
2016-03-10 14:12:59 +00:00
|
|
|
/// Alt modified character.
|
|
|
|
Alt(char),
|
|
|
|
/// Ctrl modified character.
|
|
|
|
///
|
|
|
|
/// Note that certain keys may not be modifiable with `ctrl`, due to limitations of terminals.
|
|
|
|
Ctrl(char),
|
2016-03-09 08:39:22 +00:00
|
|
|
/// Invalid character code.
|
|
|
|
Invalid,
|
|
|
|
// TODO handle errors better?
|
|
|
|
/// IO error.
|
|
|
|
Error,
|
2016-03-13 10:55:24 +00:00
|
|
|
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
#[doc(hidden)]
|
|
|
|
__IsNotComplete
|
2016-03-09 08:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over input keys.
|
2016-03-09 12:07:38 +00:00
|
|
|
#[cfg(feature = "nightly")]
|
2016-03-09 10:38:43 +00:00
|
|
|
pub struct Keys<I> {
|
|
|
|
chars: I,
|
2016-03-09 08:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "nightly")]
|
2016-03-09 10:38:43 +00:00
|
|
|
impl<I: Iterator<Item = Result<char, CharsError>>> Iterator for Keys<I> {
|
2016-03-09 08:39:22 +00:00
|
|
|
type Item = Key;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Key> {
|
|
|
|
match self.chars.next() {
|
|
|
|
Some(Ok('\x1B')) => Some(match self.chars.next() {
|
|
|
|
Some(Ok('[')) => match self.chars.next() {
|
|
|
|
Some(Ok('D')) => Key::Left,
|
|
|
|
Some(Ok('C')) => Key::Right,
|
|
|
|
Some(Ok('A')) => Key::Up,
|
|
|
|
Some(Ok('B')) => Key::Down,
|
|
|
|
_ => Key::Invalid,
|
|
|
|
},
|
|
|
|
Some(Ok(c)) => Key::Alt(c),
|
|
|
|
Some(Err(_)) | None => Key::Invalid,
|
|
|
|
}),
|
2016-03-10 20:57:42 +00:00
|
|
|
Some(Ok('\n')) | Some(Ok('\r')) => Some(Key::Char('\n')),
|
|
|
|
Some(Ok('\t')) => Some(Key::Char('\t')),
|
2016-03-09 08:39:22 +00:00
|
|
|
Some(Ok('\x7F')) => Some(Key::Backspace),
|
2016-03-10 14:21:11 +00:00
|
|
|
Some(Ok(c @ '\x01' ... '\x1A')) => Some(Key::Ctrl((c as u8 - 0x1 + b'a') as char)),
|
2016-03-10 14:12:59 +00:00
|
|
|
Some(Ok(c @ '\x1C' ... '\x1F')) => Some(Key::Ctrl((c as u8 - 0x1C + b'4') as char)),
|
2016-03-09 08:39:22 +00:00
|
|
|
Some(Ok(c)) => Some(Key::Char(c)),
|
|
|
|
None => None,
|
|
|
|
Some(Err(_)) => Some(Key::Error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:11 +00:00
|
|
|
/// Extension to `Read` trait.
|
2016-03-08 20:39:24 +00:00
|
|
|
pub trait TermRead {
|
2016-03-09 08:39:22 +00:00
|
|
|
/// An iterator over key inputs.
|
|
|
|
#[cfg(feature = "nightly")]
|
2016-03-09 10:38:43 +00:00
|
|
|
fn keys(self) -> Keys<Chars<Self>> where Self: Sized;
|
2016-03-09 08:39:22 +00:00
|
|
|
|
2016-03-07 17:42:11 +00:00
|
|
|
/// Read a password.
|
2016-03-08 09:08:50 +00:00
|
|
|
///
|
|
|
|
/// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
|
|
|
|
/// complete the password input.
|
2016-03-13 10:55:24 +00:00
|
|
|
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> IoResult<Option<String>>;
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 20:39:24 +00:00
|
|
|
impl<R: Read> TermRead for R {
|
2016-03-09 08:39:22 +00:00
|
|
|
#[cfg(feature = "nightly")]
|
2016-03-09 10:38:43 +00:00
|
|
|
fn keys(self) -> Keys<Chars<R>> {
|
2016-03-09 08:39:22 +00:00
|
|
|
Keys {
|
|
|
|
chars: self.chars(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 10:55:24 +00:00
|
|
|
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> IoResult<Option<String>> {
|
2016-03-08 07:51:34 +00:00
|
|
|
let _raw = try!(writer.into_raw_mode());
|
2016-03-08 10:29:16 +00:00
|
|
|
let mut passbuf = Vec::with_capacity(30);
|
2016-03-07 17:42:11 +00:00
|
|
|
|
2016-03-08 10:04:09 +00:00
|
|
|
for c in self.bytes() {
|
2016-03-08 07:51:34 +00:00
|
|
|
match c {
|
2016-03-13 10:55:24 +00:00
|
|
|
Err(e) => return Err(e),
|
2016-03-08 10:29:22 +00:00
|
|
|
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
|
2016-03-08 10:29:16 +00:00
|
|
|
Ok(b'\n') | Ok(b'\r') => break,
|
|
|
|
Ok(c) => passbuf.push(c),
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 10:55:24 +00:00
|
|
|
let passwd = try!(String::from_utf8(passbuf).map_err(|e| Error::new(ErrorKind::InvalidData, e)));
|
2016-03-08 10:29:16 +00:00
|
|
|
|
|
|
|
Ok(Some(passwd))
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 16:18:31 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
#[test]
|
|
|
|
fn test_keys() {
|
2016-03-09 18:17:00 +00:00
|
|
|
use {TermRead, Key};
|
|
|
|
|
2016-03-09 16:18:31 +00:00
|
|
|
let mut i = b"\x1Bayo\x7F\x1B[D".keys();
|
|
|
|
|
|
|
|
assert_eq!(i.next(), Some(Key::Alt('a')));
|
|
|
|
assert_eq!(i.next(), Some(Key::Char('y')));
|
|
|
|
assert_eq!(i.next(), Some(Key::Char('o')));
|
|
|
|
assert_eq!(i.next(), Some(Key::Backspace));
|
|
|
|
assert_eq!(i.next(), Some(Key::Left));
|
|
|
|
assert_eq!(i.next(), None);
|
|
|
|
}
|
|
|
|
}
|