use crate::cursor::Cursor;
use super::node::{Kind, Node};
#[derive(Clone)]
pub struct Tree {
root: Node,
cursor: Cursor<Vec<Kind>>,
}
impl Tree {
pub fn new(root: Node) -> Self {
Self {
root: root.clone(),
cursor: Cursor::new(root.flatten_visibles(), 0, false),
}
}
pub fn kinds(&self) -> Vec<Kind> {
self.cursor.contents().clone()
}
pub fn position(&self) -> usize {
self.cursor.position()
}
pub fn get(&self) -> Vec<String> {
let kind = self.cursor.contents()[self.position()].clone();
match kind {
Kind::Folded { id, path } | Kind::Unfolded { id, path } => {
let mut ret = self.root.get_waypoints(&path);
ret.push(id.to_string());
ret
}
}
}
pub fn toggle(&mut self) {
let path = match self.cursor.contents()[self.position()].clone() {
Kind::Folded { path, .. } => path,
Kind::Unfolded { path, .. } => path,
};
self.root.toggle(&path);
self.cursor = Cursor::new(self.root.flatten_visibles(), self.position(), false);
}
pub fn backward(&mut self) -> bool {
self.cursor.backward()
}
pub fn forward(&mut self) -> bool {
self.cursor.forward()
}
pub fn move_to_head(&mut self) {
self.cursor.move_to_head()
}
pub fn move_to_tail(&mut self) {
self.cursor.move_to_tail()
}
}