pub mod tree_chars {
pub const BRANCH: &str = "├─";
pub const LAST: &str = "└─";
pub const PIPE: &str = "│ ";
pub const SPACE: &str = " ";
}
#[derive(Clone, Debug, Default)]
pub struct TreePrefix {
depth_flags: Vec<bool>,
}
impl TreePrefix {
pub fn new() -> Self {
Self {
depth_flags: Vec::new(),
}
}
pub fn push(&mut self, has_more: bool) {
self.depth_flags.push(has_more);
}
pub fn pop(&mut self) {
self.depth_flags.pop();
}
pub fn prefix(&self, is_last: bool) -> String {
let mut result = String::new();
for &has_more in &self.depth_flags {
result.push_str(if has_more {
tree_chars::PIPE
} else {
tree_chars::SPACE
});
}
result.push_str(if is_last {
tree_chars::LAST
} else {
tree_chars::BRANCH
});
result
}
pub fn continuation(&self) -> String {
let mut result = String::new();
for &has_more in &self.depth_flags {
result.push_str(if has_more {
tree_chars::PIPE
} else {
tree_chars::SPACE
});
}
result.push_str(tree_chars::SPACE);
result
}
pub fn depth(&self) -> usize {
self.depth_flags.len()
}
pub fn clear(&mut self) {
self.depth_flags.clear();
}
}