diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-19 18:37:55 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-19 18:37:55 -0400 |
commit | 74a38d8c0dc929ebe8e565fd685d015f5be2df3f (patch) | |
tree | 58f0af18bc022f93195008dcbf4d160450b7ac15 | |
parent | df9869105eede8bb51ff81f25a80482ab125dfb2 (diff) |
unify cut operation across different platforms
- add progress bar back on the bottom
-rw-r--r-- | src/commands/file_operations.rs | 137 | ||||
-rw-r--r-- | src/run.rs | 9 |
2 files changed, 43 insertions, 103 deletions
diff --git a/src/commands/file_operations.rs b/src/commands/file_operations.rs index 570cc99..0db7f41 100644 --- a/src/commands/file_operations.rs +++ b/src/commands/file_operations.rs @@ -186,9 +186,6 @@ impl PasteFiles { &self, context: &mut JoshutoContext, ) -> Result<FileOperationThread, std::io::Error> { - use std::os::linux::fs::MetadataExt; - - let src_ino; let paths = SELECTED_FILES.lock().unwrap().take(); match paths { Some(paths) => { @@ -198,25 +195,15 @@ impl PasteFiles { "no files selected", )); } - src_ino = paths[0].metadata()?.st_dev(); let tab_src = TAB_SRC.load(atomic::Ordering::SeqCst); let tab_dest = context.curr_tab_index; let destination = context.tabs[tab_dest].curr_path.clone(); - let dest_ino = destination.metadata()?.st_dev(); let options = self.options.clone(); let (tx, rx) = mpsc::channel(); - #[cfg(target_os = "linux")] - let handle = if dest_ino == src_ino { - thread::spawn(move || fs_rename_thread(options, tx, destination, paths)) - } else { - thread::spawn(move || fs_cut_thread(options, tx, destination, paths)) - }; - - #[cfg(not(target_os = "linux"))] let handle = thread::spawn(move || fs_cut_thread(options, tx, destination, paths)); let thread = FileOperationThread { @@ -227,12 +214,10 @@ impl PasteFiles { }; Ok(thread) } - None => { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - "no files selected", - )) - } + None => Err(std::io::Error::new( + std::io::ErrorKind::Other, + "no files selected", + )), } } @@ -277,15 +262,15 @@ impl PasteFiles { } } -fn fs_rename_thread( +fn fs_cut_thread( options: fs_extra::dir::CopyOptions, tx: mpsc::Sender<ProgressInfo>, dest: path::PathBuf, paths: Vec<path::PathBuf>, ) -> std::result::Result<(), std::io::Error> { let mut progress_info = ProgressInfo { - bytes_finished: 1, - total_bytes: paths.len() as u64 + 1, + bytes_finished: 4, + total_bytes: paths.len() as u64 + 4, }; let mut destination = dest; @@ -306,86 +291,36 @@ fn fs_rename_thread( i += 1; } } - std::fs::rename(&path, &destination)?; - destination.pop(); - - progress_info.bytes_finished += 1; - match tx.send(progress_info.clone()) { - _ => {} - } - } - Ok(()) -} - -fn fs_cut_thread(options: fs_extra::dir::CopyOptions, - tx: mpsc::Sender<ProgressInfo>, - dest: path::PathBuf, - paths: Vec<path::PathBuf>, -) -> std::result::Result<(), std::io::Error> { - let mut progress_info = ProgressInfo { - bytes_finished: 4, - total_bytes: paths.len() as u64 + 4, - }; - - let mut destination = dest; - - for path in paths { - let file_name = path.file_name().unwrap().to_os_string(); - - if path.symlink_metadata()?.is_dir() { - destination.push(file_name.clone()); - if !options.skip_exist { - let mut i = 0; - while destination.exists() { - destination.pop(); - - let mut file_name = file_name.clone(); - file_name.push(&format!("_{}", i)); - - destination.push(file_name); - i += 1; - } - } - std::fs::create_dir(&destination)?; - let path: Vec<path::PathBuf> = std::fs::read_dir(path)? - .filter_map(|s| { - match s { + match std::fs::rename(&path, &destination) { + Ok(_) => {} + Err(_) => { + if path.symlink_metadata()?.is_dir() { + std::fs::create_dir(&destination)?; + let path: Vec<path::PathBuf> = std::fs::read_dir(path)? + .filter_map(|s| match s { Ok(s) => Some(s.path()), - _ => None + _ => None, + }) + .collect(); + + match fs_extra::move_items(&path, &destination, &options) { + Err(e) => { + let err = + std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)); + return Err(err); } - }) - .collect(); - - match fs_extra::move_items(&path, &destination, &options) { - Err(e) => { - let err = std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)); - return Err(err); - } - _ => {} - } - } else { - destination.push(file_name.clone()); - if !options.skip_exist { - let mut i = 0; - while destination.exists() { - destination.pop(); - - let mut file_name = file_name.clone(); - file_name.push(&format!("_{}", i)); - - destination.push(file_name); - i += 1; + _ => {} + } + } else { + std::fs::copy(&path, &destination)?; + std::fs::remove_file(&path)?; } } - std::fs::copy(&path, &destination)?; - std::fs::remove_file(&path)?; } - destination.pop(); + progress_info.bytes_finished += 1; - match tx.send(progress_info.clone()) { - _ => {} - } + tx.send(progress_info.clone()); } return Ok(()); } @@ -422,13 +357,11 @@ fn fs_copy_thread( } std::fs::create_dir(&destination)?; let path: Vec<path::PathBuf> = std::fs::read_dir(path)? - .filter_map(|s| { - match s { - Ok(s) => Some(s.path()), - _ => None - } - }) - .collect(); + .filter_map(|s| match s { + Ok(s) => Some(s.path()), + _ => None, + }) + .collect(); match fs_extra::copy_items(&path, &destination, &options) { Err(e) => { @@ -57,7 +57,7 @@ fn join_thread(context: &mut JoshutoContext, thread: FileOperationThread, view: 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() { let dirty_tab = &mut context.tabs[tab_src]; @@ -98,6 +98,13 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { join_thread(context, thread, view); ncurses::doupdate(); } + Ok(progress_info) => { + ui::draw_progress_bar( + &view.bot_win, + progress_info.bytes_finished as f32 / + progress_info.total_bytes as f32, + ); + } _ => {} } } |