diff --git a/src/input.rs b/src/input.rs index a7f7d84..04c9459 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,34 +1,58 @@ -use std::io; -use std::thread; +use std::{mem, io, thread}; use std::pin::Pin; use std::task::{Context, Poll}; -use linefeed::{Interface, ReadResult}; -use linefeed::memory::MemoryTerminal; +use termion::event::Key; +use termion::input::TermRead; use futures::Stream; +use futures::ready; use futures::channel::mpsc; use futures::SinkExt; pub struct Inputs { - rx: mpsc::Receiver, + rx: mpsc::Receiver, hndl: thread::JoinHandle<()>, + cmd_line: String, } impl Inputs { - pub fn new(term: MemoryTerminal) -> Self { + pub fn new() -> Self { let (mut tx, rx) = mpsc::channel(64); let hndl = thread::spawn(move || { let stdin = io::stdin(); - let mut reader = Interface::with_term("sute", term).unwrap(); - reader.set_prompt("> ").unwrap(); - while let ReadResult::Input(line) = reader.read_line().unwrap() { - smol::block_on(tx.send(line)); + let mut keys = stdin.keys(); + for key in keys { + let key = key.unwrap(); + smol::block_on(tx.send(key)); } }); - Self { rx, hndl } + Self { + rx: rx, + hndl: hndl, + cmd_line: String::new(), + } + } + + pub fn get_line(&self) -> &str { + self.cmd_line.as_ref() + } + + pub fn handle_key(&mut self, key: Key) -> Option { + match key { + Key::Char('\n') => Some(mem::replace(&mut self.cmd_line, String::new())), + Key::Char(c) => { + self.cmd_line.push(c); + None + }, + Key::Backspace => { + self.cmd_line.pop(); + None + }, + _ => None + } } } @@ -36,6 +60,14 @@ impl Stream for Inputs { type Item = String; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { - Pin::new(&mut self.rx).poll_next(cx) + if let Some(key) = ready!(Pin::new(&mut self.rx).poll_next(cx)) { + if let Some(line) = self.handle_key(key) { + Poll::Ready(Some(line)) + } else { + Poll::Pending + } + } else { + Poll::Pending + } } }