sl_console/
input.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! User input

use std::io::{self, Read, Write};
use std::ops;
use std::time::Duration;

use crate::console::{ConsoleRead, ConsoleWrite};
use crate::event::{self, Event, Key, KeyCode};

/// An iterator over input events.
pub struct EventsAndRaw<R> {
    inner: R,
}

impl<R: ConsoleRead> Iterator for EventsAndRaw<R> {
    type Item = Result<(Event, Vec<u8>), io::Error>;

    fn next(&mut self) -> Option<Result<(Event, Vec<u8>), io::Error>> {
        self.inner.get_event_and_raw(None)
    }
}

/// An iterator over input keys.
pub struct Keys<R> {
    inner: R,
}

impl<R: ConsoleRead> Iterator for Keys<R> {
    type Item = Result<Key, io::Error>;

    fn next(&mut self) -> Option<Result<Key, io::Error>> {
        self.inner.get_key()
    }
}

/// An iterator over input events.
pub struct Events<R> {
    inner: R,
}

impl<R: ConsoleRead> Iterator for Events<R> {
    type Item = Result<Event, io::Error>;

    fn next(&mut self) -> Option<Result<Event, io::Error>> {
        self.inner.get_event()
    }
}

/// Get the next input event and the bytes that define it.
pub(crate) fn event_and_raw(
    source: &mut dyn Read,
    leftover: &mut Option<u8>,
) -> Option<Result<(Event, Vec<u8>), io::Error>> {
    if let Some(c) = leftover {
        // we have a leftover byte, use it
        let ch = *c;
        *leftover = None;
        return Some(parse_event(ch, &mut source.bytes()));
    }

    // 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::new(KeyCode::Esc)), vec![b'\x1B'])),
            c => parse_event(c, &mut source.bytes()),
        },
        Ok(2) => {
            let option_iter = &mut Some(buf[1]).into_iter();
            let result = {
                let mut iter = option_iter.map(Ok).chain(source.bytes());
                parse_event(buf[0], &mut iter)
            };
            // If the option_iter wasn't consumed, keep the byte for later.
            *leftover = option_iter.next();
            result
        }
        Ok(_) => unreachable!(),
        Err(e) => Err(e),
    };

    Some(res)
}

fn parse_event<I>(item: u8, iter: &mut I) -> io::Result<(Event, Vec<u8>)>
where
    I: Iterator<Item = io::Result<u8>>,
{
    let mut buf = vec![item];
    let mut iter = iter.inspect(|byte| {
        if let Ok(byte) = *byte {
            buf.push(byte);
        }
    });
    event::parse_event(item, &mut iter)
        .or_else(|_| Ok(Event::Unsupported(buf.clone())))
        .map(|e| (e, buf))
}

/// Extension to `ConsoleRead` trait.
pub trait ConsoleReadExt {
    /// An iterator over input events and the raw bytes that make them.
    fn events_and_raw(self) -> EventsAndRaw<Self>
    where
        Self: Sized;

    /// An iterator over input events.
    fn events(self) -> Events<Self>
    where
        Self: Sized;

    /// An iterator over key inputs.
    fn keys(self) -> Keys<Self>
    where
        Self: Sized;

    /// Get the next input event from the console.
    /// This version will block until an event is ready.
    /// Returns None if the Console has no more data vs a read that would block.
    fn get_event(&mut self) -> Option<io::Result<Event>>;

    /// Get the next input event from the console.
    ///
    /// If no data is ready before timeout then will return a WouldBlock error.
    /// Returns None if the Console has no more data vs a read that would block.
    fn get_event_timeout(&mut self, timeout: Duration) -> Option<io::Result<Event>>;

    /// Get the next key event from the console.
    ///
    /// This will skip over non-key events (they will be lost).
    /// This version will block until an event is ready.
    /// Returns None if the Console has no more data.
    fn get_key(&mut self) -> Option<io::Result<Key>>;
}

impl<R: ConsoleRead> ConsoleReadExt for R {
    fn events_and_raw(self) -> EventsAndRaw<Self> {
        EventsAndRaw { inner: self }
    }

