feat: Updated the Ratatui Tree Widget to allow the tree to story any data type that implements Into<Text>, so that users can also easily fetch data from within the tree based on what's selected for non-text data types.

This commit is contained in:
2024-11-14 15:38:37 -07:00
parent 735f111866
commit 3e23a73f6b
8 changed files with 78 additions and 84 deletions
+7 -20
View File
@@ -7,7 +7,7 @@ use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Span;
use ratatui::widgets::{Block, Scrollbar, ScrollbarOrientation};
use ratatui::{crossterm, Frame, Terminal};
use tui_tree_widget::{Tree, TreeItem, TreeState};
use managarr_tree_widget::{Tree, TreeItem, TreeState};
#[must_use]
struct App {
@@ -82,24 +82,6 @@ impl App {
fn draw(&mut self, frame: &mut Frame) {
let area = frame.area();
let selected = self.state.selected();
let flatten = self.state.flatten(&self.items);
let current_selection = flatten
.iter()
.find(|i| self.state.selected() == i.identifier);
let is_selected = current_selection.is_some()
&& current_selection.unwrap().item.content().to_string() == *"Echo";
let style = if is_selected {
Style::new()
.fg(Color::Black)
.bg(Color::Cyan)
.add_modifier(Modifier::BOLD)
} else {
Style::new()
.fg(Color::Black)
.bg(Color::LightGreen)
.add_modifier(Modifier::BOLD)
};
let widget = Tree::new(&self.items)
.expect("all item identifiers are unique")
.block(
@@ -113,7 +95,12 @@ impl App {
.track_symbol(None)
.end_symbol(None),
))
.highlight_style(style)
.highlight_style(
Style::new()
.fg(Color::Black)
.bg(Color::LightGreen)
.add_modifier(Modifier::BOLD),
)
.highlight_symbol(">> ");
frame.render_stateful_widget(widget, area, &mut self.state);
}