termion/
input.rs

1//! User input.
2
3use std::io::{self, Read, Write};
4use std::ops;
5use std::os::fd::AsFd;
6
7use event::{self, Event, Key};
8use raw::IntoRawMode;
9
10/// An iterator over input keys.
11pub struct Keys<R> {
12    iter: Events<R>,
13}
14
15impl<R: Read> Iterator for Keys<R> {
16    type Item = Result<Key, io::Error>;
17
18    fn next(&mut self) -> Option<Result<Key, io::Error>> {
19        loop {
20            match self.iter.next() {
21                Some(Ok(Event::Key(k))) => return Some(Ok(k)),
22                Some(Ok(_)) => continue,
23                Some(Err(e)) => return Some(Err(e)),
24                None => return None,
25            };
26        }
27    }
28}
29
30/// An iterator over input events.
31pub struct Events<R> {
32    inner: EventsAndRaw<R>,
33}
34
35impl<R: Read> Iterator for Events<R> {
36    type Item = Result<Event, io::Error>;
37
38    fn next(&mut self) -> Option<Result<Event, io::Error>> {
39        self.inner
40            .next()
41            .map(|tuple| tuple.map(|(event, _raw)| event))
42    }
43}
44
45/// An iterator over input events and the bytes that define them.
46pub struct EventsAndRaw<R> {
47    source: R,
48    leftover: Option<u8>,
49}
50
51impl<R: Read> Iterator for EventsAndRaw<R> {
52    type Item = Result<(Event, Vec<u8>), io::Error>;
53
54    fn next(&mut self) -> Option<Result<(Event, Vec<u8>), io::Error>> {
55        let source = &mut self.source;
56
57        if let Some(c) = self.leftover {
58            // we have a leftover byte, use it
59            self.leftover = None;
60            return Some(parse_event(c, &mut source.bytes()));
61        }
62
63        // Here we read two bytes at a time. We need to distinguish between single ESC key presses,
64        // and escape sequences (which start with ESC or a x1B byte). The idea is that if this is
65        // an escape sequence, we will read multiple bytes (the first byte being ESC) but if this
66        // is a single ESC keypress, we will only read a single byte.
67        let mut buf = [0u8; 2];
68        let res = match source.read(&mut buf) {
69            Ok(0) => return None,
70            Ok(1) => match buf[0] {
71                b'\x1B' => Ok((Event::Key(Key::Esc), vec![b'\x1B'])),
72                c => parse_event(c, &mut source.bytes()),
73            },
74            Ok(2) => {
75                let option_iter = &mut Some(buf[1]).into_iter();
76                let result = {
77                    let mut iter = option_iter.map(|c| Ok(c)).chain(source.bytes());
78                    parse_event(buf[0], &mut iter)
79                };
80                // If the option_iter wasn't consumed, keep the byte for later.
81                self.leftover = option_iter.next();
82                result
83            }
84            Ok(_) => unreachable!(),
85            Err(e) => Err(e),
86        };
87
88        Some(res)
89    }
90}
91
92fn parse_event<I>(item: u8, iter: &mut I) -> Result<(Event, Vec<u8>), io::Error>
93where
94    I: Iterator<Item = Result<u8, io::Error>>,
95{
96    let mut buf = vec![item];
97    let result = {
98        let mut iter = iter.inspect(|byte| {
99            if let &Ok(byte) = byte {
100                buf.push(byte);
101            }
102        });
103        event::parse_event(item, &mut iter)
104    };
105    result
106        .or(Ok(Event::Unsupported(buf.clone())))
107        .map(|e| (e, buf))
108}
109
110/// Extension to `Read` trait.
111pub trait TermRead {
112    /// An iterator over input events.
113    fn events(self) -> Events<Self>
114    where
115        Self: Sized;
116
117    /// An iterator over key inputs.
118    fn keys(self) -> Keys<Self>
119    where
120        Self: Sized;
121
122    /// Read a line.
123    ///
124    /// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
125    /// complete the input.
126    fn read_line(&mut self) -> io::Result<Option<String>>;
127
128    /// Read a password.
129    ///
130    /// EOT and ETX will abort the prompt, returning `None`. Newline or carriage return will
131    /// complete the input.
132    fn read_passwd<W: Write + AsFd>(&mut self, writer: &mut W) -> io::Result<Option<String>> {
133        let _raw = writer.into_raw_mode()?;
134        self.read_line()
135    }
136}
137
138impl<R: Read + TermReadEventsAndRaw> TermRead for R {
139    fn events(self) -> Events<Self> {
140        Events {
141            inner: self.events_and_raw(),
142        }
143    }
144    fn keys(self) -> Keys<Self> {
145        Keys {
146            iter: self.events(),
147        }
148    }
149
150    fn read_line(&mut self) -> io::Result<Option<String>> {
151        let mut buf = Vec::with_capacity(30);
152
153        for c in self.bytes() {
154            match c {
155                Err(e) => return Err(e),
156                Ok(0) | Ok(3) | Ok(4) => return Ok(None),
157                Ok(0x7f) => {
158                    buf.pop();
159                }
160                Ok(b'\n') | Ok(b'\r') => break,
161                Ok(c) => buf.push(c),
162            }
163        }
164
165        let string =
166            String::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
167        Ok(Some(string))
168    }
169}
170
171/// Extension to `TermRead` trait. A separate trait in order to maintain backwards compatibility.
172pub trait TermReadEventsAndRaw {
173    /// An iterator over input events and the bytes that define them.
174    fn events_and_raw(self) -> EventsAndRaw<Self>
175    where
176        Self: Sized;
177}
178
179impl<R: Read> TermReadEventsAndRaw for R {
180    fn events_and_raw(self) -> EventsAndRaw<Self> {
181        EventsAndRaw {
182            source: self,
183            leftover: None,
184        }
185    }
186}
187
188/// A sequence of escape codes to enable terminal mouse support.
189const ENTER_MOUSE_SEQUENCE: &'static str = csi!("?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h");
190
191/// A sequence of escape codes to disable terminal mouse support.
192const EXIT_MOUSE_SEQUENCE: &'static str = csi!("?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l");
193
194/// A terminal with added mouse support.
195///
196/// This can be obtained through the `From` implementations.
197pub struct MouseTerminal<W: Write> {
198    term: W,
199}
200
201impl<W: Write> From<W> for MouseTerminal<W> {
202    fn from(mut from: W) -> MouseTerminal<W> {
203        from.write_all(ENTER_MOUSE_SEQUENCE.as_bytes()).unwrap();
204
205        MouseTerminal { term: from }
206    }
207}
208
209impl<W: Write> Drop for MouseTerminal<W> {
210    fn drop(&mut self) {
211        self.term.write_all(EXIT_MOUSE_SEQUENCE.as_bytes()).unwrap();
212    }
213}
214
215impl<W: Write> ops::Deref for MouseTerminal<W> {
216    type Target = W;
217
218    fn deref(&self) -> &W {
219        &self.term
220    }
221}
222
223impl<W: Write> ops::DerefMut for MouseTerminal<W> {
224    fn deref_mut(&mut self) -> &mut W {
225        &mut self.term
226    }
227}
228
229impl<W: Write> Write for MouseTerminal<W> {
230    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
231        self.term.write(buf)
232    }
233
234    fn flush(&mut self) -> io::Result<()> {
235        self.term.flush()
236    }
237}
238
239#[cfg(unix)]
240mod unix_impl {
241    use super::*;
242    use std::os::unix::io::{AsRawFd, RawFd};
243
244    impl<W: Write + AsRawFd> AsRawFd for MouseTerminal<W> {
245        fn as_raw_fd(&self) -> RawFd {
246            self.term.as_raw_fd()
247        }
248    }
249}
250
251#[cfg(test)]
252mod test {
253    use super::*;
254    use event::{Event, Key, MouseButton, MouseEvent};
255
256    #[test]
257    fn test_keys() {
258        let mut i = b"\x1Bayo\x7F\x1B[D".keys();
259
260        assert_eq!(i.next().unwrap().unwrap(), Key::Alt('a'));
261        assert_eq!(i.next().unwrap().unwrap(), Key::Char('y'));
262        assert_eq!(i.next().unwrap().unwrap(), Key::Char('o'));
263        assert_eq!(i.next().unwrap().unwrap(), Key::Backspace);
264        assert_eq!(i.next().unwrap().unwrap(), Key::Left);
265        assert!(i.next().is_none());
266    }
267
268    #[test]
269    fn test_events() {
270        let mut i = b"\x1B[\x00bc\x7F\x1B[D\
271                    \x1B[M\x00\x22\x24\x1B[<0;2;4;M\x1B[32;2;4M\x1B[<0;2;4;m\x1B[35;2;4Mb"
272            .events();
273
274        assert_eq!(
275            i.next().unwrap().unwrap(),
276            Event::Unsupported(vec![0x1B, b'[', 0x00])
277        );
278        assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Char('b')));
279        assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Char('c')));
280        assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Backspace));
281        assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Left));
282        assert_eq!(
283            i.next().unwrap().unwrap(),
284            Event::Mouse(MouseEvent::Press(MouseButton::WheelUp, 2, 4))
285        );
286        assert_eq!(
287            i.next().unwrap().unwrap(),
288            Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))
289        );
290        assert_eq!(
291            i.next().unwrap().unwrap(),
292            Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))
293        );
294        assert_eq!(
295            i.next().unwrap().unwrap(),
296            Event::Mouse(MouseEvent::Release(2, 4))
297        );
298        assert_eq!(
299            i.next().unwrap().unwrap(),
300            Event::Mouse(MouseEvent::Release(2, 4))
301        );
302        assert_eq!(i.next().unwrap().unwrap(), Event::Key(Key::Char('b')));
303        assert!(i.next().is_none());
304    }
305
306    #[test]
307    fn test_events_and_raw() {
308        let input = b"\x1B[\x00bc\x7F\x1B[D\
309                    \x1B[M\x00\x22\x24\x1B[<0;2;4;M\x1B[32;2;4M\x1B[<0;2;4;m\x1B[35;2;4Mb";
310        let mut output = Vec::<u8>::new();
311        {
312            let mut i = input
313                .events_and_raw()
314                .map(|res| res.unwrap())
315                .inspect(|&(_, ref raw)| {
316                    output.extend(raw);
317                })
318                .map(|(event, _)| event);
319
320            assert_eq!(
321                i.next().unwrap(),
322                Event::Unsupported(vec![0x1B, b'[', 0x00])
323            );
324            assert_eq!(i.next().unwrap(), Event::Key(Key::Char('b')));
325            assert_eq!(i.next().unwrap(), Event::Key(Key::Char('c')));
326            assert_eq!(i.next().unwrap(), Event::Key(Key::Backspace));
327            assert_eq!(i.next().unwrap(), Event::Key(Key::Left));
328            assert_eq!(
329                i.next().unwrap(),
330                Event::Mouse(MouseEvent::Press(MouseButton::WheelUp, 2, 4))
331            );
332            assert_eq!(
333                i.next().unwrap(),
334                Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))
335            );
336            assert_eq!(
337                i.next().unwrap(),
338                Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))
339            );
340            assert_eq!(i.next().unwrap(), Event::Mouse(MouseEvent::Release(2, 4)));
341            assert_eq!(i.next().unwrap(), Event::Mouse(MouseEvent::Release(2, 4)));
342            assert_eq!(i.next().unwrap(), Event::Key(Key::Char('b')));
343            assert!(i.next().is_none());
344        }
345
346        assert_eq!(input.iter().map(|b| *b).collect::<Vec<u8>>(), output)
347    }
348
349    #[test]
350    fn test_function_keys() {
351        let mut st = b"\x1BOP\x1BOQ\x1BOR\x1BOS".keys();
352        for i in 1..5 {
353            assert_eq!(st.next().unwrap().unwrap(), Key::F(i));
354        }
355
356        let mut st = b"\x1B[11~\x1B[12~\x1B[13~\x1B[14~\x1B[15~\
357        \x1B[17~\x1B[18~\x1B[19~\x1B[20~\x1B[21~\x1B[23~\x1B[24~"
358            .keys();
359        for i in 1..13 {
360            assert_eq!(st.next().unwrap().unwrap(), Key::F(i));
361        }
362    }
363
364    #[test]
365    fn test_special_keys() {
366        let mut st = b"\x1B[2~\x1B[H\x1B[7~\x1B[5~\x1B[3~\x1B[F\x1B[8~\x1B[6~".keys();
367        assert_eq!(st.next().unwrap().unwrap(), Key::Insert);
368        assert_eq!(st.next().unwrap().unwrap(), Key::Home);
369        assert_eq!(st.next().unwrap().unwrap(), Key::Home);
370        assert_eq!(st.next().unwrap().unwrap(), Key::PageUp);
371        assert_eq!(st.next().unwrap().unwrap(), Key::Delete);
372        assert_eq!(st.next().unwrap().unwrap(), Key::End);
373        assert_eq!(st.next().unwrap().unwrap(), Key::End);
374        assert_eq!(st.next().unwrap().unwrap(), Key::PageDown);
375        assert!(st.next().is_none());
376    }
377
378    #[test]
379    fn test_esc_key() {
380        let mut st = b"\x1B".keys();
381        assert_eq!(st.next().unwrap().unwrap(), Key::Esc);
382        assert!(st.next().is_none());
383    }
384
385    fn line_match(a: &str, b: Option<&str>) {
386        let line = a.as_bytes().read_line().unwrap();
387        let pass = a.as_bytes().read_passwd(&mut std::io::stdout()).unwrap();
388
389        // godammit rustc
390
391        assert_eq!(line, pass);
392
393        if let Some(l) = line {
394            assert_eq!(Some(l.as_str()), b);
395        } else {
396            assert!(b.is_none());
397        }
398    }
399
400    #[test]
401    fn test_read() {
402        let test1 = "this is the first test";
403        let test2 = "this is the second test";
404
405        line_match(test1, Some(test1));
406        line_match(test2, Some(test2));
407    }
408
409    #[test]
410    fn test_backspace() {
411        line_match(
412            "this is the\x7f first\x7f\x7f test",
413            Some("this is th fir test"),
414        );
415        line_match(
416            "this is the seco\x7fnd test\x7f",
417            Some("this is the secnd tes"),
418        );
419    }
420
421    #[test]
422    fn test_end() {
423        line_match(
424            "abc\nhttps://www.youtube.com/watch?v=dQw4w9WgXcQ",
425            Some("abc"),
426        );
427        line_match(
428            "hello\rhttps://www.youtube.com/watch?v=yPYZpwSpKmA",
429            Some("hello"),
430        );
431    }
432
433    #[test]
434    fn test_abort() {
435        line_match("abc\x03https://www.youtube.com/watch?v=dQw4w9WgXcQ", None);
436        line_match("hello\x04https://www.youtube.com/watch?v=yPYZpwSpKmA", None);
437    }
438}