From c258640705732c469dcf9b2e6ffd2fb68b7e0b12 Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Sat, 11 Feb 2023 14:22:13 -0500 Subject: add current file to file operations --- src/io/file_operation.rs | 13 ++++++++ src/io/io_worker.rs | 75 +++++++++++++++++++++++--------------------- src/ui/widgets/tui_worker.rs | 49 ++++++++++++++++++----------- src/util/fs.rs | 35 +++++++++++++++++++++ src/util/mod.rs | 1 + 5 files changed, 118 insertions(+), 55 deletions(-) create mode 100644 src/util/fs.rs diff --git a/src/io/file_operation.rs b/src/io/file_operation.rs index 43589aa..c6dfb74 100644 --- a/src/io/file_operation.rs +++ b/src/io/file_operation.rs @@ -1,3 +1,5 @@ +use std::path; + #[derive(Clone, Copy, Debug)] pub enum FileOperation { Cut, @@ -59,6 +61,7 @@ impl std::fmt::Display for FileOperationOptions { #[derive(Clone, Debug)] pub struct FileOperationProgress { _kind: FileOperation, + _current_file: path::PathBuf, _files_processed: usize, _total_files: usize, _bytes_processed: u64, @@ -68,6 +71,7 @@ pub struct FileOperationProgress { impl FileOperationProgress { pub fn new( _kind: FileOperation, + _current_file: path::PathBuf, _files_processed: usize, _total_files: usize, _bytes_processed: u64, @@ -75,6 +79,7 @@ impl FileOperationProgress { ) -> Self { Self { _kind, + _current_file, _files_processed, _total_files, _bytes_processed, @@ -86,6 +91,14 @@ impl FileOperationProgress { self._kind } + pub fn current_file(&self) -> &path::Path { + self._current_file.as_path() + } + + pub fn set_current_file(&mut self, current_file: path::PathBuf) { + self._current_file = current_file; + } + pub fn files_processed(&self) -> usize { self._files_processed } diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs index 6ed7b9f..42a3ef5 100644 --- a/src/io/io_worker.rs +++ b/src/io/io_worker.rs @@ -1,4 +1,3 @@ -use std::collections::VecDeque; use std::fs; use std::io; use std::path; @@ -8,6 +7,7 @@ use std::sync::mpsc; use std::os::unix; use crate::io::{FileOperation, FileOperationOptions, FileOperationProgress}; +use crate::util::fs::query_number_of_items; use crate::util::name_resolution::rename_filename_conflict; #[derive(Clone, Debug)] @@ -55,7 +55,14 @@ impl IoWorkerThread { tx: mpsc::Sender, ) -> io::Result { let (total_files, total_bytes) = query_number_of_items(&self.paths)?; - let mut progress = FileOperationProgress::new(self.kind(), 0, total_files, 0, total_bytes); + let mut progress = FileOperationProgress::new( + self.kind(), + self.paths[0].to_path_buf(), + 0, + total_files, + 0, + total_bytes, + ); for path in self.paths.iter() { let _ = tx.send(progress.clone()); recursive_copy( @@ -74,7 +81,14 @@ impl IoWorkerThread { tx: mpsc::Sender, ) -> io::Result { let (total_files, total_bytes) = query_number_of_items(&self.paths)?; - let mut progress = FileOperationProgress::new(self.kind(), 0, total_files, 0, total_bytes); + let mut progress = FileOperationProgress::new( + self.kind(), + self.paths[0].to_path_buf(), + 0, + total_files, + 0, + total_bytes, + ); for path in self.paths.iter() { let _ = tx.send(progress.clone()); recursive_cut( @@ -94,7 +108,14 @@ impl IoWorkerThread { ) -> io::Result { let total_files = self.paths.len(); let total_bytes = total_files as u64; - let mut progress = FileOperationProgress::new(self.kind(), 0, total_files, 0, total_bytes); + let mut progress = FileOperationProgress::new( + self.kind(), + self.paths[0].to_path_buf(), + 0, + total_files, + 0, + total_bytes, + ); #[cfg(unix)] for src in self.paths.iter() { @@ -119,7 +140,14 @@ impl IoWorkerThread { ) -> io::Result { let total_files = self.paths.len(); let total_bytes = total_files as u64; - let mut progress = FileOperationProgress::new(self.kind(), 0, total_files, 0, total_bytes); + let mut progress = FileOperationProgress::new( + self.kind(), + self.paths[0].to_path_buf(), + 0, + total_files, + 0, + total_bytes, + ); #[cfg(unix)] for src in self.paths.iter() { @@ -166,6 +194,7 @@ impl IoWorkerThread { let (total_files, total_bytes) = query_number_of_items(&self.paths)?; let progress = FileOperationProgress::new( self.kind(), + self.paths[0].to_path_buf(), total_files, total_files, total_bytes, @@ -180,37 +209,6 @@ impl IoWorkerThread { } } -fn query_number_of_items(paths: &[path::PathBuf]) -> io::Result<(usize, u64)> { - let mut total_bytes = 0; - let mut total_files = 0; - - let mut dirs: VecDeque = VecDeque::new(); - for path in paths.iter() { - let metadata = path.symlink_metadata()?; - if metadata.is_dir() { - dirs.push_back(path.clone()); - } else { - let metadata = path.symlink_metadata()?; - total_bytes += metadata.len(); - total_files += 1; - } - } - - while let Some(dir) = dirs.pop_front() { - for entry in fs::read_dir(dir)? { - let path = entry?.path(); - if path.is_dir() { - dirs.push_back(path); - } else { - let metadata = path.symlink_metadata()?; - total_bytes += metadata.len(); - total_files += 1; - } - } - } - Ok((total_files, total_bytes)) -} - pub fn recursive_copy( tx: &mpsc::Sender, src: &path::Path, @@ -225,6 +223,9 @@ pub fn recursive_copy( if !options.overwrite { rename_filename_conflict(&mut dest_buf); } + + progress.set_current_file(src.to_path_buf()); + let file_type = fs::symlink_metadata(src)?.file_type(); if file_type.is_dir() { match fs::create_dir(dest_buf.as_path()) { @@ -280,6 +281,8 @@ pub fn recursive_cut( let metadata = fs::symlink_metadata(src)?; let file_type = metadata.file_type(); + progress.set_current_file(src.to_path_buf()); + match fs::rename(src, dest_buf.as_path()) { Ok(_) => { let bytes_processed = progress.bytes_processed() + metadata.len(); diff --git a/src/ui/widgets/tui_worker.rs b/src/ui/widgets/tui_worker.rs index 4edfa22..3d06131 100644 --- a/src/ui/widgets/tui_worker.rs +++ b/src/ui/widgets/tui_worker.rs @@ -19,6 +19,9 @@ impl<'a> TuiWorker<'a> { impl<'a> Widget for TuiWorker<'a> { fn render(self, area: Rect, buf: &mut Buffer) { + if area.height < 7 { + return; + } match self.context.worker_ref() { Some(io_obs) => { if let Some(progress) = io_obs.progress.as_ref() { @@ -27,19 +30,19 @@ impl<'a> Widget for TuiWorker<'a> { ..area }; TuiCurrentWorker::new(io_obs, progress).render(current_area, buf); + } - // draw queued up work - let style = Style::default() - .fg(Color::Yellow) - .add_modifier(Modifier::BOLD); - buf.set_stringn(0, 5, "Queue:", area.width as usize, style); + // draw queued up work + let style = Style::default() + .fg(Color::Yellow) + .add_modifier(Modifier::BOLD); + buf.set_stringn(0, 6, "Queue:", area.width as usize, style); - let queue_area = Rect { - y: area.y + 7, - ..area - }; - TuiWorkerQueue::new(self.context).render(queue_area, buf); - } + let queue_area = Rect { + y: area.y + 7, + ..area + }; + TuiWorkerQueue::new(self.context).render(queue_area, buf); } _ => { let style = Style::default(); @@ -76,22 +79,30 @@ impl<'a> Widget for TuiCurrentWorker<'a> { let total_size = format::file_size_to_string(self.progress.total_bytes()); let msg = format!( - "{} ({}/{}) ({}/{})\n", + "{} ({}/{}) ({}/{}) {:?}", op_str, self.progress.files_processed() + 1, self.progress.total_files(), processed_size, total_size, + self.observer.dest_path(), ); buf.set_stringn(left, top, msg, right as usize, Style::default()); - buf.set_stringn( - left, - top + 1, - format!("{:?}\n", self.observer.dest_path()), - right as usize, - Style::default(), - ); + if let Some(file_name) = self + .progress + .current_file() + .file_name() + .map(|s| s.to_string_lossy()) + { + buf.set_stringn( + left, + top + 1, + format!("{}", file_name), + right as usize, + Style::default(), + ); + } // draw a progress bar let progress_bar_width = (self.progress.files_processed() as f32 diff --git a/src/util/fs.rs b/src/util/fs.rs new file mode 100644 index 0000000..cabf374 --- /dev/null +++ b/src/util/fs.rs @@ -0,0 +1,35 @@ +use std::collections::VecDeque; +use std::fs; +use std::io; +use std::path; + +pub fn query_number_of_items(paths: &[path::PathBuf]) -> io::Result<(usize, u64)> { + let mut total_bytes = 0; + let mut total_files = 0; + + let mut dirs: VecDeque = VecDeque::new(); + for path in paths.iter() { + let metadata = path.symlink_metadata()?; + if metadata.is_dir() { + dirs.push_back(path.clone()); + } else { + let metadata = path.symlink_metadata()?; + total_bytes += metadata.len(); + total_files += 1; + } + } + + while let Some(dir) = dirs.pop_front() { + for entry in fs::read_dir(dir)? { + let path = entry?.path(); + if path.is_dir() { + dirs.push_back(path); + } else { + let metadata = path.symlink_metadata()?; + total_bytes += metadata.len(); + total_files += 1; + } + } + } + Ok((total_files, total_bytes)) +} \ No newline at end of file diff --git a/src/util/mod.rs b/src/util/mod.rs index 562ec57..313f193 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -2,6 +2,7 @@ pub mod devicons; pub mod format; +pub mod fs; pub mod keyparse; pub mod mimetype; pub mod name_resolution; -- cgit v1.2.3 From 3339c9b2294890fa4bd017794345bc1315cca92c Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Fri, 10 Mar 2023 10:45:22 -0500 Subject: rework delete --- src/key_command/impl_from_str.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs index 953793f..1d45658 100644 --- a/src/key_command/impl_from_str.rs +++ b/src/key_command/impl_from_str.rs @@ -243,11 +243,12 @@ impl std::str::FromStr for Command { } Ok(Self::PasteFiles { options }) } else if command == CMD_DELETE_FILES { - let (mut permanently, mut background) = (false, true); + let (mut permanently, mut background) = (false, false); for arg in arg.split_whitespace() { + eprintln!("arg: {:?}", arg); match arg { - "--foreground=true" => background = true, - "--foreground=false" => background = false, + "--background=true" => background = true, + "--background=false" => background = false, "--permanently" => permanently = true, _ => { return Err(JoshutoError::new( -- cgit v1.2.3