mirror of
https://gitlab.com/fabinfra/fabaccess/sute.git
synced 2025-03-12 06:31:53 +01:00
Starts building a line editor
This commit is contained in:
parent
d0440942b1
commit
d074a551ad
56
src/input.rs
56
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<String>,
|
||||
rx: mpsc::Receiver<Key>,
|
||||
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<String> {
|
||||
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<Option<Self::Item>> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user