2016-09-07 10:39:32 +01:00
|
|
|
//! User input.
|
2016-07-23 15:40:27 +01:00
|
|
|
|
2016-03-15 19:32:25 +00:00
|
|
|
use std::io::{self, Read, Write};
|
2016-07-23 19:41:43 +01:00
|
|
|
use std::ops;
|
2016-03-15 19:32:25 +00:00
|
|
|
|
2016-07-23 19:41:43 +01:00
|
|
|
use event::{parse_event, Event, Key};
|
2016-07-23 15:40:27 +01:00
|
|
|
use raw::IntoRawMode;
|
2016-03-07 17:42:11 +00:00
|
|
|
|
2016-03-09 08:39:22 +00:00
|
|
|
/// An iterator over input keys.
|
2016-10-26 10:53:36 +01:00
|
|
|
pub struct Keys<R> {
|
|
|
|
iter: Events<R>,
|
2016-07-19 01:31:34 +01:00
|
|
|
}
|
2016-07-15 06:41:31 +01:00
|
|
|
|
2016-10-26 10:53:36 +01:00
|
|
|
impl<R: Read> Iterator for Keys<R> {
|
2016-07-19 01:31:34 +01:00
|
|
|
type Item = Result<Key, io::Error>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<Key, io::Error>> {
|
|
|
|
loop {
|
|
|
|
match self.iter.next() {
|
2016-07-20 00:00:22 +01:00
|
|
|
Some(Ok(Event::Key(k))) => return Some(Ok(k)),
|
2016-07-19 01:31:34 +01:00
|
|
|
Some(Ok(_)) => continue,
|
|
|
|
e @ Some(Err(_)) => e,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 08:39:22 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 01:31:34 +01:00
|
|
|
/// An iterator over input events.
|
2016-10-26 10:53:36 +01:00
|
|
|
pub struct Events<R> {
|
|
|
|
source: R,
|
|
|
|
leftover: Option<u8>,
|
2016-03-09 08:39:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:53:36 +01:00
|
|
|
impl<R: Read> Iterator for Events<R> {
|
2016-07-19 01:31:34 +01:00
|
|
|
type Item = Result<Event, io::Error>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Result<Event, io::Error>> {
|
2016-10-26 10:53:36 +01:00
|
|
|
let mut source = &mut self.source;
|
|
|
|
|
|
|
|
if let Some(c) = self.leftover {
|
|
|
|
// we have a leftover byte, use it
|
|
|
|
self.leftover = None;
|
|
|
|
return Some(parse_event(Ok(c), &mut source.bytes()).or(Ok(Event::Unsupported)));
|
2016-07-19 01:31:34 +01:00
|
|
|
}
|
2016-10-26 10:53:36 +01:00
|
|
|
|
|
|
|
// Here we read two bytes at a time. We need to distinguish between single ESC key presses,
|
|
|
|
// and escape sequences (which start with ESC or a x1B byte). The idea is that if this is
|
|
|
|
// an escape sequence, we will read multiple bytes (the first byte being ESC) but if this
|
|
|
|
// is a single ESC keypress, we will only read a single byte.
|
|
|
|
let mut buf = [0u8; 2];
|
|
|
|
let res = match source.read(&mut buf) {
|
|
|
|
Ok(0) => return None,
|
|
|
|
Ok(1) => match buf[0] {
|
|
|
|
b'\x1B' => Ok(Event::Key(Key::Esc)),
|
|
|
|
c => parse_event(Ok(c), &mut source.bytes()),
|
|
|
|
},
|
|
|
|
Ok(2) => {
|
|
|
|
if buf[0] != b'\x1B' {
|
|
|
|
// this is not an escape sequence, but we read two bytes, save the second byte
|
|
|
|
// for later
|
|
|
|
self.leftover = Some(buf[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut iter = buf[1..2].iter().map(|c| Ok(*c)).chain(source.bytes());
|
|
|
|
parse_event(Ok(buf[0]), &mut iter)
|
|
|
|
}
|
|
|
|
Ok(_) => unreachable!(),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(res.or(Ok(Event::Unsupported)))
|
2016-03-09 08:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:42:11 +00:00
|
|
|
/// Extension to `Read` trait.
|
2016-03-08 20:39:24 +00:00
|
|
|
pub trait TermRead {
|
2016-07-19 01:31:34 +01:00
|
|
|
/// An iterator over input events.
|
2016-10-26 10:53:36 +01:00
|
|
|
fn events(self) -> Events<Self> where Self: Sized;
|
2016-07-19 01:31:34 +01:00
|
|
|
|
2016-03-09 08:39:22 +00:00
|
|
|
/// An iterator over key inputs.
|
2016-10-26 10:53:36 +01:00
|
|
|
fn keys(self) -> Keys<Self> where Self: Sized;
|
2016-03-09 08:39:22 +00:00
|
|
|
|
2016-03-20 15:15:05 +00:00
|
|
|
/// 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>>;
|
|
|
|
|
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
|
2016-03-20 15:15:05 +00:00
|
|
|
/// complete the input.
|
|
|
|
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> io::Result<Option<String>> {
|
|
|
|
let _raw = try!(writer.into_raw_mode());
|
|
|
|
self.read_line()
|
|
|
|
}
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 20:39:24 +00:00
|
|
|
impl<R: Read> TermRead for R {
|
2016-10-26 10:53:36 +01:00
|
|
|
fn events(self) -> Events<Self> {
|
2016-07-19 01:31:34 +01:00
|
|
|
Events {
|
2016-10-26 10:53:36 +01:00
|
|
|
source: self,
|
|
|
|
leftover: None,
|
2016-07-19 01:31:34 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 10:53:36 +01:00
|
|
|
fn keys(self) -> Keys<Self> {
|
2016-03-09 08:39:22 +00:00
|
|
|
Keys {
|
2016-07-19 01:31:34 +01:00
|
|
|
iter: self.events(),
|
2016-03-09 08:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:15:05 +00:00
|
|
|
fn read_line(&mut self) -> io::Result<Option<String>> {
|
|
|
|
let mut buf = 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-04-02 18:20:47 +01:00
|
|
|
Ok(0x7f) => { buf.pop(); },
|
2016-03-08 10:29:16 +00:00
|
|
|
Ok(b'\n') | Ok(b'\r') => break,
|
2016-03-20 15:15:05 +00:00
|
|
|
Ok(c) => buf.push(c),
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:15:05 +00:00
|
|
|
let string = try!(String::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)));
|
|
|
|
Ok(Some(string))
|
2016-03-07 17:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 16:18:31 +00:00
|
|
|
|
2016-07-23 19:41:43 +01:00
|
|
|
/// A sequence of escape codes to enable terminal mouse support.
|
|
|
|
const ENTER_MOUSE_SEQUENCE: &'static str = csi!("?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h");
|
|
|
|
|
|
|
|
/// A sequence of escape codes to disable terminal mouse support.
|
|
|
|
const EXIT_MOUSE_SEQUENCE: &'static str = csi!("?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l");
|
|
|
|
|
|
|
|
/// A terminal with added mouse support.
|
|
|
|
///
|
|
|
|
/// This can be obtained through the `From` implementations.
|
|
|
|
pub struct MouseTerminal<W: Write> {
|
|
|
|
term: W,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> From<W> for MouseTerminal<W> {
|
|
|
|
fn from(mut from: W) -> MouseTerminal<W> {
|
|
|
|
from.write(ENTER_MOUSE_SEQUENCE.as_bytes()).unwrap();
|
|
|
|
|
|
|
|
MouseTerminal { term: from }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> Drop for MouseTerminal<W> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.term.write(EXIT_MOUSE_SEQUENCE.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> ops::Deref for MouseTerminal<W> {
|
|
|
|
type Target = W;
|
|
|
|
|
|
|
|
fn deref(&self) -> &W {
|
|
|
|
&self.term
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> ops::DerefMut for MouseTerminal<W> {
|
|
|
|
fn deref_mut(&mut self) -> &mut W {
|
|
|
|
&mut self.term
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> Write for MouseTerminal<W> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.term.write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.term.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:18:31 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2016-04-02 17:05:31 +01:00
|
|
|
use super::*;
|
|
|
|
use std::io;
|
2016-07-20 12:06:04 +01:00
|
|
|
use event::{Key, Event, MouseEvent, MouseButton};
|
2016-04-02 17:05:31 +01:00
|
|
|
|
2016-03-09 16:18:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_keys() {
|
|
|
|
let mut i = b"\x1Bayo\x7F\x1B[D".keys();
|
|
|
|
|
2016-06-14 13:24:07 +01:00
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Key::Alt('a'));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Key::Char('y'));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Key::Char('o'));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Key::Backspace);
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Key::Left);
|
|
|
|
assert!(i.next().is_none());
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|
2016-04-02 17:05:31 +01:00
|
|
|
|
2016-07-20 12:06:04 +01:00
|
|
|
#[test]
|
|
|
|
fn test_events() {
|
|
|
|
let mut i = b"\x1B[\x00bc\x7F\x1B[D\
|
|
|
|
\x1B[M\x00\x22\x24\x1B[<0;2;4;M\x1B[32;2;4M\x1B[<0;2;4;m\x1B[35;2;4Mb".events();
|
|
|
|
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Unsupported);
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Char('b')));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Char('c')));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Backspace));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Left));
|
2016-07-24 19:22:47 +01:00
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Mouse(MouseEvent::Press(MouseButton::WheelUp, 2, 4)));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4)));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4)));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Mouse(MouseEvent::Release(2, 4)));
|
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Mouse(MouseEvent::Release(2, 4)));
|
2016-07-20 12:06:04 +01:00
|
|
|
assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Char('b')));
|
|
|
|
assert!(i.next().is_none());
|
|
|
|
}
|
|
|
|
|
2016-07-16 21:46:29 +01:00
|
|
|
#[test]
|
|
|
|
fn test_function_keys() {
|
|
|
|
let mut st = b"\x1BOP\x1BOQ\x1BOR\x1BOS".keys();
|
|
|
|
for i in 1 .. 5 {
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::F(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut st = b"\x1B[11~\x1B[12~\x1B[13~\x1B[14~\x1B[15~\
|
|
|
|
\x1B[17~\x1B[18~\x1B[19~\x1B[20~\x1B[21~\x1B[23~\x1B[24~".keys();
|
|
|
|
for i in 1 .. 13 {
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::F(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_special_keys() {
|
|
|
|
let mut st = b"\x1B[2~\x1B[H\x1B[7~\x1B[5~\x1B[3~\x1B[F\x1B[8~\x1B[6~".keys();
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::Insert);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::Home);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::Home);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::PageUp);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::Delete);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::End);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::End);
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::PageDown);
|
|
|
|
assert!(st.next().is_none());
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:53:36 +01:00
|
|
|
#[test]
|
|
|
|
fn test_esc_key() {
|
|
|
|
let mut st = b"\x1B".keys();
|
|
|
|
assert_eq!(st.next().unwrap().unwrap(), Key::Esc);
|
|
|
|
assert!(st.next().is_none());
|
|
|
|
}
|
|
|
|
|
2016-04-02 18:20:47 +01:00
|
|
|
fn line_match(a: &str, b: Option<&str>) {
|
|
|
|
let mut sink = io::sink();
|
|
|
|
|
|
|
|
let line = a.as_bytes().read_line().unwrap();
|
|
|
|
let pass = a.as_bytes().read_passwd(&mut sink).unwrap();
|
|
|
|
|
|
|
|
// godammit rustc
|
|
|
|
|
|
|
|
assert_eq!(line, pass);
|
|
|
|
|
|
|
|
if let Some(l) = line {
|
|
|
|
assert_eq!(Some(l.as_str()), b);
|
|
|
|
} else {
|
|
|
|
assert!(b.is_none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 17:05:31 +01:00
|
|
|
#[test]
|
2016-04-02 18:20:47 +01:00
|
|
|
fn test_read() {
|
2016-04-02 17:05:31 +01:00
|
|
|
let test1 = "this is the first test";
|
|
|
|
let test2 = "this is the second test";
|
|
|
|
|
2016-04-02 18:20:47 +01:00
|
|
|
line_match(test1, Some(test1));
|
|
|
|
line_match(test2, Some(test2));
|
2016-04-02 17:05:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-04-02 18:20:47 +01:00
|
|
|
fn test_backspace() {
|
|
|
|
line_match("this is the\x7f first\x7f\x7f test", Some("this is th fir test"));
|
|
|
|
line_match("this is the seco\x7fnd test\x7f", Some("this is the secnd tes"));
|
|
|
|
}
|
2016-04-02 17:05:31 +01:00
|
|
|
|
2016-04-02 18:20:47 +01:00
|
|
|
#[test]
|
|
|
|
fn test_end() {
|
|
|
|
line_match("abc\nhttps://www.youtube.com/watch?v=dQw4w9WgXcQ", Some("abc"));
|
|
|
|
line_match("hello\rhttps://www.youtube.com/watch?v=yPYZpwSpKmA", Some("hello"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_abort() {
|
|
|
|
line_match("abc\x03https://www.youtube.com/watch?v=dQw4w9WgXcQ", None);
|
|
|
|
line_match("hello\x04https://www.youtube.com/watch?v=yPYZpwSpKmA", None);
|
2016-04-02 17:05:31 +01:00
|
|
|
}
|
2016-07-16 21:46:29 +01:00
|
|
|
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|