-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
867859b
commit 6a9e95b
Showing
8 changed files
with
178 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
.DS_Store | ||
foobar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::{ | ||
file::{tree::Tree, File}, | ||
user::{ | ||
args::{FileType, Layout}, | ||
Context, | ||
}, | ||
}; | ||
use ahash::HashSet; | ||
use indextree::NodeId; | ||
|
||
/// Predicate used for filtering a [`File`] based on [`FileType`]. | ||
pub type FileTypeFilter = dyn Fn(&File) -> bool; | ||
|
||
impl Tree { | ||
/// Updates the [`Tree`]'s inner [`indextree::Arena`] to only contain files of certain | ||
/// file-types. | ||
pub fn filter_file_type( | ||
&mut self, | ||
Context { | ||
layout, file_type, .. | ||
}: &Context, | ||
) { | ||
if file_type.is_empty() { | ||
return; | ||
} | ||
|
||
let mut filters = Vec::<Box<FileTypeFilter>>::new(); | ||
|
||
for ft in HashSet::from_iter(file_type) { | ||
match ft { | ||
FileType::Dir if matches!(layout, Layout::Tree | Layout::InvertedTree) => { | ||
filters.push(Box::new(|f| f.is_dir())) | ||
}, | ||
FileType::Dir => filters.push(Box::new(|f| f.is_dir())), | ||
FileType::File => filters.push(Box::new(|f| f.is_file())), | ||
FileType::Symlink => filters.push(Box::new(|f| f.is_symlink())), | ||
|
||
#[cfg(unix)] | ||
FileType::Pipe => filters.push(Box::new(|f| f.is_fifo())), | ||
#[cfg(unix)] | ||
FileType::Socket => filters.push(Box::new(|f| f.is_socket())), | ||
#[cfg(unix)] | ||
FileType::Char => filters.push(Box::new(|f| f.is_char_device())), | ||
#[cfg(unix)] | ||
FileType::Block => filters.push(Box::new(|f| f.is_block_device())), | ||
} | ||
} | ||
|
||
let no_match = |node_id: &NodeId| !filters.iter().any(|f| f(self.arena[*node_id].get())); | ||
|
||
let to_remove = self | ||
.root_id | ||
.descendants(&self.arena) | ||
.filter(no_match) | ||
.collect::<Vec<_>>(); | ||
|
||
to_remove | ||
.into_iter() | ||
.for_each(|n| n.detach(&mut self.arena)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters