diff options
-rw-r--r-- | src/fileinfo.rs | 11 | ||||
-rw-r--r-- | src/tree.rs | 44 |
2 files changed, 28 insertions, 27 deletions
diff --git a/src/fileinfo.rs b/src/fileinfo.rs index 335ae9e..6484491 100644 --- a/src/fileinfo.rs +++ b/src/fileinfo.rs @@ -359,7 +359,7 @@ impl PathContent { let fileinfo = FileInfo::from_path_with_name(path, filename_from_path(path)?, users)?; if let Some(true_files) = - files_collection(&fileinfo, users, show_hidden, filter_kind, false) + files_collection(&fileinfo.path, users, show_hidden, filter_kind, false) { files.extend(true_files); } @@ -697,13 +697,13 @@ fn filekind_and_filename(filename: &str, file_kind: &FileKind<Valid>) -> String /// Files are filtered by filterkind and the display hidden flag. /// Returns None if there's no file. pub fn files_collection( - fileinfo: &FileInfo, + path: &path::Path, users: &Users, show_hidden: bool, filter_kind: &FilterKind, keep_dir: bool, ) -> Option<Vec<FileInfo>> { - match read_dir(&fileinfo.path) { + match read_dir(&path) { Ok(read_dir) => Some( read_dir .filter_map(|direntry| direntry.ok()) @@ -714,10 +714,7 @@ pub fn files_collection( .collect(), ), Err(error) => { - info!( - "Couldn't read path {path} - {error}", - path = fileinfo.path.display(), - ); + info!("Couldn't read path {path} - {error}", path = path.display(),); None } } diff --git a/src/tree.rs b/src/tree.rs index 253e102..34e9782 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -120,31 +120,23 @@ impl Tree { } let children_will_be_added = depth + start_depth - reached_depth > 1; let mut node = Node::new(&path, None); - if let Ok(fileinfo) = node.fileinfo(users) { - if path.is_dir() && !path.is_symlink() && children_will_be_added { - if let Some(mut files) = - files_collection(&fileinfo, users, show_hidden, filter_kind, true) - { - sort_kind.sort(&mut files); - let children = files - .iter() - .map(|fileinfo| { - stack.push(fileinfo.path.to_owned()); - fileinfo - }) - .map(|fileinfo| fileinfo.path.to_owned()) - .collect(); - node.set_children(Some(children)); - }; - } + if path.is_dir() && !path.is_symlink() && children_will_be_added { + if let Some(mut files) = + files_collection(&path, users, show_hidden, filter_kind, true) + { + sort_kind.sort(&mut files); + let children = Self::make_children_and_stack_them(&mut stack, &files); + node.set_children(Some(children)); + }; } last_path = node.path.to_owned(); nodes.insert(node.path.to_owned(), node); } - if let Some(node) = nodes.get_mut(&root_path) { - node.select() - } + let Some(node) = nodes.get_mut(&root_path) else { + unreachable!("root path should be in nodes"); + }; + node.select(); Self { selected: root_path.clone(), @@ -155,6 +147,17 @@ impl Tree { } } + fn make_children_and_stack_them(stack: &mut Vec<PathBuf>, files: &[FileInfo]) -> Vec<PathBuf> { + files + .iter() + .map(|fileinfo| fileinfo.path.to_owned()) + .map(|path| { + stack.push(path.to_owned()); + path + }) + .collect() + } + pub fn empty() -> Self { Self { root_path: PathBuf::default(), @@ -237,6 +240,7 @@ impl Tree { } let mut current_path = self.selected.to_owned(); + // TODO: refactor using ancestors. Not so easy since we keep track of parent and current while let Some(parent_path) = current_path.parent() { let Some(parent_node) = self.nodes.get(parent_path) else { current_path = parent_path.to_owned(); |