termscp/src/ui/input.rs

106 lines
3.1 KiB
Rust
Raw Normal View History

2020-11-21 11:42:24 +01:00
//! ## Input
//!
//! `input` is the module which provides all the functionalities related to input events in the user interface
2021-03-26 22:25:10 +01:00
/**
* MIT License
*
* termscp - Copyright (c) 2021 Christian Visintin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2020-11-21 11:42:24 +01:00
extern crate crossterm;
2020-11-21 12:12:10 +01:00
use crossterm::event::{poll, read, Event};
2020-11-21 11:42:24 +01:00
use std::time::Duration;
/// ## InputHandler
///
/// InputHandler is the struct which runs a thread which waits for
/// input events from the user and reports them through a receiver
2021-03-04 15:49:13 +01:00
pub(crate) struct InputHandler;
2020-11-21 11:42:24 +01:00
impl InputHandler {
/// ### InputHandler
///
///
pub(crate) fn new() -> InputHandler {
2020-11-21 12:12:10 +01:00
InputHandler {}
2020-11-21 11:42:24 +01:00
}
2020-11-21 21:26:27 +01:00
/// ### fetch_events
2020-11-21 11:42:24 +01:00
///
/// Check if new events have been received from handler
2020-12-15 14:18:16 +01:00
#[allow(dead_code)]
2020-11-21 21:26:27 +01:00
pub(crate) fn fetch_events(&self) -> Result<Vec<Event>, ()> {
2020-11-21 11:42:24 +01:00
let mut inbox: Vec<Event> = Vec::new();
loop {
2020-11-25 10:26:37 +01:00
match self.read_event() {
Ok(ev_opt) => match ev_opt {
Some(ev) => inbox.push(ev),
2021-03-26 20:38:47 +01:00
None => break,
2020-11-25 10:26:37 +01:00
},
2021-03-26 20:38:47 +01:00
Err(_) => return Err(()),
2020-11-25 10:26:37 +01:00
}
}
Ok(inbox)
}
/// ### read_event
2021-03-26 20:38:47 +01:00
///
2020-11-25 10:26:37 +01:00
/// Read event from input listener
pub(crate) fn read_event(&self) -> Result<Option<Event>, ()> {
if let Ok(available) = poll(Duration::from_millis(10)) {
match available {
true => {
// Read event
if let Ok(ev) = read() {
Ok(Some(ev))
} else {
Err(())
2020-11-21 12:12:10 +01:00
}
}
2021-03-26 20:38:47 +01:00
false => Ok(None),
2020-11-21 11:42:24 +01:00
}
2020-11-25 10:26:37 +01:00
} else {
Err(())
2020-11-21 11:42:24 +01:00
}
}
2020-11-21 12:12:10 +01:00
}
2020-11-21 11:42:24 +01:00
2020-11-21 12:12:10 +01:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ui_input_new() {
2020-11-24 15:13:36 +01:00
let _: InputHandler = InputHandler::new();
2020-11-21 12:12:10 +01:00
}
/* ERRORS ON GITHUB ACTIONS
2020-11-21 12:12:10 +01:00
#[test]
fn test_ui_input_fetch() {
let input_hnd: InputHandler = InputHandler::new();
// Try recv
assert_eq!(input_hnd.fetch_messages().ok().unwrap().len(), 0);
}*/
2020-11-21 11:42:24 +01:00
}