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/run.rs | |
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/run.rs')
-rw-r--r-- | src/run.rs | 152 |
1 files changed, 72 insertions, 80 deletions
@@ -1,10 +1,10 @@ use std::process; -use std::time; +use std::thread; use termion::event::Key; -use crate::commands::{CommandKeybind, FileOperationThread, JoshutoCommand, ReloadDirList}; -use crate::config::{self, JoshutoCommandMapping, JoshutoConfig}; +use crate::commands::{CommandKeybind, JoshutoCommand, ReloadDirList}; +use crate::config::{JoshutoCommandMapping, JoshutoConfig}; use crate::context::JoshutoContext; use crate::tab::JoshutoTab; use crate::ui; @@ -12,11 +12,12 @@ use crate::util::event::{Event, Events}; use crate::window::JoshutoPanel; use crate::window::JoshutoView; -fn recurse_get_keycommand(keymap: &JoshutoCommandMapping) -> Option<&JoshutoCommand> { +fn recurse_get_keycommand<'a>( + events: &Events, + keymap: &'a JoshutoCommandMapping, +) -> Option<&'a dyn JoshutoCommand> { let (term_rows, term_cols) = ui::getmaxyx(); - ncurses::timeout(-1); - let events = Events::new(); let event = { let keymap_len = keymap.len(); let win = JoshutoPanel::new( @@ -39,24 +40,20 @@ fn recurse_get_keycommand(keymap: &JoshutoCommandMapping) -> Option<&JoshutoComm }; ncurses::doupdate(); - match event { + let command = match event { Ok(Event::Input(input)) => match input { - Key::Esc => { - None - } - key @ Key::Char(_) => { - match keymap.get(&key) { - Some(CommandKeybind::CompositeKeybind(m)) => recurse_get_keycommand(&m), - Some(CommandKeybind::SimpleKeybind(s)) => Some(s.as_ref()), - _ => None, - } - } - _ => { - None - } - } + Key::Esc => None, + key @ Key::Char(_) => match keymap.get(&key) { + Some(CommandKeybind::CompositeKeybind(m)) => recurse_get_keycommand(events, &m), + Some(CommandKeybind::SimpleKeybind(s)) => Some(s.as_ref()), + _ => None, + }, + _ => None, + }, _ => None, - } + }; + ncurses::doupdate(); + command } fn reload_tab( @@ -72,52 +69,6 @@ fn reload_tab( Ok(()) } -fn join_thread( - context: &mut JoshutoContext, - thread: FileOperationThread<u64, fs_extra::TransitProcess>, - view: &JoshutoView, -) -> std::io::Result<()> { - ncurses::werase(view.bot_win.win); - ncurses::doupdate(); - - let (tab_src, tab_dest) = (thread.tab_src, thread.tab_dest); - match thread.handle.join() { - Err(e) => { - ui::wprint_err(&view.bot_win, format!("{:?}", e).as_str()); - view.bot_win.queue_for_refresh(); - } - Ok(_) => { - if tab_src < context.tabs.len() { - reload_tab(tab_src, context, view)?; - } - if tab_dest != tab_src && tab_dest < context.tabs.len() { - reload_tab(tab_dest, context, view)?; - } - } - } - Ok(()) -} - -fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) -> std::io::Result<()> { - let thread_wait_duration: time::Duration = time::Duration::from_millis(100); - for i in 0..context.threads.len() { - match &context.threads[i].recv_timeout(&thread_wait_duration) { - Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => { - let thread = context.threads.swap_remove(i); - join_thread(context, thread, view)?; - ncurses::doupdate(); - break; - } - Ok(progress_info) => { - ui::draw_fs_operation_progress(&view.bot_win, &progress_info); - ncurses::doupdate(); - } - _ => {} - } - } - Ok(()) -} - #[inline] fn resize_handler(context: &mut JoshutoContext, view: &JoshutoView) { ui::redraw_tab_view(&view.tab_win, &context); @@ -159,25 +110,54 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) { let mut context = JoshutoContext::new(config_t); let mut view = JoshutoView::new(context.config_t.column_ratio); init_context(&mut context, &view); + ncurses::doupdate(); - let events = Events::new(); + let mut io_handle = None; while !context.exit { - let event = events.next(); + /* checking if there are workers that need to be run */ + match io_handle.as_ref() { + None => { + if !context.worker_queue.is_empty() { + let worker = context.worker_queue.pop_front().unwrap(); + io_handle = { + let event_tx = context.events.event_tx.clone(); + let sync_tx = context.events.sync_tx.clone(); + let thread = thread::spawn(move || { + worker.start(); + while let Ok(evt) = worker.recv() { + event_tx.send(evt); + sync_tx.send(()); + } + worker.handle.join(); + event_tx.send(Event::IOWorkerResult); + sync_tx.send(()); + }); + Some(thread) + }; + } + } + _ => {} + } + + let event = context.events.next(); if let Ok(event) = event { match event { Event::Input(key) => { let keycommand = match keymap_t.get(&key) { - Some(CommandKeybind::CompositeKeybind(m)) => match recurse_get_keycommand(&m) { - Some(s) => s, - None => { - ui::wprint_err(&view.bot_win, &format!("Unknown keycode: {:?}", key)); - ncurses::doupdate(); - continue; + Some(CommandKeybind::CompositeKeybind(m)) => { + match recurse_get_keycommand(&context.events, &m) { + Some(s) => s, + None => { + ui::wprint_err( + &view.bot_win, + &format!("Unknown keycode: {:?}", key), + ); + ncurses::doupdate(); + continue; + } } - }, - Some(CommandKeybind::SimpleKeybind(s)) => { - s.as_ref() } + Some(CommandKeybind::SimpleKeybind(s)) => s.as_ref(), None => { ui::wprint_err(&view.bot_win, &format!("Unknown keycode: {:?}", key)); ncurses::doupdate(); @@ -192,7 +172,19 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) { } ncurses::doupdate(); } - event => ui::wprint_err(&view.bot_win, &format!("Unknown keycode: {:?}", event)), + Event::IOWorkerProgress(p) => { + ui::wprint_err(&view.bot_win, &format!("bytes copied {}", p)) + } + Event::IOWorkerResult => { + match io_handle { + Some(handle) => { + handle.join(); + ui::wprint_err(&view.bot_win, "io_worker done"); + } + None => {} + } + io_handle = None; + } } ncurses::doupdate(); } |