mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Fixed input handler
This commit is contained in:
108
src/ui/input.rs
108
src/ui/input.rs
@@ -25,60 +25,21 @@
|
|||||||
|
|
||||||
extern crate crossterm;
|
extern crate crossterm;
|
||||||
|
|
||||||
// Deps
|
use crossterm::event::{poll, read, Event};
|
||||||
use crossterm::event::{read, Event};
|
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
|
||||||
use std::thread;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// ## InputHandler
|
/// ## InputHandler
|
||||||
///
|
///
|
||||||
/// InputHandler is the struct which runs a thread which waits for
|
/// InputHandler is the struct which runs a thread which waits for
|
||||||
/// input events from the user and reports them through a receiver
|
/// input events from the user and reports them through a receiver
|
||||||
pub(crate) struct InputHandler {
|
pub(crate) struct InputHandler {}
|
||||||
running: Arc<Mutex<bool>>,
|
|
||||||
join_hnd: Option<thread::JoinHandle<()>>,
|
|
||||||
receiver: mpsc::Receiver<Event>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InputHandler {
|
impl InputHandler {
|
||||||
/// ### InputHandler
|
/// ### InputHandler
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
pub(crate) fn new() -> InputHandler {
|
pub(crate) fn new() -> InputHandler {
|
||||||
let (thread_sender, client_receiver) = mpsc::channel();
|
InputHandler {}
|
||||||
let client_running = Arc::new(Mutex::new(false));
|
|
||||||
let thread_running = Arc::clone(&client_running);
|
|
||||||
let join_hnd = thread::spawn(move || {
|
|
||||||
InputHandler::thread_run(thread_sender, thread_running);
|
|
||||||
});
|
|
||||||
InputHandler {
|
|
||||||
running: client_running,
|
|
||||||
join_hnd: Some(join_hnd),
|
|
||||||
receiver: client_receiver,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ### stop
|
|
||||||
///
|
|
||||||
/// Stop InputHandler
|
|
||||||
pub(crate) fn stop(&mut self) {
|
|
||||||
if self.join_hnd.is_some() {
|
|
||||||
//Set join to true
|
|
||||||
{
|
|
||||||
// Set running to false
|
|
||||||
{
|
|
||||||
let mut running = self.running.lock().unwrap();
|
|
||||||
*running = false;
|
|
||||||
}
|
|
||||||
// Join
|
|
||||||
self.join_hnd
|
|
||||||
.take()
|
|
||||||
.map(thread::JoinHandle::join)
|
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ### fetch_messages
|
/// ### fetch_messages
|
||||||
@@ -87,43 +48,40 @@ impl InputHandler {
|
|||||||
pub(crate) fn fetch_messages(&self) -> Result<Vec<Event>, ()> {
|
pub(crate) fn fetch_messages(&self) -> Result<Vec<Event>, ()> {
|
||||||
let mut inbox: Vec<Event> = Vec::new();
|
let mut inbox: Vec<Event> = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
match self.receiver.try_recv() {
|
if let Ok(available) = poll(Duration::from_millis(10)) {
|
||||||
Ok(message) => inbox.push(message),
|
match available {
|
||||||
Err(err) => match err {
|
true => {
|
||||||
mpsc::TryRecvError::Empty => break,
|
// Read event
|
||||||
_ => return Err(()),
|
if let Ok(ev) = read() {
|
||||||
},
|
inbox.push(ev);
|
||||||
|
} else {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false => break,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(inbox)
|
Ok(inbox)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ### run
|
#[cfg(test)]
|
||||||
///
|
mod tests {
|
||||||
/// Run method for thread
|
|
||||||
fn thread_run(sender: mpsc::Sender<Event>, running: Arc<Mutex<bool>>) {
|
use super::*;
|
||||||
{
|
|
||||||
let mut running = running.lock().unwrap();
|
#[test]
|
||||||
*running = true;
|
fn test_ui_input_new() {
|
||||||
}
|
let input_hnd: InputHandler = InputHandler::new();
|
||||||
loop {
|
}
|
||||||
// Check if running is false
|
|
||||||
{
|
#[test]
|
||||||
let running = running.lock().unwrap();
|
fn test_ui_input_fetch() {
|
||||||
if *running == false {
|
let input_hnd: InputHandler = InputHandler::new();
|
||||||
break;
|
// Try recv
|
||||||
}
|
assert_eq!(input_hnd.fetch_messages().ok().unwrap().len(), 0);
|
||||||
}
|
|
||||||
// Fetch events
|
|
||||||
if let Ok(ev) = read() {
|
|
||||||
// Send event
|
|
||||||
if let Err(_) = sender.send(ev) {
|
|
||||||
// The counterpart has died
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Sleep
|
|
||||||
thread::sleep(Duration::from_millis(50));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,4 +23,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Modules
|
||||||
|
pub mod context;
|
||||||
pub(crate) mod input;
|
pub(crate) mod input;
|
||||||
|
|
||||||
|
// Context
|
||||||
Reference in New Issue
Block a user