diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-06-05 16:41:26 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-06-05 16:41:31 -0400 |
commit | 89dadc7c604cb8367b90c4cc0f097d2fabc93ac6 (patch) | |
tree | 72725729821171385d36cc60579366a3b2e91c01 | |
parent | 530973aea3fa46c469541bc291513a75e3aacd3e (diff) |
get_selected_paths now returns just a vec rather an option
- fix not being able to select the current entry
-rw-r--r-- | src/commands/delete_files.rs | 31 | ||||
-rw-r--r-- | src/commands/file_operations.rs | 40 | ||||
-rw-r--r-- | src/commands/open_file.rs | 12 | ||||
-rw-r--r-- | src/io/dirlist.rs | 9 | ||||
-rw-r--r-- | src/io/metadata.rs | 1 | ||||
-rw-r--r-- | src/ui.rs | 60 |
6 files changed, 85 insertions, 68 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index f971ab6..4d7924b 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -44,19 +44,24 @@ impl DeleteFiles { let curr_tab = &mut context.tabs[context.curr_tab_index]; let mut ch = ncurses::getch(); if ch == 'y' as i32 || ch == KEYMAP_T.enter { - if let Some(paths) = curr_tab.curr_list.get_selected_paths() { - if paths.len() > 1 { - ui::wprint_msg(&view.bot_win, "Are you sure? (y/N)"); - ncurses::doupdate(); - ch = ncurses::getch(); - } else { - ch = 'y' as i32; - } - if ch == 'y' as i32 { - Self::remove_files(&paths)?; - ui::wprint_msg(&view.bot_win, "Deleted files"); - ReloadDirList::reload(context.curr_tab_index, context)?; - } + let paths = curr_tab.curr_list.get_selected_paths(); + if paths.is_empty() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "no files selected", + )); + } + if paths.len() > 1 { + ui::wprint_msg(&view.bot_win, "Are you sure? (y/N)"); + ncurses::doupdate(); + ch = ncurses::getch(); + } else { + ch = 'y' as i32; + } + if ch == 'y' as i32 { + Self::remove_files(&paths)?; + ui::wprint_msg(&view.bot_win, "Deleted files"); + ReloadDirList::reload(context.curr_tab_index, context)?; } } Ok(()) diff --git a/src/commands/file_operations.rs b/src/commands/file_operations.rs index 0dc0dca..aba8c7e 100644 --- a/src/commands/file_operations.rs +++ b/src/commands/file_operations.rs @@ -33,17 +33,19 @@ impl LocalState { TAB_SRC.store(tab_index, atomic::Ordering::Release); } - pub fn repopulated_selected_files(dirlist: &JoshutoDirList) -> bool { - let selected: Vec<path::PathBuf> = dirlist - .selected_entries() - .map(|e| e.file_path().clone()) - .collect(); + pub fn repopulated_selected_files(dirlist: &JoshutoDirList) -> Result<(), std::io::Error> { + let selected = dirlist.get_selected_paths(); if selected.is_empty() { - false + Err(std::io::Error::new( + std::io::ErrorKind::Other, + "no files selected", + )) } else { + let selected_clone: Vec<path::PathBuf> = + selected.iter().map(|p| (*p).clone()).collect(); let mut data = SELECTED_FILES.lock().unwrap(); - *data = Some(selected); - true + *data = Some(selected_clone); + Ok(()) } } } @@ -93,11 +95,14 @@ impl std::fmt::Display for CutFiles { impl JoshutoRunnable for CutFiles { fn execute(&self, context: &mut JoshutoContext, _: &JoshutoView) -> Result<(), JoshutoError> { let curr_tab = context.curr_tab_ref(); - if LocalState::repopulated_selected_files(&curr_tab.curr_list) { - LocalState::set_file_op(FileOp::Cut); - LocalState::set_tab_src(context.curr_tab_index); + match LocalState::repopulated_selected_files(&curr_tab.curr_list) { + Ok(_) => { + LocalState::set_file_op(FileOp::Cut); + LocalState::set_tab_src(context.curr_tab_index); + Ok(()) + } + Err(e) => Err(JoshutoError::IO(e)), } - Ok(()) } } @@ -124,11 +129,14 @@ impl std::fmt::Display for CopyFiles { impl JoshutoRunnable for CopyFiles { fn execute(&self, context: &mut JoshutoContext, _: &JoshutoView) -> Result<(), JoshutoError> { let curr_tab = context.curr_tab_ref(); - if LocalState::repopulated_selected_files(&curr_tab.curr_list) { - LocalState::set_file_op(FileOp::Copy); - LocalState::set_tab_src(context.curr_tab_index); + match LocalState::repopulated_selected_files(&curr_tab.curr_list) { + Ok(_) => { + LocalState::set_file_op(FileOp::Copy); + LocalState::set_tab_src(context.curr_tab_index); + Ok(()) + } + Err(e) => Err(JoshutoError::IO(e)), } - Ok(()) } } diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 34f823a..bdc8e44 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -58,14 +58,13 @@ impl OpenFile { curr_tab.refresh(view, &context.config_t); } else { let curr_tab = &context.tabs[context.curr_tab_index]; - let paths: Option<Vec<&PathBuf>> = curr_tab.curr_list.get_selected_paths(); + let paths = curr_tab.curr_list.get_selected_paths(); - if let Some(paths) = paths { - Self::open_file(&paths); - } else { + if paths.is_empty() { let err = std::io::Error::new(std::io::ErrorKind::NotFound, "No files selected"); return Err(err); } + Self::open_file(&paths); let curr_tab = &mut context.tabs[context.curr_tab_index]; if curr_tab.curr_list.need_update() { curr_tab @@ -214,9 +213,8 @@ impl std::fmt::Display for OpenFileWith { impl JoshutoRunnable for OpenFileWith { fn execute(&self, context: &mut JoshutoContext, _: &JoshutoView) -> Result<(), JoshutoError> { let curr_list = &context.tabs[context.curr_tab_index].curr_list; - if let Some(paths) = curr_list.get_selected_paths() { - Self::open_with(&paths); - } + let paths = curr_list.get_selected_paths(); + Self::open_with(&paths); Ok(()) } } diff --git a/src/io/dirlist.rs b/src/io/dirlist.rs index f7a57a4..1b8d203 100644 --- a/src/io/dirlist.rs +++ b/src/io/dirlist.rs @@ -85,12 +85,15 @@ impl JoshutoDirList { self.contents.iter().filter(|entry| entry.is_selected()) } - pub fn get_selected_paths(&self) -> Option<Vec<&path::PathBuf>> { + pub fn get_selected_paths(&self) -> Vec<&path::PathBuf> { let vec: Vec<&path::PathBuf> = self.selected_entries().map(|e| e.file_path()).collect(); if vec.is_empty() { - Some(vec![self.get_curr_ref()?.file_path()]) + match self.get_curr_ref() { + Some(s) => vec![s.file_path()], + _ => vec![], + } } else { - Some(vec) + vec } } diff --git a/src/io/metadata.rs b/src/io/metadata.rs index 341e8f2..fb85a6e 100644 --- a/src/io/metadata.rs +++ b/src/io/metadata.rs @@ -23,6 +23,7 @@ impl JoshutoMetadata { let modified = metadata.modified()?; let permissions = metadata.permissions(); let file_type = metadata.file_type(); + #[cfg(unix)] let uid = metadata.uid(); #[cfg(unix)] @@ -1,8 +1,8 @@ use std::fs; use std::time; -use users::UsersCache; use users::mock::{Groups, Users}; +use users::UsersCache; use crate::config::{JoshutoColorTheme, JoshutoConfig}; use crate::context::JoshutoContext; @@ -241,21 +241,23 @@ pub fn display_contents( win.queue_for_refresh(); } -pub fn wprint_file_status(win: &window::JoshutoPanel, entry: &JoshutoDirEntry, index: usize, len: usize) { +pub fn wprint_file_status( + win: &window::JoshutoPanel, + entry: &JoshutoDirEntry, + index: usize, + len: usize, +) { wprint_file_mode(win.win, entry); ncurses::waddch(win.win, ' ' as ncurses::chtype); - ncurses::waddstr( - win.win, - format!("{}/{} ", index + 1, len).as_str(), - ); + ncurses::waddstr(win.win, format!("{}/{} ", index + 1, len).as_str()); let usercache: UsersCache = UsersCache::new(); match usercache.get_user_by_uid(entry.metadata.uid) { Some(s) => match s.name().to_str() { Some(name) => ncurses::waddstr(win.win, name), None => ncurses::waddstr(win.win, "OsStr error"), - } + }, None => ncurses::waddstr(win.win, "unknown user"), }; ncurses::waddch(win.win, ' ' as ncurses::chtype); @@ -263,7 +265,7 @@ pub fn wprint_file_status(win: &window::JoshutoPanel, entry: &JoshutoDirEntry, i Some(s) => match s.name().to_str() { Some(name) => ncurses::waddstr(win.win, name), None => ncurses::waddstr(win.win, "OsStr error"), - } + }, None => ncurses::waddstr(win.win, "unknown user"), }; @@ -285,33 +287,33 @@ pub fn wprint_file_mode(win: ncurses::WINDOW, file: &JoshutoDirEntry) { pub fn wprint_file_info(win: ncurses::WINDOW, file: &JoshutoDirEntry) { #[cfg(unix)] { - use std::os::unix::fs::PermissionsExt; + use std::os::unix::fs::PermissionsExt; - let mode = file.metadata.permissions.mode(); + let mode = file.metadata.permissions.mode(); - let mtime_string = file_mtime_to_string(file.metadata.modified); - ncurses::waddstr(win, &mtime_string); - ncurses::waddch(win, ' ' as ncurses::chtype); + let mtime_string = file_mtime_to_string(file.metadata.modified); + ncurses::waddstr(win, &mtime_string); + ncurses::waddch(win, ' ' as ncurses::chtype); - if file.file_path().is_dir() { - let is_link: u32 = libc::S_IFLNK as u32; - if mode >> 9 & is_link >> 9 == mode >> 9 { - if let Ok(path) = fs::read_link(&file.file_path()) { - ncurses::waddstr(win, " -> "); - ncurses::waddstr(win, path.to_str().unwrap()); + if file.file_path().is_dir() { + let is_link: u32 = libc::S_IFLNK as u32; + if mode >> 9 & is_link >> 9 == mode >> 9 { + if let Ok(path) = fs::read_link(&file.file_path()) { + ncurses::waddstr(win, " -> "); + ncurses::waddstr(win, path.to_str().unwrap()); + } } + } else { + let file_size_string = file_size_to_string_detailed(file.metadata.len as f64); + ncurses::waddstr(win, &file_size_string); } - } else { - let file_size_string = file_size_to_string_detailed(file.metadata.len as f64); - ncurses::waddstr(win, &file_size_string); - } - /* - ncurses::waddstr(win, " "); - if let Some(s) = tree_magic::from_filepath(&file.file_path()) { - ncurses::waddstr(win, &s); - } - */ + /* + ncurses::waddstr(win, " "); + if let Some(s) = tree_magic::from_filepath(&file.file_path()) { + ncurses::waddstr(win, &s); + } + */ } } |