diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-02-09 12:39:31 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-02-09 13:07:31 -0500 |
commit | becbb90b2f22d58c98693d653f55ba604bb03f75 (patch) | |
tree | 085b9ac9b197a9ad3f0dd20df36848c5619adefe /src/util | |
parent | 656582a6c867c25667661be9b327b4cc73859d7d (diff) |
rework input thread and file operations
- no longer depend on fs_extra for copy/paste files
- in house solution preserves permissions
- ioworkers are now queued, no more parallel io tasks
- input thread now listens for ioworker threads as well
- cargo fmt
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/event.rs | 102 | ||||
-rw-r--r-- | src/util/key_mapping.rs | 2 | ||||
-rw-r--r-- | src/util/mod.rs | 76 |
3 files changed, 69 insertions, 111 deletions
diff --git a/src/util/event.rs b/src/util/event.rs index c6559cf..2db5e5c 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -1,15 +1,21 @@ use std::io; use std::sync::mpsc; use std::thread; -use std::time::Duration; +use std::time::{Duration, SystemTime}; + +use chrono::offset::Local; +use chrono::DateTime; use termion::event::Key; use termion::input::TermRead; +use crate::io::IOWorkerThread; + #[derive(Debug)] -pub enum Event<I> { - Input(I), - Tick, +pub enum Event { + Input(Key), + IOWorkerProgress(u64), + IOWorkerResult, } #[derive(Debug, Clone, Copy)] @@ -28,56 +34,84 @@ impl Default for Config { /// A small event handler that wrap termion input and tick events. Each event /// type is handled in its own thread and returned to a common `Receiver` pub struct Events { - rx: mpsc::Receiver<Event<Key>>, + pub event_tx: mpsc::Sender<Event>, + event_rx: mpsc::Receiver<Event>, + pub sync_tx: mpsc::Sender<()>, + sync_rx: mpsc::Receiver<()>, input_handle: thread::JoinHandle<()>, -// tick_handle: thread::JoinHandle<()>, + // fileio_handle: thread::JoinHandle<()>, } impl Events { - pub fn new() -> Events { + pub fn new() -> Self { Events::with_config(Config::default()) } - pub fn with_config(config: Config) -> Events { - let (tx, rx) = mpsc::channel(); + pub fn with_config(_: Config) -> Self { + let (sync_tx, sync_rx) = mpsc::channel(); + let (event_tx, event_rx) = mpsc::channel(); + let input_handle = { - let tx = tx.clone(); + let sync_tx = sync_tx.clone(); + let event_tx = event_tx.clone(); thread::spawn(move || { let stdin = io::stdin(); - for evt in stdin.keys() { - match evt { - Ok(key) => { - if let Err(e) = tx.send(Event::Input(key)) { - eprintln!("Err: {:#?}", e); - return; + let mut keys = stdin.keys(); + loop { + if let Some(evt) = keys.next() { + match evt { + Ok(key) => { + if let Err(e) = event_tx.send(Event::Input(key)) { + eprintln!("Input thread send err: {:#?}", e); + return; + } + sync_tx.send(()); } + _ => {} } - Err(e) => {} } } }) }; -/* - let tick_handle = { - let tx = tx.clone(); - thread::spawn(move || { - let tx = tx.clone(); - loop { - tx.send(Event::Tick).unwrap(); - thread::sleep(config.tick_rate); - } - }) - }; -*/ + Events { - rx, + event_tx, + event_rx, + sync_tx, + sync_rx, input_handle, -// tick_handle, } } - pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> { - self.rx.recv() + pub fn next(&self) -> Result<Event, mpsc::RecvError> { + let now = SystemTime::now(); + let datetime: DateTime<Local> = now.into(); + #[cfg(debug_assertions)] + eprintln!( + "\nwaiting for event...{}", + datetime.format("%d/%m/%Y %T %6f") + ); + + let event = self.event_rx.recv(); + + #[cfg(debug_assertions)] + eprintln!("got event: {:?}", event); + + let now = SystemTime::now(); + let datetime: DateTime<Local> = now.into(); + + #[cfg(debug_assertions)] + eprintln!("Event captured at: {}", datetime.format("%d/%m/%Y %T %6f")); + + #[cfg(debug_assertions)] + eprintln!("waiting for recv..."); + + self.sync_rx.recv(); + let now = SystemTime::now(); + let datetime: DateTime<Local> = now.into(); + + #[cfg(debug_assertions)] + eprintln!("Done: {}", datetime.format("%d/%m/%Y %T %6f")); + event } } - diff --git a/src/util/key_mapping.rs b/src/util/key_mapping.rs index 9d7c182..fa69e92 100644 --- a/src/util/key_mapping.rs +++ b/src/util/key_mapping.rs @@ -34,7 +34,7 @@ pub fn str_to_key(s: &str) -> Option<Key> { _ => None, }; - if let Some(a) = key { + if let Some(_) = key { return key; } diff --git a/src/util/mod.rs b/src/util/mod.rs index 94ead10..32992c0 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,78 +1,2 @@ pub mod event; pub mod key_mapping; - -use rand::distributions::{Distribution, Uniform}; -use rand::rngs::ThreadRng; - -#[derive(Clone)] -pub struct RandomSignal { - distribution: Uniform<u64>, - rng: ThreadRng, -} - -impl RandomSignal { - pub fn new(lower: u64, upper: u64) -> RandomSignal { - RandomSignal { - distribution: Uniform::new(lower, upper), - rng: rand::thread_rng(), - } - } -} - -impl Iterator for RandomSignal { - type Item = u64; - fn next(&mut self) -> Option<u64> { - Some(self.distribution.sample(&mut self.rng)) - } -} - -#[derive(Clone)] -pub struct SinSignal { - x: f64, - interval: f64, - period: f64, - scale: f64, -} - -impl SinSignal { - pub fn new(interval: f64, period: f64, scale: f64) -> SinSignal { - SinSignal { - x: 0.0, - interval, - period, - scale, - } - } -} - -impl Iterator for SinSignal { - type Item = (f64, f64); - fn next(&mut self) -> Option<Self::Item> { - let point = (self.x, (self.x * 1.0 / self.period).sin() * self.scale); - self.x += self.interval; - Some(point) - } -} - -pub struct TabsState<'a> { - pub titles: Vec<&'a str>, - pub index: usize, -} - -impl<'a> TabsState<'a> { - pub fn new(titles: Vec<&'a str>) -> TabsState { - TabsState { titles, index: 0 } - } - pub fn next(&mut self) { - self.index = (self.index + 1) % self.titles.len(); - } - - pub fn previous(&mut self) { - if self.index > 0 { - self.index -= 1; - } else { - self.index = self.titles.len() - 1; - } - } -} - |