refactor(example): merge into single file

This commit is contained in:
EdJoPaTo
2024-02-03 02:15:36 +01:00
parent 4cbe932332
commit 14b9e42cb2
2 changed files with 20 additions and 68 deletions
+20 -17
View File
@@ -1,6 +1,3 @@
mod util;
use crate::util::StatefulTree;
use crossterm::{ use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute, execute,
@@ -15,16 +12,18 @@ use ratatui::{
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use tui_tree_widget::{Tree, TreeItem}; use tui_tree_widget::{Tree, TreeItem, TreeState};
struct App<'a> { struct App<'a> {
tree: StatefulTree<'a>, state: TreeState<&'static str>,
items: Vec<TreeItem<'a, &'static str>>,
} }
impl<'a> App<'a> { impl<'a> App<'a> {
fn new() -> Self { fn new() -> Self {
Self { Self {
tree: StatefulTree::with_items(vec![ state: TreeState::default(),
items: vec![
TreeItem::new_leaf("a", "Alfa"), TreeItem::new_leaf("a", "Alfa"),
TreeItem::new( TreeItem::new(
"b", "b",
@@ -45,7 +44,7 @@ impl<'a> App<'a> {
) )
.expect("all item identifiers are unique"), .expect("all item identifiers are unique"),
TreeItem::new_leaf("h", "Hotel"), TreeItem::new_leaf("h", "Hotel"),
]), ],
} }
} }
} }
@@ -83,9 +82,9 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
terminal.draw(|f| { terminal.draw(|f| {
let area = f.size(); let area = f.size();
let items = Tree::new(app.tree.items.clone()) let items = Tree::new(app.items.clone())
.expect("all item identifiers are unique") .expect("all item identifiers are unique")
.block(Block::bordered().title(format!("Tree Widget {:?}", app.tree.state))) .block(Block::bordered().title(format!("Tree Widget {:?}", app.state)))
.highlight_style( .highlight_style(
Style::new() Style::new()
.fg(Color::Black) .fg(Color::Black)
@@ -93,19 +92,23 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
.add_modifier(Modifier::BOLD), .add_modifier(Modifier::BOLD),
) )
.highlight_symbol(">> "); .highlight_symbol(">> ");
f.render_stateful_widget(items, area, &mut app.tree.state); f.render_stateful_widget(items, area, &mut app.state);
})?; })?;
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
match key.code { match key.code {
KeyCode::Char('q') => return Ok(()), KeyCode::Char('q') => return Ok(()),
KeyCode::Char('\n' | ' ') => app.tree.toggle(), KeyCode::Char('\n' | ' ') => app.state.toggle_selected(),
KeyCode::Left => app.tree.left(), KeyCode::Left => app.state.key_left(),
KeyCode::Right => app.tree.right(), KeyCode::Right => app.state.key_right(),
KeyCode::Down => app.tree.down(), KeyCode::Down => app.state.key_down(&app.items),
KeyCode::Up => app.tree.up(), KeyCode::Up => app.state.key_up(&app.items),
KeyCode::Home => app.tree.first(), KeyCode::Home => {
KeyCode::End => app.tree.last(), app.state.select_first(&app.items);
}
KeyCode::End => {
app.state.select_last(&app.items);
}
_ => {} _ => {}
} }
} }
-51
View File
@@ -1,51 +0,0 @@
use tui_tree_widget::{TreeItem, TreeState};
pub struct StatefulTree<'a> {
pub state: TreeState<&'static str>,
pub items: Vec<TreeItem<'a, &'static str>>,
}
impl<'a> StatefulTree<'a> {
#[allow(dead_code)]
pub fn new() -> Self {
Self {
state: TreeState::default(),
items: Vec::new(),
}
}
pub fn with_items(items: Vec<TreeItem<'a, &'static str>>) -> Self {
Self {
state: TreeState::default(),
items,
}
}
pub fn first(&mut self) {
self.state.select_first(&self.items);
}
pub fn last(&mut self) {
self.state.select_last(&self.items);
}
pub fn down(&mut self) {
self.state.key_down(&self.items);
}
pub fn up(&mut self) {
self.state.key_up(&self.items);
}
pub fn left(&mut self) {
self.state.key_left();
}
pub fn right(&mut self) {
self.state.key_right();
}
pub fn toggle(&mut self) {
self.state.toggle_selected();
}
}