Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and add tests for parsing event #3

Merged
merged 2 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use num::FromPrimitive;

use terminal::Terminal;
Expand All @@ -16,29 +14,29 @@ pub enum Event {
Resize { width: usize, height: usize },
}

/// Parse event from buffer. Returns `Err` if any IO error occurs.
/// `Ok(None)` means 'no error occurs, but buffered bytes is not enough'.
pub fn parse(buf: &[u8], term: &Terminal) -> io::Result<Option<(usize, Event)>> {
/// Parse event from buffer.
/// `None` means 'buffered bytes are not enough'.
pub fn parse(buf: &[u8], term: &Terminal) -> Option<(usize, Event)> {
if buf.is_empty() {
return Ok(None);
return None;
}
if buf[0] == b'\x1b' {
// escape sequence
if let Some(result) = parse_escape_sequence(buf, term)? {
return Ok(Some(result));
if let Some(result) = parse_escape_sequence(buf, term) {
return Some(result);
}
}

if let Some(key) = key_from_byte(buf[0]) {
// for single-byte keys like ctrl-A
return Ok(Some((1, Event::Key(key))));
return Some((1, Event::Key(key)));
}

let (len_utf8, ch) = match decode_char(buf) {
None => return Ok(None),
None => return None,
Some(r) => r,
};
Ok(Some((len_utf8, Event::Char(ch))))
Some((len_utf8, Event::Char(ch)))
}

// Copy from core/str/mod.rs in Rust.
Expand Down Expand Up @@ -89,18 +87,17 @@ fn decode_char(buf: &[u8]) -> Option<(usize, char)> {
}
}

fn parse_escape_sequence(buf: &[u8], term: &Terminal) -> io::Result<Option<(usize, Event)>> {
fn parse_escape_sequence(buf: &[u8], term: &Terminal) -> Option<(usize, Event)> {
debug_assert!(buf[0] == b'\x1b');

for &key in ESCAPE_KEYS.iter() {
if let Some(keybytes) = term.escaped_key_bytes(key) {
if buf.starts_with(keybytes) {
return Ok(Some((keybytes.len(), Event::Key(key))));
return Some((keybytes.len(), Event::Key(key)));
}
}
}

Ok(None)
None
}

fn key_from_byte(byte: u8) -> Option<Key> {
Expand All @@ -117,3 +114,18 @@ static ESCAPE_KEYS: [Key; 4] = [
Key::ArrowLeft,
Key::ArrowRight,
];

#[test]
fn test_decode_char() {
let tests = [
("abc".as_bytes(), Some('a')),
("あいう".as_bytes(), Some('あ')),
(&[227, 129, 130], Some('あ')),
(&[227, 129], None),
(&[], None),
];
for &(bytes, expected) in tests.iter() {
let expected = expected.map(|ch| (ch.len_utf8(), ch));
assert_eq!(decode_char(bytes), expected);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ fn spawn_ttyin_reader(tx: mpsc::Sender<Event>, term: Arc<Terminal>) -> io::Resul
};
let mut from = 0;
loop {
if let Some((read_byte, ev)) = event::parse(&buf[from..], &*term).unwrap() {
if let Some((read_byte, ev)) = event::parse(&buf[from..], &*term) {
from += read_byte;
if tx.send(ev).is_err() {
break;
Expand Down