gitui/filetreelist/src/treeitems_iter.rs

61 lines
1.1 KiB
Rust
Raw Normal View History

2021-05-21 15:52:05 +03:00
use crate::{filetreeitems::FileTreeItems, item::FileTreeItem};
pub struct TreeItemsIterator<'a> {
tree: &'a FileTreeItems,
index: usize,
increments: Option<usize>,
max_amount: usize,
2021-05-21 15:52:05 +03:00
}
impl<'a> TreeItemsIterator<'a> {
pub const fn new(
tree: &'a FileTreeItems,
start: usize,
max_amount: usize,
) -> Self {
TreeItemsIterator {
max_amount,
increments: None,
index: start,
tree,
}
}
2021-05-21 15:52:05 +03:00
}
impl<'a> Iterator for TreeItemsIterator<'a> {
type Item = (usize, &'a FileTreeItem);
2021-05-21 15:52:05 +03:00
fn next(&mut self) -> Option<Self::Item> {
if self.increments.unwrap_or_default() < self.max_amount {
let items = &self.tree.tree_items;
2021-05-21 15:52:05 +03:00
let mut init = self.increments.is_none();
2021-05-21 15:52:05 +03:00
if let Some(i) = self.increments.as_mut() {
*i += 1;
} else {
self.increments = Some(0);
};
2021-05-21 15:52:05 +03:00
loop {
if !init {
self.index += 1;
}
init = false;
2021-05-21 15:52:05 +03:00
if self.index >= self.tree.len() {
break;
}
2021-05-21 15:52:05 +03:00
let elem = &items[self.index];
2021-05-21 15:52:05 +03:00
if elem.info().is_visible() {
return Some((self.index, &items[self.index]));
}
}
}
2021-05-21 15:52:05 +03:00
None
}
2021-05-21 15:52:05 +03:00
}