    fn events(self) -> Events<Self> {
        Events { inner: self }
    }

    fn keys(self) -> Keys<Self> {
        Keys { inner: self }
    }

    fn get_event(&mut self) -> Option<io::Result<Event>> {
        match self.get_event_and_raw(None) {
            Some(Ok((event, _raw))) => Some(Ok(event)),
            Some(Err(err)) => Some(Err(err)),
            None => None,
        }
    }

    fn get_event_timeout(&mut self, timeout: Duration) -> Option<io::Result<Event>> {
        match self.get_event_and_raw(Some(timeout)) {
            Some(Ok((event, _raw))) => Some(Ok(event)),
            Some(Err(err)) => Some(Err(err)),
            None => None,
        }
    }

    fn get_key(&mut self) -> Option<io::Result<Key>> {
        loop {
            match self.get_event() {
                Some(Ok(Event::Key(k))) => return Some(Ok(k)),
                Some(Ok(_)) => continue,
                Some(Err(e)) => return Some(Err(e)),
                None => return None,
            }
        }
    }
}

/// A sequence of escape codes to enable terminal mouse support.
const ENTER_MOUSE_SEQUENCE: &str = csi!("?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h");

/// A sequence of escape codes to disable terminal mouse support.
const EXIT_MOUSE_SEQUENCE: &str = csi!("?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l");

/// Extension trait for ConsoleWrite to turn mouse support on or off for the console.
pub trait ConsoleMouseExt {
    /// Turn mouse support on for the console.
    fn mouse_on(&mut self) -> io::Result<()>;

    /// Turn mouse support off for the console.
    fn mouse_off(&mut self) -> io::Result<()>;
}

impl<W: ConsoleWrite> ConsoleMouseExt for W {
    fn mouse_on(&mut self) -> io::Result<()> {
        self.write_all(ENTER_MOUSE_SEQUENCE.as_bytes())?;
        Ok(())
    }

    fn mouse_off(&mut self) -> io::Result<()> {
        self.write_all(EXIT_MOUSE_SEQUENCE.as_bytes())?;
        Ok(())
    }
}

/// A terminal with added mouse support.
///
/// This can be obtained through the `From` implementations.
/// You can use this if you want an RAII guard around terminal mouse support.
pub struct MouseTerminal<W: ConsoleWrite> {
    term: W,
}

impl<W: ConsoleWrite> From<W> for MouseTerminal<W> {
    fn from(mut from: W) -> MouseTerminal<W> {
        from.write_all(ENTER_MOUSE_SEQUENCE.as_bytes()).unwrap();

        MouseTerminal { term: from }
    }
}

impl<W: ConsoleWrite> Drop for MouseTerminal<W> {
    fn drop(&mut self) {
        self.term.write_all(EXIT_MOUSE_SEQUENCE.as_bytes()).unwrap();
    }
}

impl<W: ConsoleWrite> ops::Deref for MouseTerminal<W> {
    type Target = W;

    fn deref(&self) -> &W {
        &self.term
    }
}

impl<W: ConsoleWrite> ops::DerefMut for MouseTerminal<W> {
    fn deref_mut(&mut self) -> &mut W {
        &mut self.term
    }
}

impl<W: ConsoleWrite> 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()
    }
}

impl<W: ConsoleWrite> ConsoleWrite for MouseTerminal<W> {
    fn set_raw_mode(&mut self, mode: bool) -> io::Result<bool> {
        self.term.set_raw_mode(mode)
    }

