feat: configure node open state symbols (#19)

This commit is contained in:
EdJoPaTo
2023-05-20 01:30:53 +02:00
committed by GitHub
parent 61bc6da821
commit 3e92570a16
+54 -22
View File
@@ -262,15 +262,24 @@ impl<'a> TreeItem<'a> {
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Tree<'a> { pub struct Tree<'a> {
block: Option<Block<'a>>,
items: Vec<TreeItem<'a>>, items: Vec<TreeItem<'a>>,
block: Option<Block<'a>>,
start_corner: Corner,
/// Style used as a base style for the widget /// Style used as a base style for the widget
style: Style, style: Style,
start_corner: Corner,
/// Style used to render selected item /// Style used to render selected item
highlight_style: Style, highlight_style: Style,
/// Symbol in front of the selected item (Shift all items to the right) /// Symbol in front of the selected item (Shift all items to the right)
highlight_symbol: Option<&'a str>, highlight_symbol: Option<&'a str>,
/// Symbol displayed in front of a closed node (As in the children are currently not visible)
node_closed_symbol: &'a str,
/// Symbol displayed in front of an open node. (As in the children are currently visible)
node_open_symbol: &'a str,
/// Symbol displayed in front of a node without children.
node_no_children_symbol: &'a str,
} }
impl<'a> Tree<'a> { impl<'a> Tree<'a> {
@@ -279,12 +288,15 @@ impl<'a> Tree<'a> {
T: Into<Vec<TreeItem<'a>>>, T: Into<Vec<TreeItem<'a>>>,
{ {
Self { Self {
block: None,
style: Style::default(),
items: items.into(), items: items.into(),
block: None,
start_corner: Corner::TopLeft, start_corner: Corner::TopLeft,
style: Style::default(),
highlight_style: Style::default(), highlight_style: Style::default(),
highlight_symbol: None, highlight_symbol: None,
node_closed_symbol: "\u{25b6} ", // Arrow to right
node_open_symbol: "\u{25bc} ", // Arrow down
node_no_children_symbol: " ",
} }
} }
@@ -296,14 +308,14 @@ impl<'a> Tree<'a> {
} }
#[must_use] #[must_use]
pub const fn style(mut self, style: Style) -> Self { pub const fn start_corner(mut self, corner: Corner) -> Self {
self.style = style; self.start_corner = corner;
self self
} }
#[must_use] #[must_use]
pub const fn highlight_symbol(mut self, highlight_symbol: &'a str) -> Self { pub const fn style(mut self, style: Style) -> Self {
self.highlight_symbol = Some(highlight_symbol); self.style = style;
self self
} }
@@ -314,8 +326,26 @@ impl<'a> Tree<'a> {
} }
#[must_use] #[must_use]
pub const fn start_corner(mut self, corner: Corner) -> Self { pub const fn highlight_symbol(mut self, highlight_symbol: &'a str) -> Self {
self.start_corner = corner; self.highlight_symbol = Some(highlight_symbol);
self
}
#[must_use]
pub const fn node_closed_symbol(mut self, symbol: &'a str) -> Self {
self.node_closed_symbol = symbol;
self
}
#[must_use]
pub const fn node_open_symbol(mut self, symbol: &'a str) -> Self {
self.node_open_symbol = symbol;
self
}
#[must_use]
pub const fn node_no_children_symbol(mut self, symbol: &'a str) -> Self {
self.node_no_children_symbol = symbol;
self self
} }
} }
@@ -419,22 +449,24 @@ impl<'a> StatefulWidget for Tree<'a> {
}; };
let after_depth_x = { let after_depth_x = {
let symbol = if item.item.children.is_empty() { let indent_width = item.depth() * 2;
" " let (after_indent_x, _) = buf.set_stringn(
} else if state.opened.contains(&item.identifier) {
"\u{25bc}" // Arrow down
} else {
"\u{25b6}" // Arrow to right
};
let string = format!("{:>width$}{} ", "", symbol, width = item.depth() * 2);
let max_width = area.width.saturating_sub(after_highlight_symbol_x - x);
let (x, _) = buf.set_stringn(
after_highlight_symbol_x, after_highlight_symbol_x,
y, y,
string, " ".repeat(indent_width),
max_width as usize, indent_width,
item_style, item_style,
); );
let symbol = if item.item.children.is_empty() {
self.node_no_children_symbol
} else if state.opened.contains(&item.identifier) {
self.node_open_symbol
} else {
self.node_closed_symbol
};
let max_width = area.width.saturating_sub(after_indent_x - x);
let (x, _) =
buf.set_stringn(after_indent_x, y, symbol, max_width as usize, item_style);
x x
}; };