feat: generic identifier (#27)

This commit is contained in:
EdJoPaTo
2023-10-30 13:18:58 +01:00
committed by GitHub
parent 58a80b64a7
commit b914819283
5 changed files with 253 additions and 120 deletions
+17 -6
View File
@@ -25,16 +25,26 @@ impl<'a> App<'a> {
fn new() -> Self {
Self {
tree: StatefulTree::with_items(vec![
TreeItem::new_leaf("a"),
TreeItem::new_leaf("a", "Alfa"),
TreeItem::new(
"b",
"Bravo",
vec![
TreeItem::new_leaf("c"),
TreeItem::new("d", vec![TreeItem::new_leaf("e"), TreeItem::new_leaf("f")]),
TreeItem::new_leaf("g"),
TreeItem::new_leaf("c", "Charlie"),
TreeItem::new(
"d",
"Delta",
vec![
TreeItem::new_leaf("e", "Echo"),
TreeItem::new_leaf("f", "Foxtrot"),
],
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("g", "Golf"),
],
),
TreeItem::new_leaf("h"),
)
.expect("all item identifiers are unique"),
TreeItem::new_leaf("h", "Hotel"),
]),
}
}
@@ -74,6 +84,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
let area = f.size();
let items = Tree::new(app.tree.items.clone())
.expect("all item identifiers are unique")
.block(
Block::new()
.borders(Borders::ALL)
+4 -4
View File
@@ -1,8 +1,8 @@
use tui_tree_widget::{TreeItem, TreeState};
pub struct StatefulTree<'a> {
pub state: TreeState,
pub items: Vec<TreeItem<'a>>,
pub state: TreeState<&'static str>,
pub items: Vec<TreeItem<'a, &'static str>>,
}
impl<'a> StatefulTree<'a> {
@@ -14,7 +14,7 @@ impl<'a> StatefulTree<'a> {
}
}
pub fn with_items(items: Vec<TreeItem<'a>>) -> Self {
pub fn with_items(items: Vec<TreeItem<'a, &'static str>>) -> Self {
Self {
state: TreeState::default(),
items,
@@ -22,7 +22,7 @@ impl<'a> StatefulTree<'a> {
}
pub fn first(&mut self) {
self.state.select_first();
self.state.select_first(&self.items);
}
pub fn last(&mut self) {