Skip to content

Commit

Permalink
Send key input events only
Browse files Browse the repository at this point in the history
  • Loading branch information
ryym committed Aug 8, 2018
1 parent e3afa81 commit 77f0f30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use errors::*;
use std::io;
use std::sync::mpsc::{channel, Receiver, RecvError, TryRecvError};
use std::thread;
use termion::event::Event;
use termion::event::{Event, Key};
use termion::input::Events;

pub type EventResult = io::Result<Event>;
Expand All @@ -26,7 +26,17 @@ impl Inputs {
Inputs { receiver }
}

pub fn try_recv(&mut self) -> Result<Option<EventResult>> {
pub fn try_recv_key(&mut self) -> Result<Option<io::Result<Key>>> {
self.try_recv_event().map(|option| {
option.and_then(|result| match result {
Ok(Event::Key(key)) => Some(Ok(key)),
Ok(_) => None,
Err(err) => Some(Err(err)),
})
})
}

pub fn try_recv_event(&mut self) -> Result<Option<EventResult>> {
match self.receiver.try_recv() {
Ok(event) => Ok(Some(event)),
Err(TryRecvError::Empty) => Ok(None),
Expand Down
6 changes: 3 additions & 3 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::time::Duration;
use termion as tm;
use termion::color;
use termion::cursor::Goto;
use termion::event::{Event, Key};
use termion::event::Key;

pub struct Modal<'a> {
pub title: &'a str,
Expand Down Expand Up @@ -38,8 +38,8 @@ impl<W: Write> Screen<W> {
}

pub fn next_input(&mut self) -> Result<Option<io::Result<u8>>> {
let input = match self.inputs.try_recv()? {
Some(Ok(Event::Key(Key::Char(c)))) => Some(Ok(c as u8)),
let input = match self.inputs.try_recv_key()? {
Some(Ok(Key::Char(c))) => Some(Ok(c as u8)),
_ => None,
};
Ok(input)
Expand Down

0 comments on commit 77f0f30

Please sign in to comment.