Skip to content

Commit

Permalink
tree: {fold, unfold} cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Feb 11, 2024
1 parent 32528e1 commit 5d91287
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/core/tree/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
}
Expand All @@ -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<T: AsRef<str>>(mut self, cursor: T) -> Self {
self.cursor = cursor.as_ref().to_string();
pub fn folded_cursor<T: AsRef<str>>(mut self, cursor: T) -> Self {
self.folded_cursor = cursor.as_ref().to_string();
self
}

pub fn unfolded_cursor<T: AsRef<str>>(mut self, cursor: T) -> Self {
self.unfolded_cursor = cursor.as_ref().to_string();
self
}

Expand All @@ -48,7 +55,8 @@ impl Builder {
pub fn build(self) -> Result<Renderer> {
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,
Expand Down
14 changes: 7 additions & 7 deletions src/core/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -88,7 +88,7 @@ mod test {
vec![NodeWithDepth {
data: String::from("/"),
depth: 0,
is_leaf: false,
children_visible: false,
}],
create_test_node().flatten()
);
Expand Down Expand Up @@ -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());
Expand Down
17 changes: 13 additions & 4 deletions src/core/tree/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@ use crate::{
grapheme::{trim, Graphemes},
pane::Pane,
render::{AsAny, Renderable},
tree::Tree,
tree::{NodeWithDepth, Tree},
};

#[derive(Clone)]
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<usize>,
}

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()
Expand All @@ -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
),
Expand Down
6 changes: 4 additions & 2 deletions src/preset/theme/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/preset/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5d91287

Please sign in to comment.