Skip to content

Commit

Permalink
fix: test failure for exclude flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteBaker committed Sep 8, 2024
1 parent 1117d65 commit 40bdadc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 56 deletions.
44 changes: 2 additions & 42 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,19 @@
use std::collections::VecDeque;

use crate::pathiterator::{FileIterator, IteratorItem};

pub struct FilteredIterator {
pub source: FileIterator,
cache: VecDeque<IteratorItem>,
skip: bool,
next_item: Option<IteratorItem>,
}

impl FilteredIterator {
pub fn new(iterator: FileIterator) -> Self {
FilteredIterator {
source: iterator,
cache: VecDeque::new(),
skip: false,
next_item: None,
}
}

pub fn skip_filter(&mut self) {
self.skip = true;
FilteredIterator { source: iterator }
}
}

impl Iterator for FilteredIterator {
type Item = IteratorItem;

fn next(&mut self) -> Option<Self::Item> {
if self.skip {
return self.source.next();
}

if let Some(cache_item) = self.cache.pop_front() {
return Some(cache_item);
}

if let Some(next_item) = self.next_item.take() {
return Some(next_item);
}

for item in self.source.by_ref() {
if item.is_dir() {
self.cache.push_back(item);
} else {
// If the cache already contains a folder,
// start emptying cache, and save the item.
if let Some(cache_front) = self.cache.pop_front() {
self.next_item = Some(item);
return Some(cache_front);
}
return Some(item);
}
}

None
self.source.next()
}
}
9 changes: 3 additions & 6 deletions src/pathiterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,12 @@ impl Iterator for FileIterator {
type Item = IteratorItem;

fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = self.queue.pop_back() {
self.queue.pop_back().map(|item| {
if item.is_dir() && item.level < self.config.max_level {
// println!("{:?}", item.file_name);
self.push_dir(&item);
}

Some(item)
} else {
None
}
item
})
}
}
2 changes: 1 addition & 1 deletion src/tests/test_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn test_filter_txt_files() {
..Default::default()
},
);

assert_eq!(6, summary.num_folders);
assert_eq!(1, summary.num_files);

Expand Down Expand Up @@ -109,7 +110,6 @@ fn test_exclude_txt_files() {

assert_eq!(6, summary.num_folders);
assert_eq!(3, summary.num_files);

assert_eq!(expected, output);
}

Expand Down
9 changes: 2 additions & 7 deletions src/tree_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,8 @@ impl<'a, T: Terminal<Output = W>, W: std::io::Write> TreePrinter<'a, T, W> {
show_only_dirs: self.config.show_only_dirs,
};

let list = pathiterator::FileIterator::new(path, config);
let mut list = filter::FilteredIterator::new(list);
if self.config.include_globs.is_empty() && self.config.exlude_globs.is_empty() {
list.skip_filter();
}

list
let iterator = pathiterator::FileIterator::new(path, config);
filter::FilteredIterator::new(iterator)
}

/// # Errors
Expand Down

0 comments on commit 40bdadc

Please sign in to comment.