diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-04 15:00:56 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-09-04 15:00:56 -0400 |
commit | 7741d4d2c8798ad0bb609e97fb3bda86c5318a36 (patch) | |
tree | 0176023bbe8dfff48198ba960f64a08ee785569f /src | |
parent | e6e35948545bfa14d672cafae92c2c8c153ba708 (diff) |
io operations now give more information
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/file_ops/copy.rs | 2 | ||||
-rw-r--r-- | src/commands/open_file.rs | 9 | ||||
-rw-r--r-- | src/commands/selection.rs | 11 | ||||
-rw-r--r-- | src/context/context.rs | 14 | ||||
-rw-r--r-- | src/io/io_observer.rs | 13 | ||||
-rw-r--r-- | src/io/io_worker.rs | 90 | ||||
-rw-r--r-- | src/run.rs | 4 | ||||
-rw-r--r-- | src/ui/widgets/tui_folder_view.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_menu.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_prompt.rs | 3 | ||||
-rw-r--r-- | src/ui/widgets/tui_textfield.rs | 4 | ||||
-rw-r--r-- | src/ui/widgets/tui_view.rs | 8 | ||||
-rw-r--r-- | src/util/event.rs | 2 | ||||
-rw-r--r-- | src/util/sort.rs | 2 | ||||
-rw-r--r-- | src/util/worker.rs | 19 |
15 files changed, 85 insertions, 100 deletions
diff --git a/src/commands/file_ops/copy.rs b/src/commands/file_ops/copy.rs index 138aac3..c741347 100644 --- a/src/commands/file_ops/copy.rs +++ b/src/commands/file_ops/copy.rs @@ -1,5 +1,3 @@ -use std::path; - use crate::commands::{JoshutoCommand, JoshutoRunnable}; use crate::context::{JoshutoContext, LocalStateContext}; use crate::error::JoshutoResult; diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index ce66aa5..7299b74 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -4,8 +4,7 @@ use crate::commands::{ChangeDirectory, JoshutoCommand, JoshutoRunnable}; use crate::config::mimetype::JoshutoMimetypeEntry; use crate::context::JoshutoContext; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; -use crate::fs::JoshutoDirEntry; -use crate::ui::widgets::{TuiMenu, TuiTextField}; +use crate::ui::widgets::TuiTextField; use crate::ui::TuiBackend; use crate::util::load_child::LoadChild; @@ -92,13 +91,13 @@ impl JoshutoRunnable for OpenFile { if !options.is_empty() { if options[0].get_fork() { - options[0].execute_with(files.as_slice()) + options[0].execute_with(files.as_slice())?; } else { backend.terminal_drop(); let res = options[0].execute_with(files.as_slice()); backend.terminal_restore()?; - res - }; + res?; + } } else { OpenFileWith::open_with(context, backend, options, files)?; } diff --git a/src/commands/selection.rs b/src/commands/selection.rs index aced34f..e2c80f9 100644 --- a/src/commands/selection.rs +++ b/src/commands/selection.rs @@ -63,12 +63,11 @@ impl JoshutoRunnable for SelectFiles { s.set_selected(!s.is_selected()); CursorMoveDown::new(1).execute(context, backend)?; } - } else { - if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() { - for curr in &mut curr_list.contents { - curr.set_selected(true); - } - } + } else if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() { + curr_list + .contents + .iter_mut() + .for_each(|c| c.set_selected(true)); } Ok(()) } diff --git a/src/context/context.rs b/src/context/context.rs index b29af77..2cf2661 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -4,7 +4,7 @@ use std::thread; use crate::config; use crate::context::{LocalStateContext, TabContext}; -use crate::io::{IOWorkerObserver, IOWorkerProgress, IOWorkerThread}; +use crate::io::{IOWorkerObserver, IOWorkerThread}; use crate::util::event::{Event, Events}; pub struct JoshutoContext { @@ -89,8 +89,9 @@ impl JoshutoContext { s.set_msg(msg); } } - pub fn worker_msg(&self) -> Option<&String> { - self.worker.as_ref().and_then(|s| s.get_msg()) + pub fn worker_msg(&self) -> Option<&str> { + let worker = self.worker.as_ref()?; + Some(worker.get_msg()) } pub fn start_next_job(&mut self) { @@ -99,16 +100,9 @@ impl JoshutoContext { if let Some(worker) = self.worker_queue.pop_front() { let src = worker.paths[0].clone(); let dest = worker.dest.clone(); - let file_op = worker.options.kind; let handle = thread::spawn(move || { let (wtx, wrx) = mpsc::channel(); // start worker - tx.send(Event::IOWorkerProgress(IOWorkerProgress { - kind: worker.options.kind, - index: 0, - len: worker.paths.len(), - processed: 0, - })); let worker_handle = thread::spawn(move || worker.start(wtx)); // relay worker info to event loop while let Ok(progress) = wrx.recv() { diff --git a/src/io/io_observer.rs b/src/io/io_observer.rs index c31b94d..c882911 100644 --- a/src/io/io_observer.rs +++ b/src/io/io_observer.rs @@ -4,7 +4,7 @@ use std::thread; #[derive(Debug)] pub struct IOWorkerObserver { pub handle: thread::JoinHandle<()>, - msg: Option<String>, + msg: String, src: path::PathBuf, dest: path::PathBuf, } @@ -15,7 +15,7 @@ impl IOWorkerObserver { handle, src, dest, - msg: None, + msg: String::new(), } } @@ -23,13 +23,10 @@ impl IOWorkerObserver { self.handle.join(); } pub fn set_msg(&mut self, msg: String) { - self.msg = Some(msg) + self.msg = msg } - pub fn get_msg(&self) -> Option<&String> { - self.msg.as_ref() - } - pub fn clear_msg(&mut self) { - self.msg = None + pub fn get_msg(&self) -> &str { + self.msg.as_str() } pub fn get_src_path(&self) -> &path::Path { self.src.as_path() diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs index ee599a9..d0e609f 100644 --- a/src/io/io_worker.rs +++ b/src/io/io_worker.rs @@ -1,7 +1,6 @@ use std::fs; use std::path; use std::sync::mpsc; -use std::thread; use super::rename_filename_conflict; @@ -60,27 +59,26 @@ impl IOWorkerThread { } fn paste_copy(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> { - let mut total = 0; - let len = self.paths.len(); + let mut progress = IOWorkerProgress { + kind: FileOp::Copy, + index: 0, + processed: 0, + len: self.paths.len(), + }; for (i, path) in self.paths.iter().enumerate() { - total += self.recursive_copy(path.as_path(), self.dest.as_path())?; - let progress = IOWorkerProgress { - kind: FileOp::Copy, - index: i, - processed: total, - len, - }; - tx.send(progress); + progress.index = i; + tx.send(progress.clone()); + self.recursive_copy(path.as_path(), self.dest.as_path(), tx.clone(), &mut progress)?; } Ok(IOWorkerProgress { kind: FileOp::Copy, - index: len, - processed: total, - len, + index: self.paths.len(), + processed: progress.processed, + len: self.paths.len(), }) } - fn recursive_copy(&self, src: &path::Path, dest: &path::Path) -> std::io::Result<u64> { + fn recursive_copy(&self, src: &path::Path, dest: &path::Path, tx: mpsc::Sender<IOWorkerProgress>, progress: &mut IOWorkerProgress) -> std::io::Result<()> { let mut dest_buf = dest.to_path_buf(); if let Some(s) = src.file_name() { dest_buf.push(s); @@ -89,46 +87,47 @@ impl IOWorkerThread { let file_type = fs::symlink_metadata(src)?.file_type(); if file_type.is_dir() { fs::create_dir(dest_buf.as_path())?; - let mut total = 0; for entry in fs::read_dir(src)? { let entry = entry?; let entry_path = entry.path(); - total += self.recursive_copy(entry_path.as_path(), dest_buf.as_path())?; + self.recursive_copy(entry_path.as_path(), dest_buf.as_path(), + tx.clone(), progress)?; + tx.send(progress.clone()); } - Ok(total) + Ok(()) } else if file_type.is_file() { - fs::copy(src, dest_buf) + progress.processed += fs::copy(src, dest_buf)?; + Ok(()) } else if file_type.is_symlink() { let link_path = fs::read_link(src)?; std::os::unix::fs::symlink(link_path, dest_buf)?; - Ok(0) + Ok(()) } else { - Ok(0) + Ok(()) } } fn paste_cut(&self, tx: mpsc::Sender<IOWorkerProgress>) -> std::io::Result<IOWorkerProgress> { - let mut total = 0; - let len = self.paths.len(); + let mut progress = IOWorkerProgress { + kind: FileOp::Cut, + index: 0, + processed: 0, + len: self.paths.len(), + }; for (i, path) in self.paths.iter().enumerate() { - total += self.recursive_cut(path.as_path(), self.dest.as_path())?; - let progress = IOWorkerProgress { - kind: FileOp::Copy, - index: i, - processed: total, - len, - }; - tx.send(progress); + tx.send(progress.clone()); + self.recursive_cut(path.as_path(), self.dest.as_path(), + tx.clone(), &mut progress)?; } Ok(IOWorkerProgress { - kind: FileOp::Copy, - index: len, - processed: total, - len, + kind: FileOp::Cut, + index: self.paths.len(), + processed: progress.processed, + len: self.paths.len(), }) } - pub fn recursive_cut(&self, src: &path::Path, dest: &path::Path) -> std::io::Result<u64> { + pub fn recursive_cut(&self, src: &path::Path, dest: &path::Path, tx: mpsc::Sender<IOWorkerProgress>, progress: &mut IOWorkerProgress) -> std::io::Result<()> { let mut dest_buf = dest.to_path_buf(); if let Some(s) = src.file_name() { dest_buf.push(s); @@ -138,32 +137,31 @@ impl IOWorkerThread { let file_type = metadata.file_type(); if file_type.is_dir() { match fs::rename(src, dest_buf.as_path()) { - Ok(_) => Ok(metadata.len()), + Ok(_) => { + progress.processed += metadata.len(); + } Err(e) => { - let mut total = 0; fs::create_dir(dest_buf.as_path())?; for entry in fs::read_dir(src)? { - let entry = entry?; - let entry_path = entry.path(); - total += self.recursive_cut(entry_path.as_path(), dest_buf.as_path())?; + let entry_path = entry?.path(); + self.recursive_cut(entry_path.as_path(), + dest_buf.as_path(), tx.clone(), progress)?; } fs::remove_dir(src)?; - Ok(total) } } } else if file_type.is_file() { if fs::rename(src, dest_buf.as_path()).is_err() { fs::copy(src, dest_buf.as_path())?; fs::remove_file(src)?; + progress.processed += metadata.len(); } - Ok(metadata.len()) } else if file_type.is_symlink() { let link_path = fs::read_link(src)?; std::os::unix::fs::symlink(link_path, dest_buf)?; fs::remove_file(src)?; - Ok(metadata.len()) - } else { - Ok(0) + progress.processed += metadata.len(); } + Ok(()) } } @@ -1,13 +1,10 @@ use crate::commands::CommandKeybind; use crate::config::{JoshutoCommandMapping, JoshutoConfig}; use crate::context::JoshutoContext; -use crate::history::DirectoryHistory; -use crate::io::FileOp; use crate::tab::JoshutoTab; use crate::ui; use crate::ui::widgets::{TuiCommandMenu, TuiView}; use crate::util::event::Event; -use crate::util::format; use crate::util::load_child::LoadChild; use crate::util::worker; @@ -31,7 +28,6 @@ pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) -> std::io: while !context.exit { if !context.worker_is_busy() && !context.worker_is_empty() { - context.push_msg("started new io_worker task".to_string()); context.start_next_job(); } diff --git a/src/ui/widgets/tui_folder_view.rs b/src/ui/widgets/tui_folder_view.rs index 6c145dd..df14f73 100644 --- a/src/ui/widgets/tui_folder_view.rs +++ b/src/ui/widgets/tui_folder_view.rs @@ -107,7 +107,7 @@ impl<'a> Widget for TuiFolderView<'a> { if self.show_bottom_status { /* draw the bottom status bar */ if let Some(msg) = self.context.worker_msg() { - let text = Span::styled(msg.as_str(), message_style); + let text = Span::styled(msg, message_style); Paragraph::new(text) .wrap(Wrap { trim: true }) .render(rect, buf); diff --git a/src/ui/widgets/tui_menu.rs b/src/ui/widgets/tui_menu.rs index e37c28f..fcd47ce 100644 --- a/src/ui/widgets/tui_menu.rs +++ b/src/ui/widgets/tui_menu.rs @@ -39,7 +39,6 @@ impl TuiCommandMenu { terminal.draw(|frame| { let f_size: Rect = frame.size(); - { let view = TuiView::new(&context); frame.render_widget(view, f_size); @@ -122,7 +121,6 @@ impl<'a> TuiMenu<'a> { } } - impl<'a> Widget for TuiMenu<'a> { fn render(self, area: Rect, buf: &mut Buffer) { let text_iter = self.options.iter().chain(&[" "]); diff --git a/src/ui/widgets/tui_prompt.rs b/src/ui/widgets/tui_prompt.rs index 112db21..ea6aa49 100644 --- a/src/ui/widgets/tui_prompt.rs +++ b/src/ui/widgets/tui_prompt.rs @@ -2,7 +2,7 @@ use termion::event::Key; use tui::layout::Rect; use tui::style::{Color, Style}; use tui::text::Span; -use tui::widgets::{Paragraph, Wrap}; +use tui::widgets::{Clear, Paragraph, Wrap}; use crate::context::JoshutoContext; use crate::ui::TuiBackend; @@ -48,6 +48,7 @@ impl<'a> TuiPrompt<'a> { height: 1, }; + frame.render_widget(Clear, textfield_rect); frame.render_widget( Paragraph::new(text).wrap(Wrap { trim: true }), textfield_rect, diff --git a/src/ui/widgets/tui_textfield.rs b/src/ui/widgets/tui_textfield.rs index 3ca7ad9..5d6427d 100644 --- a/src/ui/widgets/tui_textfield.rs +++ b/src/ui/widgets/tui_textfield.rs @@ -41,7 +41,9 @@ pub struct TuiTextField<'a> { impl<'a> TuiTextField<'a> { pub fn menu_items<I>(&mut self, items: I) -> &mut Self - where I: Iterator<Item = &'a str> { + where + I: Iterator<Item = &'a str>, + { self._menu_items = Some(items.collect()); self } diff --git a/src/ui/widgets/tui_view.rs b/src/ui/widgets/tui_view.rs index 29a8122..f86eaec 100644 --- a/src/ui/widgets/tui_view.rs +++ b/src/ui/widgets/tui_view.rs @@ -1,8 +1,6 @@ use tui::buffer::Buffer; -use tui::layout::{Direction, Layout, Rect}; -use tui::style::{Color, Style}; -use tui::text::Span; -use tui::widgets::{Paragraph, Widget, Wrap}; +use tui::layout::Rect; +use tui::widgets::Widget; use super::TuiFolderView; use crate::context::JoshutoContext; @@ -14,8 +12,6 @@ pub struct TuiView<'a> { pub show_bottom_status: bool, } -use super::super::{DEFAULT_LAYOUT, NO_PREVIEW_LAYOUT}; - impl<'a> TuiView<'a> { pub fn new(context: &'a JoshutoContext) -> Self { Self { diff --git a/src/util/event.rs b/src/util/event.rs index fca1edb..f019b80 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -5,7 +5,7 @@ use std::thread; use termion::event::Key; use termion::input::TermRead; -use crate::io::{FileOp, IOWorkerProgress}; +use crate::io::IOWorkerProgress; #[derive(Debug)] pub enum Event { diff --git a/src/util/sort.rs b/src/util/sort.rs index fcef409..cd0b84a 100644 --- a/src/util/sort.rs +++ b/src/util/sort.rs @@ -90,7 +90,7 @@ impl SortOption { s => s, }; } - return res; + res } pub fn filter_func(&self) -> fn(&Result<fs::DirEntry, std::io::Error>) -> bool { diff --git a/src/util/worker.rs b/src/util/worker.rs index a8bc66d..8689d8a 100644 --- a/src/util/worker.rs +++ b/src/util/worker.rs @@ -8,13 +8,21 @@ pub fn process_worker_progress(context: &mut JoshutoContext, res: IOWorkerProgre let size_str = format::file_size_to_string(res.processed); match res.kind { FileOp::Cut => { - let msg = format!("moving ({}/{}) {} completed", - res.index + 1, res.len, size_str); + let msg = format!( + "moving ({}/{}) {} completed", + res.index + 1, + res.len, + size_str + ); context.set_worker_msg(msg); } FileOp::Copy => { - let msg = format!("copying ({}/{}) {} completed", - res.index + 1, res.len, size_str); + let msg = format!( + "copying ({}/{}) {} completed", + res.index + 1, + res.len, + size_str + ); context.set_worker_msg(msg); } } @@ -37,8 +45,7 @@ pub fn process_finished_worker( FileOp::Cut => "moved", }; let size_str = format::file_size_to_string(progress.processed); - let msg = format!("successfully {} {} items ({})", - op, progress.len, size_str); + let msg = format!("successfully {} {} items ({})", op, progress.len, size_str); context.push_msg(msg); } Err(e) => { |