    fn is_raw_mode(&self) -> bool {
        self.term.is_raw_mode()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use event::{Event, Key, KeyCode, KeyMod, MouseButton, MouseEvent};
    use std::cell::RefCell;

    thread_local!(static LEFTOVER: RefCell<Option<u8>> = RefCell::new(None));

    impl ConsoleRead for &[u8] {
        fn get_event_and_raw(
            &mut self,
            _timeout: Option<Duration>,
        ) -> Option<io::Result<(Event, Vec<u8>)>> {
            LEFTOVER.with(|leftover| event_and_raw(self, &mut leftover.borrow_mut()))
        }

        fn poll(&mut self, _timeout: Option<Duration>) -> bool {
            self.len() > 0
        }

        fn read_timeout(
            &mut self,
            buf: &mut [u8],
            _timeout: Option<Duration>,
        ) -> io::Result<usize> {
            self.read(buf)
        }
    }

    #[test]
    fn test_keys() {
        let mut i = b"\x1Bayo\x7F\x1B[D".keys();

        assert_eq!(
            i.next().unwrap().unwrap(),
            Key::new_mod(KeyCode::Char('a'), KeyMod::Alt)
        );
        assert_eq!(i.next().unwrap().unwrap(), Key::new(KeyCode::Char('y')));
        assert_eq!(i.next().unwrap().unwrap(), Key::new(KeyCode::Char('o')));
        assert_eq!(i.next().unwrap().unwrap(), Key::new(KeyCode::Backspace));
        assert_eq!(i.next().unwrap().unwrap(), Key::new(KeyCode::Left));
        assert!(i.next().is_none());
    }

    #[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(vec![0x1B, b'[', 0x00])
        );
        assert_eq!(
            i.next().unwrap().unwrap(),
            Event::Key(Key::new(KeyCode::Char('b')))
        );
        assert_eq!(
            i.next().unwrap().unwrap(),
            Event::Key(Key::new(KeyCode::Char('c')))
        );
        assert_eq!(
            i.next().unwrap().unwrap(),
            Event::Key(Key::new(KeyCode::Backspace))
        );
        assert_eq!(
            i.next().unwrap().unwrap(),
            Event::Key(Key::new(KeyCode::Left))
        );
        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))
        );
        assert_eq!(
            i.next().unwrap().unwrap(),
            Event::Key(Key::new(KeyCode::Char('b')))
        );
        assert!(i.next().is_none());
    }

    #[test]
    fn test_events_and_raw() {
        let input = 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";
        let mut output = Vec::<u8>::new();
        {
            let mut i = input
                .events_and_raw()
                .map(|res| res.unwrap())
                .inspect(|&(_, ref raw)| {
                    output.extend(raw);
                })
                .map(|(event, _)| event);

            assert_eq!(
                i.next().unwrap(),
                Event::Unsupported(vec![0x1B, b'[', 0x00])
            );
            assert_eq!(i.next().unwrap(), Event::Key(Key::new(KeyCode::Char('b'))));
            assert_eq!(i.next().unwrap(), Event::Key(Key::new(KeyCode::Char('c'))));
            assert_eq!(i.next().unwrap(), Event::Key(Key::new(KeyCode::Backspace)));
            assert_eq!(i.next().unwrap(), Event::Key(Key::new(KeyCode::Left)));
            assert_eq!(
                i.next().unwrap(),
                Event::Mouse(MouseEvent::Press(MouseButton::WheelUp, 2, 4))
            );
            assert_eq!(
                i.next().unwrap(),
                Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))
            );
            assert_eq!(
                i.next().unwrap(),
                Event::Mouse(MouseEvent::Press(MouseButton::Left, 2, 4))
            );
            assert_eq!(i.next().unwrap(), Event::Mouse(MouseEvent::Release(2, 4)));
            assert_eq!(i.next().unwrap(), Event::Mouse(MouseEvent::Release(2, 4)));
            assert_eq!(i.next().unwrap(), Event::Key(Key::new(KeyCode::Char('b'))));
            assert!(i.next().is_none());
        }

        assert_eq!(input.iter().map(|b| *b).collect::<Vec<u8>>(), output)
    }

    #[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::new(KeyCode::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::new(KeyCode::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::new(KeyCode::Insert));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::Home));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::Home));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::PageUp));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::Delete));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::End));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::End));
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::PageDown));
        assert!(st.next().is_none());
    }

    #[test]
    fn test_esc_key() {
        let mut st = b"\x1B".keys();
        assert_eq!(st.next().unwrap().unwrap(), Key::new(KeyCode::Esc));
        assert!(st.next().is_none());
    }
}