diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-12-30 15:26:31 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-12-30 15:26:31 +0100 |
commit | 8995c5a35b488c07297b56131d1a85483192ab5c (patch) | |
tree | 419e61a46bf5e9d6373b852e254b3fb1fb8bb402 | |
parent | e3844dc16fcff2fee67dee6dfa52a507bb89321a (diff) |
replace some to_owned by referencestree-view
-rw-r--r-- | development.md | 1 | ||||
-rw-r--r-- | src/completion.rs | 38 | ||||
-rw-r--r-- | src/event_exec.rs | 29 | ||||
-rw-r--r-- | src/fileinfo.rs | 39 | ||||
-rw-r--r-- | src/preview.rs | 9 | ||||
-rw-r--r-- | src/tab.rs | 9 | ||||
-rw-r--r-- | src/term_manager.rs | 6 |
7 files changed, 64 insertions, 67 deletions
diff --git a/development.md b/development.md index fb645fd..907c919 100644 --- a/development.md +++ b/development.md @@ -283,6 +283,7 @@ - [x] use as another display - [ ] navigation - [ ] zoxide support +- [ ] cache users & groups - [ ] Version 0.2.0 : tests diff --git a/src/completion.rs b/src/completion.rs index 5989801..2d44c0a 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -81,9 +81,9 @@ impl Completion { self.proposals = proposals; } - fn extend(&mut self, proposals: Vec<String>) { + fn extend(&mut self, proposals: &[String]) { self.index = 0; - self.proposals.extend_from_slice(&proposals) + self.proposals.extend_from_slice(proposals) } /// Empty the proposals `Vec`. @@ -146,14 +146,14 @@ impl Completion { fn extend_absolute_paths(&mut self, parent: &str, last_name: &str) { if let Ok(path) = std::fs::canonicalize(parent) { if let Ok(entries) = fs::read_dir(path) { - self.extend(Self::entries_matching_filename(entries, last_name)) + self.extend(&Self::entries_matching_filename(entries, last_name)) } } } fn extend_relative_paths(&mut self, current_path: &str, last_name: &str) { if let Ok(entries) = fs::read_dir(current_path) { - self.extend(Self::entries_matching_filename(entries, last_name)) + self.extend(&Self::entries_matching_filename(entries, last_name)) } } @@ -168,26 +168,26 @@ impl Completion { /// Looks for programs in $PATH completing the one typed by the user. fn exec(&mut self, input_string: &str) -> FmResult<()> { let mut proposals: Vec<String> = vec![]; - for path in std::env::var_os("PATH") - .unwrap_or_default() - .to_str() - .unwrap_or_default() - .split(':') - .filter(|path| std::path::Path::new(path).exists()) - { - let comp: Vec<String> = fs::read_dir(path)? - .filter_map(|e| e.ok()) - .filter(|e| { - e.file_type().unwrap().is_file() && filename_startswith(e, input_string) - }) - .map(|e| e.path().to_string_lossy().into_owned()) - .collect(); - proposals.extend(comp); + if let Some(paths) = std::env::var_os("PATH") { + for path in std::env::split_paths(&paths).filter(|path| path.exists()) { + proposals.extend(Self::find_completion_in_path(path, input_string)?); + } } self.update(proposals); Ok(()) } + fn find_completion_in_path( + path: std::path::PathBuf, + input_string: &str, + ) -> FmResult<Vec<String>> { + Ok(fs::read_dir(path)? + .filter_map(|e| e.ok()) + .filter(|e| e.file_type().unwrap().is_file() && filename_startswith(e, input_string)) + .map(|e| e.path().to_string_lossy().into_owned()) + .collect()) + } + /// Looks for file within current folder completing what the user typed. fn search(&mut self, input_string: &str, path_content: &PathContent) -> FmResult<()> { self.update( diff --git a/src/event_exec.rs b/src/event_exec.rs index b3d66b5..e1f698c 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -156,10 +156,9 @@ impl EventExec { /// If the saved path is invalid, it does nothing but reset the view. pub fn exec_marks_jump(status: &mut Status, c: char) -> FmResult<()> { if let Some(path) = status.marks.get(c) { - let path = path.to_owned(); - status.selected().history.push(&path); - status.selected().path_content = PathContent::new(path, status.selected().show_hidden)?; - }; + let path = path.clone(); + status.selected().set_pathcontent(&path)? + } Self::event_normal(status.selected()) } @@ -225,7 +224,7 @@ impl EventExec { u32::from_str_radix(&status.selected().input.string(), 8).unwrap_or(0_u32); if permissions <= Status::MAX_PERMISSIONS { for path in status.flagged.content.iter() { - Status::set_permissions(path.clone(), permissions)? + Status::set_permissions(path, permissions)? } status.flagged.clear() } @@ -754,7 +753,7 @@ impl EventExec { } // "nvim-send --remote-send '<esc>:e readme.md<cr>' --servername 127.0.0.1:8888" if let Ok(nvim_listen_address) = Self::nvim_listen_address(tab) { - if let Some(path_str) = tab.path_content.selected_path_str() { + if let Some(path_str) = tab.path_content.selected_path_string() { let _ = execute_in_child( NVIM_RPC_SENDER, &vec![ @@ -785,7 +784,7 @@ impl EventExec { /// Copy the selected filepath to the clipboard. The absolute path. pub fn event_filepath_to_clipboard(tab: &Tab) -> FmResult<()> { - if let Some(filepath) = tab.path_content.selected_path_str() { + if let Some(filepath) = tab.path_content.selected_path_string() { let mut ctx = ClipboardContext::new()?; ctx.set_contents(filepath)?; // For some reason, it's not writen if you don't read it back... @@ -842,7 +841,7 @@ impl EventExec { } fs::rename( tab.path_content - .selected_path_str() + .selected_path_string() .ok_or_else(|| FmError::custom("exec rename", "File not found"))?, tab.path_content .path @@ -898,7 +897,7 @@ impl EventExec { let mut args: Vec<&str> = exec_command.split(' ').collect(); let command = args.remove(0); if std::path::Path::new(command).exists() { - let path = &tab.path_content.selected_path_str().ok_or_else(|| { + let path = &tab.path_content.selected_path_string().ok_or_else(|| { FmError::custom("exec exec", &format!("can't find command {}", command)) })?; args.push(path); @@ -915,7 +914,7 @@ impl EventExec { let tab = status.selected_non_mut(); execute_in_child( DEFAULT_DRAGNDROP, - &vec![&tab.path_content.selected_path_str().ok_or_else(|| { + &vec![&tab.path_content.selected_path_string().ok_or_else(|| { FmError::custom( "event drag n drop", "can't find dragon-drop in the system. Is the application installed?", @@ -995,7 +994,7 @@ impl EventExec { let path = string_to_path(completed)?; tab.input.reset(); tab.history.push(&path); - tab.path_content = PathContent::new(path, tab.show_hidden)?; + tab.path_content = PathContent::new(&path, tab.show_hidden)?; tab.window.reset(tab.path_content.content.len()); Ok(()) } @@ -1007,9 +1006,8 @@ impl EventExec { let path = tab .shortcut .selected() - .ok_or_else(|| FmError::custom("exec shortcut", "empty shortcuts"))? - .to_owned(); - tab.history.push(&path); + .ok_or_else(|| FmError::custom("exec shortcut", "empty shortcuts"))?; + tab.history.push(path); tab.path_content = PathContent::new(path, tab.show_hidden)?; Self::event_normal(tab) } @@ -1021,8 +1019,7 @@ impl EventExec { tab.path_content = PathContent::new( tab.history .selected() - .ok_or_else(|| FmError::custom("exec history", "path unreachable"))? - .to_owned(), + .ok_or_else(|| FmError::custom("exec history", "path unreachable"))?, tab.show_hidden, )?; tab.history.drop_queue(); diff --git a/src/fileinfo.rs b/src/fileinfo.rs index 2cd7b0e..46480a2 100644 --- a/src/fileinfo.rs +++ b/src/fileinfo.rs @@ -229,20 +229,21 @@ impl PathContent { /// Reads the paths and creates a new `PathContent`. /// Files are sorted by filename by default. /// Selects the first file if any. - pub fn new(path: path::PathBuf, show_hidden: bool) -> FmResult<Self> { + pub fn new(path: &path::Path, show_hidden: bool) -> FmResult<Self> { + let path = path.to_owned(); let filter = FilterKind::All; - let mut files = Self::files(&path, show_hidden, filter.clone())?; + let mut content = Self::files(&path, show_hidden, &filter)?; let sort_kind = SortKind::default(); - sort_kind.sort(&mut files); + sort_kind.sort(&mut content); let selected_index: usize = 0; - if !files.is_empty() { - files[selected_index].select(); + if !content.is_empty() { + content[selected_index].select(); } - let used_space = get_used_space(&files); + let used_space = get_used_space(&content); Ok(Self { path, - content: files, + content, index: selected_index, show_hidden, sort_kind, @@ -252,7 +253,7 @@ impl PathContent { } pub fn change_directory(&mut self, path: &path::Path) -> FmResult<()> { - self.content = Self::files(path, self.show_hidden, self.filter.clone())?; + self.content = Self::files(path, self.show_hidden, &self.filter)?; self.sort_kind.sort(&mut self.content); self.index = 0; if !self.content.is_empty() { @@ -268,7 +269,11 @@ impl PathContent { self.filter = filter } - fn files(path: &path::Path, show_hidden: bool, filter: FilterKind) -> FmResult<Vec<FileInfo>> { + fn files( + path: &path::Path, + show_hidden: bool, + filter_kind: &FilterKind, + ) -> FmResult<Vec<FileInfo>> { match read_dir(path) { Ok(read_dir) => { let files: Vec<FileInfo> = if show_hidden { @@ -276,7 +281,7 @@ impl PathContent { .filter_map(|res_direntry| res_direntry.ok()) .map(|direntry| FileInfo::new(&direntry)) .filter_map(|res_file_entry| res_file_entry.ok()) - .filter(|fileinfo| filter.filter_by(fileinfo)) + .filter(|fileinfo| filter_kind.filter_by(fileinfo)) .collect() } else { read_dir @@ -284,7 +289,7 @@ impl PathContent { .filter(|e| is_not_hidden(e).unwrap_or(true)) .map(|direntry| FileInfo::new(&direntry)) .filter_map(|res_file_entry| res_file_entry.ok()) - .filter(|fileinfo| filter.filter_by(fileinfo)) + .filter(|fileinfo| filter_kind.filter_by(fileinfo)) .collect() }; Ok(files) @@ -353,7 +358,7 @@ impl PathContent { /// Reads and sort the content with current key. /// Select the first file if any. pub fn reset_files(&mut self) -> Result<(), FmError> { - self.content = Self::files(&self.path, self.show_hidden, self.filter.clone())?; + self.content = Self::files(&self.path, self.show_hidden, &self.filter)?; self.sort_kind = SortKind::default(); self.sort(); self.index = 0; @@ -364,7 +369,7 @@ impl PathContent { } /// Path of the currently selected file. - pub fn selected_path_str(&self) -> Option<String> { + pub fn selected_path_string(&self) -> Option<String> { Some(self.selected()?.path.to_str()?.to_owned()) } @@ -511,20 +516,20 @@ fn convert_octal_mode(mode: usize) -> &'static str { /// Reads the owner name and returns it as a string. fn extract_owner(metadata: &Metadata) -> FmResult<String> { Ok(get_user_by_uid(metadata.uid()) - .ok_or_else(|| FmError::custom("owner", "Couldn't read uid"))? + .ok_or_else(|| FmError::custom("extract owner", "Couldn't read uid"))? .name() .to_str() - .ok_or_else(|| FmError::custom("metadata", "Couldn't read owner name"))? + .ok_or_else(|| FmError::custom("extract owner", "Couldn't read owner name"))? .to_owned()) } /// Reads the group name and returns it as a string. fn extract_group(metadata: &Metadata) -> FmResult<String> { Ok(get_group_by_gid(metadata.gid()) - .ok_or_else(|| FmError::custom("owner", "Couldn't read gid"))? + .ok_or_else(|| FmError::custom("extract group", "Couldn't read gid"))? .name() .to_str() - .ok_or_else(|| FmError::custom("metadata", "Couldn't read group name"))? + .ok_or_else(|| FmError::custom("extract group", "Couldn't read group name"))? .to_owned()) } diff --git a/src/preview.rs b/src/preview.rs index dac707c..7ae7cca 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -506,13 +506,10 @@ impl Directory { 10, )?; let tree_str = format!("{}", tree); - let tree_lines: Vec<String> = tree_str.lines().map(|s| s.to_owned()).collect(); - let len = tree_lines.len(); + let content: Vec<String> = tree_str.lines().map(|s| s.to_owned()).collect(); + let len = content.len(); - Ok(Self { - content: tree_lines, - len, - }) + Ok(Self { content, len }) } fn len(&self) -> usize { @@ -51,7 +51,7 @@ impl Tab { /// Creates a new tab from args and height. pub fn new(args: Args, height: usize) -> FmResult<Self> { let path = std::fs::canonicalize(path::Path::new(&args.path))?; - let path_content = PathContent::new(path.clone(), false)?; + let path_content = PathContent::new(&path, false)?; let show_hidden = false; let nvim_server = args.server; let mode = Mode::Normal; @@ -105,13 +105,12 @@ impl Tab { /// Fail silently if the current directory is empty or if the selected /// file isn't a directory. pub fn go_to_child(&mut self) -> FmResult<()> { - let childpath = self + let childpath = &self .path_content .selected() .ok_or_else(|| FmError::custom("go_to_child", "Empty directory"))? - .path - .clone(); - self.history.push(&childpath); + .path; + self.history.push(childpath); self.path_content = PathContent::new(childpath, self.show_hidden)?; self.window.reset(self.path_content.content.len()); self.input.cursor_start(); diff --git a/src/term_manager.rs b/src/term_manager.rs index 6ae7ad5..d712b7b 100644 --- a/src/term_manager.rs +++ b/src/term_manager.rs @@ -107,9 +107,7 @@ impl<'a> WinTab<'a> { /// When a confirmation is needed we ask the user to input `'y'` or /// something else. fn first_line(&self, tab: &Tab, disk_space: &str, canvas: &mut dyn Canvas) -> FmResult<()> { - let first_row = self.create_first_row(tab, disk_space)?; - self.draw_colored_strings(0, 0, first_row, canvas)?; - Ok(()) + self.draw_colored_strings(0, 0, self.create_first_row(tab, disk_space)?, canvas) } fn second_line(&self, status: &Status, tab: &Tab, canvas: &mut dyn Canvas) -> FmResult<()> { @@ -150,7 +148,7 @@ impl<'a> WinTab<'a> { let first_row = match tab.mode { Mode::Normal => { vec![ - format!("{} ", tab.path_content.path_to_str()?), + format!("{} ", tab.path_content.path.display()), format!("{} files ", tab.path_content.content.len()), format!("{} ", tab.path_content.used_space()), format!("Avail: {} ", disk_space), |