-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
2 changed files
with
31 additions
and
25 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,52 +1,58 @@ | ||
mod node; | ||
|
||
pub use node::{Node, NodeWithDepth}; | ||
mod render; | ||
pub use render::Renderer; | ||
mod build; | ||
pub use build::Builder; | ||
|
||
#[derive(Clone, Default)] | ||
use crate::core::cursor::Cursor; | ||
|
||
#[derive(Clone)] | ||
pub struct Tree { | ||
root: Node, | ||
cache: Vec<NodeWithDepth>, | ||
position: usize, | ||
cursor: Cursor<Vec<NodeWithDepth>>, | ||
} | ||
|
||
impl Tree { | ||
pub fn new(root: Node) -> Self { | ||
Self { | ||
root: root.clone(), | ||
cache: root.flatten(), | ||
position: 0, | ||
cursor: Cursor::new(root.flatten()), | ||
} | ||
} | ||
|
||
pub fn nodes_with_depth(&self) -> Vec<NodeWithDepth> { | ||
self.cache.clone() | ||
pub fn nodes(&self) -> Vec<NodeWithDepth> { | ||
self.cursor.contents().clone() | ||
} | ||
|
||
pub fn position(&self) -> usize { | ||
self.cursor.position() | ||
} | ||
|
||
pub fn get(&self) -> String { | ||
self.cache.get(self.position).unwrap().data.clone() | ||
self.cursor | ||
.contents() | ||
.get(self.position()) | ||
.unwrap() | ||
.data | ||
.clone() | ||
} | ||
|
||
pub fn toggle(&mut self) { | ||
self.root.toggle(self.cursor.position()); | ||
self.cursor = Cursor::new_with_position(self.root.flatten(), self.position()); | ||
} | ||
|
||
pub fn backward(&mut self) -> bool { | ||
if 0 < self.position { | ||
self.position -= 1; | ||
return true; | ||
} | ||
false | ||
self.cursor.backward() | ||
} | ||
|
||
pub fn forward(&mut self) -> bool { | ||
if !self.cache.is_empty() && self.position < self.cache.len() - 1 { | ||
self.position += 1; | ||
return true; | ||
} | ||
false | ||
self.cursor.forward() | ||
} | ||
|
||
pub fn toggle(&mut self) { | ||
self.root.toggle(self.position); | ||
self.cache = self.root.flatten(); | ||
pub fn move_to_head(&mut self) { | ||
self.cursor.move_to_head() | ||
} | ||
} |
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