summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-06-05 13:43:16 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-06-05 13:43:16 -0400
commit8799211cf9e7547416618725a86463444ed9e342 (patch)
treedfb3b73905a48f341051585845214589b07d21b2 /src
parent724ec36a7ab0dadb0229b9a16eb6179b032be39d (diff)
replace unnecessary path copying with references instead
Diffstat (limited to 'src')
-rw-r--r--src/commands/delete_files.rs6
-rw-r--r--src/commands/file_operations.rs17
-rw-r--r--src/commands/open_file.rs16
-rw-r--r--src/io/dirlist.rs26
-rw-r--r--src/sort.rs10
-rw-r--r--src/unix.rs4
6 files changed, 38 insertions, 41 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index f9ca485..f971ab6 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -20,8 +20,8 @@ impl DeleteFiles {
"delete_files"
}
- pub fn remove_files(paths: Vec<path::PathBuf>) -> Result<(), std::io::Error> {
- for path in &paths {
+ pub fn remove_files(paths: &[&path::PathBuf]) -> Result<(), std::io::Error> {
+ for path in paths {
if let Ok(metadata) = fs::symlink_metadata(path) {
if metadata.is_dir() {
fs::remove_dir_all(&path)?;
@@ -53,7 +53,7 @@ impl DeleteFiles {
ch = 'y' as i32;
}
if ch == 'y' as i32 {
- Self::remove_files(paths)?;
+ Self::remove_files(&paths)?;
ui::wprint_msg(&view.bot_win, "Deleted files");
ReloadDirList::reload(context.curr_tab_index, context)?;
}
diff --git a/src/commands/file_operations.rs b/src/commands/file_operations.rs
index 71bd30b..0dc0dca 100644
--- a/src/commands/file_operations.rs
+++ b/src/commands/file_operations.rs
@@ -34,13 +34,16 @@ impl LocalState {
}
pub fn repopulated_selected_files(dirlist: &JoshutoDirList) -> bool {
- let mut data = SELECTED_FILES.lock().unwrap();
- match dirlist.get_selected_paths() {
- Some(s) => {
- *data = Some(s);
- true
- }
- None => false,
+ let selected: Vec<path::PathBuf> = dirlist
+ .selected_entries()
+ .map(|e| e.file_path().clone())
+ .collect();
+ if selected.is_empty() {
+ false
+ } else {
+ let mut data = SELECTED_FILES.lock().unwrap();
+ *data = Some(selected);
+ true
}
}
}
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs
index 57d2892..34f823a 100644
--- a/src/commands/open_file.rs
+++ b/src/commands/open_file.rs
@@ -57,9 +57,8 @@ impl OpenFile {
}
curr_tab.refresh(view, &context.config_t);
} else {
- let paths: Option<Vec<PathBuf>> = context.tabs[context.curr_tab_index]
- .curr_list
- .get_selected_paths();
+ let curr_tab = &context.tabs[context.curr_tab_index];
+ let paths: Option<Vec<&PathBuf>> = curr_tab.curr_list.get_selected_paths();
if let Some(paths) = paths {
Self::open_file(&paths);
@@ -80,10 +79,9 @@ impl OpenFile {
}
fn open_directory(path: &Path, context: &mut JoshutoContext) -> Result<(), std::io::Error> {
- let curr_tab = &mut context.tabs[context.curr_tab_index];
-
std::env::set_current_dir(path)?;
+ let curr_tab = &mut context.tabs[context.curr_tab_index];
let mut new_curr_list = curr_tab
.history
.pop_or_create(path, &context.config_t.sort_option)?;
@@ -97,7 +95,7 @@ impl OpenFile {
Ok(())
}
- fn open_file(paths: &[PathBuf]) {
+ fn open_file(paths: &[&PathBuf]) {
let mimetype_options = Self::get_options(&paths[0]);
ncurses::savetty();
@@ -145,7 +143,7 @@ impl OpenFileWith {
"open_file_with"
}
- pub fn open_with(paths: &[PathBuf]) {
+ pub fn open_with(paths: &[&PathBuf]) {
const PROMPT: &str = ":open_with ";
let mimetype_options: Vec<&mimetype::JoshutoMimetypeEntry> =
@@ -186,7 +184,7 @@ impl OpenFileWith {
if s < mimetype_options.len() {
ncurses::savetty();
ncurses::endwin();
- unix::open_with_entry(&paths, &mimetype_options[s]);
+ unix::open_with_entry(paths, &mimetype_options[s]);
ncurses::resetty();
ncurses::refresh();
}
@@ -196,7 +194,7 @@ impl OpenFileWith {
user_input.split_whitespace().map(String::from).collect();
ncurses::savetty();
ncurses::endwin();
- unix::open_with_args(&paths, &args);
+ unix::open_with_args(paths, &args);
ncurses::resetty();
ncurses::refresh();
}
diff --git a/src/io/dirlist.rs b/src/io/dirlist.rs
index 0ecd333..f7a57a4 100644
--- a/src/io/dirlist.rs
+++ b/src/io/dirlist.rs
@@ -85,13 +85,10 @@ impl JoshutoDirList {
self.contents.iter().filter(|entry| entry.is_selected())
}
- pub fn get_selected_paths(&self) -> Option<Vec<path::PathBuf>> {
- let vec: Vec<path::PathBuf> = self
- .selected_entries()
- .map(|e| e.file_path().clone())
- .collect();
+ pub fn get_selected_paths(&self) -> Option<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().clone()])
+ Some(vec![self.get_curr_ref()?.file_path()])
} else {
Some(vec)
}
@@ -127,10 +124,19 @@ fn read_dir_list(
sort_option: &sort::SortOption,
) -> Result<Vec<JoshutoDirEntry>, std::io::Error> {
let filter_func = sort_option.filter_func();
- let results: fs::ReadDir = fs::read_dir(path)?;
- let result_vec: Vec<JoshutoDirEntry> = results
+ let results: Vec<JoshutoDirEntry> = fs::read_dir(path)?
.filter(filter_func)
- .filter_map(sort::map_entry_default)
+ .filter_map(map_entry_default)
.collect();
- Ok(result_vec)
+ Ok(results)
+}
+
+fn map_entry_default(result: Result<fs::DirEntry, std::io::Error>) -> Option<JoshutoDirEntry> {
+ match result {
+ Ok(direntry) => match JoshutoDirEntry::from(&direntry) {
+ Ok(s) => Some(s),
+ Err(_) => None,
+ },
+ Err(_) => None,
+ }
}
diff --git a/src/sort.rs b/src/sort.rs
index 9985eef..acc474e 100644
--- a/src/sort.rs
+++ b/src/sort.rs
@@ -107,16 +107,6 @@ fn filter_hidden(result: &Result<fs::DirEntry, std::io::Error>) -> bool {
}
}
-pub fn map_entry_default(result: Result<fs::DirEntry, std::io::Error>) -> Option<JoshutoDirEntry> {
- match result {
- Ok(direntry) => match JoshutoDirEntry::from(&direntry) {
- Ok(s) => Some(s),
- Err(_) => None,
- },
- Err(_) => None,
- }
-}
-
const fn dummy_dir_first(_: &JoshutoDirEntry, _: &JoshutoDirEntry) -> cmp::Ordering {
cmp::Ordering::Equal
}
diff --git a/src/unix.rs b/src/unix.rs
index 70e234b..e8f52aa 100644
--- a/src/unix.rs
+++ b/src/unix.rs
@@ -67,7 +67,7 @@ pub fn set_mode(path: &Path, mode: u32) {
}
}
-pub fn open_with_entry(paths: &[PathBuf], entry: &mimetype::JoshutoMimetypeEntry) {
+pub fn open_with_entry(paths: &[&PathBuf], entry: &mimetype::JoshutoMimetypeEntry) {
let program = entry.program.clone();
let mut command = process::Command::new(program);
@@ -94,7 +94,7 @@ pub fn open_with_entry(paths: &[PathBuf], entry: &mimetype::JoshutoMimetypeEntry
};
}
-pub fn open_with_args(paths: &[PathBuf], args: &[String]) {
+pub fn open_with_args(paths: &[&PathBuf], args: &[String]) {
let program = args[0].clone();
let mut command = process::Command::new(program);