diff options
author | Noah Too <krivahtoo@gmail.com> | 2022-06-22 18:03:43 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-22 11:03:43 -0400 |
commit | ba9a5726ab31071c83e83fc808a910fccdbd86f7 (patch) | |
tree | c04165a357b9d355d07be3ad4f1bca8fe9f8e54c /src/fs | |
parent | f22eecdd3b7addb9b20ec4510106c6ee339d215e (diff) |
Add `flat` command (#177)
* add `flat` command
* fix bulk_rename command
* fix `open_file` commands
* update docs
* fix errors found by clippy
* fix errors found by clippy::unnecessary-to-owned
* fix open and open_with commands
* fix: crash on root dirs
Diffstat (limited to 'src/fs')
-rw-r--r-- | src/fs/entry.rs | 66 |
1 files changed, 44 insertions, 22 deletions
diff --git a/src/fs/entry.rs b/src/fs/entry.rs index 4dbaeb4..8337bc4 100644 --- a/src/fs/entry.rs +++ b/src/fs/entry.rs @@ -20,37 +20,30 @@ impl JoshutoDirEntry { pub fn from(direntry: &fs::DirEntry, options: &DisplayOption) -> io::Result<Self> { let path = direntry.path(); - let mut metadata = JoshutoMetadata::from(&path)?; let name = direntry .file_name() .as_os_str() .to_string_lossy() .to_string(); - if options.automatically_count_files() && metadata.file_type().is_dir() { - if let Ok(size) = get_directory_size(path.as_path()) { - metadata.update_directory_size(size); - } - } + Self::gen_entry(path, name, options) + } - #[cfg(feature = "devicons")] - let label = if options.show_icons() { - create_icon_label(name.as_str(), &metadata) - } else { - name.clone() - }; + pub fn from_walk( + direntry: &walkdir::DirEntry, + base: &path::Path, + options: &DisplayOption, + ) -> io::Result<Self> { + let path = direntry.path().to_path_buf(); - #[cfg(not(feature = "devicons"))] - let label = name.clone(); + let name = direntry + .path() + .strip_prefix(base) + .unwrap() + .to_string_lossy() + .to_string(); - Ok(Self { - name, - label, - path, - metadata, - selected: false, - _marked: false, - }) + Self::gen_entry(path, name, options) } pub fn file_name(&self) -> &str { @@ -84,6 +77,35 @@ impl JoshutoDirEntry { None => "", } } + + fn gen_entry(path: path::PathBuf, name: String, options: &DisplayOption) -> io::Result<Self> { + let mut metadata = JoshutoMetadata::from(&path)?; + + if options.automatically_count_files() && metadata.file_type().is_dir() { + if let Ok(size) = get_directory_size(path.as_path()) { + metadata.update_directory_size(size); + } + } + + #[cfg(feature = "devicons")] + let label = if options.show_icons() { + create_icon_label(name.as_str(), &metadata) + } else { + name.clone() + }; + + #[cfg(not(feature = "devicons"))] + let label = name.clone(); + + Ok(Self { + name, + label, + path, + metadata, + selected: false, + _marked: false, + }) + } } impl std::fmt::Display for JoshutoDirEntry { |