From 5d9128764d02fd2aac2b3887c837b248638a4ff1 Mon Sep 17 00:00:00 2001 From: ynqa Date: Sun, 11 Feb 2024 11:11:43 +0900 Subject: [PATCH] tree: {fold, unfold} cursor --- src/core/tree/build.rs | 18 +++++++++++++----- src/core/tree/node.rs | 14 +++++++------- src/core/tree/render.rs | 17 +++++++++++++---- src/preset/theme/tree.rs | 6 ++++-- src/preset/tree.rs | 3 ++- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/core/tree/build.rs b/src/core/tree/build.rs index 85fa9ad6..5fb9ff8b 100644 --- a/src/core/tree/build.rs +++ b/src/core/tree/build.rs @@ -9,7 +9,8 @@ use crate::{ pub struct Builder { tree: Tree, style: ContentStyle, - cursor: String, + folded_cursor: String, + unfolded_cursor: String, cursor_style: ContentStyle, lines: Option, } @@ -19,14 +20,20 @@ impl Builder { Self { tree: Tree::new(root), style: Default::default(), - cursor: Default::default(), + folded_cursor: Default::default(), + unfolded_cursor: Default::default(), cursor_style: Default::default(), lines: Default::default(), } } - pub fn cursor>(mut self, cursor: T) -> Self { - self.cursor = cursor.as_ref().to_string(); + pub fn folded_cursor>(mut self, cursor: T) -> Self { + self.folded_cursor = cursor.as_ref().to_string(); + self + } + + pub fn unfolded_cursor>(mut self, cursor: T) -> Self { + self.unfolded_cursor = cursor.as_ref().to_string(); self } @@ -48,7 +55,8 @@ impl Builder { pub fn build(self) -> Result { Ok(Renderer { tree: self.tree, - cursor: self.cursor, + folded_cursor: self.folded_cursor, + unfolded_cursor: self.unfolded_cursor, style: self.style, cursor_style: self.cursor_style, lines: self.lines, diff --git a/src/core/tree/node.rs b/src/core/tree/node.rs index 3ff37a74..ebde87b2 100644 --- a/src/core/tree/node.rs +++ b/src/core/tree/node.rs @@ -9,7 +9,7 @@ pub struct Node { pub struct NodeWithDepth { pub data: String, pub depth: usize, - pub is_leaf: bool, + pub children_visible: bool, } impl Node { @@ -37,7 +37,7 @@ impl Node { res.push(NodeWithDepth { data: self.data.clone(), depth, - is_leaf: self.children.is_empty(), + children_visible: self.children_visible, }); if self.children_visible && !self.children.is_empty() { @@ -88,7 +88,7 @@ mod test { vec![NodeWithDepth { data: String::from("/"), depth: 0, - is_leaf: false, + children_visible: false, }], create_test_node().flatten() ); @@ -120,22 +120,22 @@ mod test { NodeWithDepth { data: String::from("/"), depth: 0, - is_leaf: false, + children_visible: true, }, NodeWithDepth { data: String::from("a"), depth: 1, - is_leaf: false, + children_visible: false, }, NodeWithDepth { data: String::from("b"), depth: 1, - is_leaf: true, + children_visible: false, }, NodeWithDepth { data: String::from("c"), depth: 1, - is_leaf: true, + children_visible: false, }, ]; assert_eq!(expect, root.flatten()); diff --git a/src/core/tree/render.rs b/src/core/tree/render.rs index ed657632..95caa800 100644 --- a/src/core/tree/render.rs +++ b/src/core/tree/render.rs @@ -8,7 +8,7 @@ use crate::{ grapheme::{trim, Graphemes}, pane::Pane, render::{AsAny, Renderable}, - tree::Tree, + tree::{NodeWithDepth, Tree}, }; #[derive(Clone)] @@ -16,13 +16,22 @@ pub struct Renderer { pub tree: Tree, pub style: ContentStyle, - pub cursor: String, + pub folded_cursor: String, + pub unfolded_cursor: String, pub cursor_style: ContentStyle, pub lines: Option, } impl Renderable for Renderer { fn make_pane(&self, width: u16) -> crate::pane::Pane { + let cursor = |item: &NodeWithDepth| -> &str { + if item.children_visible { + &self.unfolded_cursor + } else { + &self.folded_cursor + } + }; + let matrix = self .tree .nodes() @@ -31,14 +40,14 @@ impl Renderable for Renderer { .map(|(i, item)| { if i == self.tree.position() { Graphemes::new_with_style( - format!("{}{}{}", self.cursor, " ".repeat(item.depth), item.data), + format!("{}{}{}", cursor(item), " ".repeat(item.depth), item.data), self.cursor_style, ) } else { Graphemes::new_with_style( format!( "{}{}{}", - " ".repeat(Graphemes::new(self.cursor.clone()).widths()), + " ".repeat(Graphemes::new(cursor(item)).widths()), " ".repeat(item.depth), item.data ), diff --git a/src/preset/theme/tree.rs b/src/preset/theme/tree.rs index 4d2a57a0..aa4401cc 100644 --- a/src/preset/theme/tree.rs +++ b/src/preset/theme/tree.rs @@ -6,7 +6,8 @@ use crate::{ pub struct Theme { pub title_style: ContentStyle, pub item_style: ContentStyle, - pub cursor: String, + pub folded_cursor: String, + pub unfolded_cursor: String, pub cursor_style: ContentStyle, } @@ -17,7 +18,8 @@ impl Default for Theme { .attrs(Attributes::from(Attribute::Bold)) .build(), item_style: Style::new().build(), - cursor: String::from("❯ "), + folded_cursor: String::from("▶︎ "), + unfolded_cursor: String::from("▼ "), cursor_style: Style::new().fgc(Color::DarkCyan).build(), } } diff --git a/src/preset/tree.rs b/src/preset/tree.rs index 8bd24ae2..2b850e55 100644 --- a/src/preset/tree.rs +++ b/src/preset/tree.rs @@ -25,7 +25,8 @@ impl Tree { self.title_builder = self.title_builder.style(theme.title_style); self.tree_builder = self .tree_builder - .cursor(theme.cursor) + .folded_cursor(theme.folded_cursor) + .unfolded_cursor(theme.unfolded_cursor) .style(theme.item_style) .cursor_style(theme.cursor_style); self