use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
pub struct TreeSymbols {
pub continued: &'static str,
pub join_first: &'static str,
pub join_last: &'static str,
pub join_inner: &'static str,
pub join_only: &'static str,
pub branch: &'static str,
pub leaf: &'static str,
pub multiline_first: Option<&'static str>,
pub multiline_continued: Option<&'static str>,
}
#[derive(Debug, Clone)]
pub struct TreeConfig {
pub symbols: TreeSymbols,
pub indent: usize,
pub show_first_level: bool,
}
impl TreeSymbols {
pub fn new() -> Self {
Self {
continued: "│",
join_first: "├",
join_inner: "├",
join_last: "└",
join_only: "└",
branch: "─",
leaf: "╼ ",
multiline_first: None,
multiline_continued: None,
}
}
pub fn with_pipes() -> Self {
Self {
continued: "║",
join_first: "╠",
join_inner: "╠",
join_last: "╚",
join_only: "╚",
branch: "═",
leaf: "╼ ",
multiline_first: None,
multiline_continued: None,
}
}
pub fn with_thick() -> Self {
Self {
continued: "┃",
join_first: "┣",
join_inner: "┣",
join_last: "┗",
join_only: "┗",
branch: "━",
leaf: "╼ ",
multiline_first: None,
multiline_continued: None,
}
}
pub fn with_rounded() -> Self {
Self {
continued: "│",
join_first: "├",
join_inner: "├",
join_last: "╰",
join_only: "╰",
branch: "─",
leaf: "╼ ",
multiline_first: None,
multiline_continued: None,
}
}
pub fn with_dashed() -> Self {
Self {
continued: "┊",
join_first: "┊",
join_inner: "┊",
join_last: "'",
join_only: "'",
branch: "╌",
leaf: "- ",
multiline_first: None,
multiline_continued: None,
}
}
pub fn continued(mut self, sym: &'static str) -> Self {
self.continued = sym;
self
}
pub fn join_first(mut self, sym: &'static str) -> Self {
self.join_first = sym;
self
}
pub fn join_inner(mut self, sym: &'static str) -> Self {
self.join_inner = sym;
self
}
pub fn join_last(mut self, sym: &'static str) -> Self {
self.join_last = sym;
self
}
pub fn join_only(mut self, sym: &'static str) -> Self {
self.join_only = sym;
self
}
pub fn branch(mut self, sym: &'static str) -> Self {
self.branch = sym;
self
}
pub fn leaf(mut self, sym: &'static str) -> Self {
self.leaf = sym;
self
}
pub fn multiline_first(mut self, sym: &'static str) -> Self {
self.multiline_first = Some(sym);
self
}
pub fn multiline_continued(mut self, sym: &'static str) -> Self {
self.multiline_continued = Some(sym);
self
}
}
impl TreeConfig {
pub fn new() -> Self {
Self {
symbols: TreeSymbols::new(),
indent: 2,
show_first_level: false,
}
}
pub fn with_symbols(symbols: TreeSymbols) -> Self {
Self {
symbols,
indent: 2,
show_first_level: false,
}
}
pub fn indent(mut self, x: usize) -> Self {
self.indent = x;
self
}
pub fn show_first_level(mut self) -> Self {
self.show_first_level = true;
self
}
pub fn hide_first_level(mut self) -> Self {
self.show_first_level = false;
self
}
pub fn symbols(mut self, x: TreeSymbols) -> Self {
self.symbols = x;
self
}
}
impl Default for TreeSymbols {
fn default() -> Self {
tree_config_symbols()
}
}
impl Default for TreeConfig {
fn default() -> Self {
tree_config()
}
}
static DEFAULT_CONFIG: Lazy<Arc<Mutex<TreeConfig>>> =
Lazy::new(|| -> Arc<Mutex<TreeConfig>> { Arc::new(Mutex::new(TreeConfig::new())) });
pub fn set_tree_config(x: TreeConfig) {
*DEFAULT_CONFIG.lock().unwrap() = x;
}
pub fn tree_config() -> TreeConfig {
DEFAULT_CONFIG.lock().unwrap().clone()
}
pub fn set_tree_config_symbols(x: TreeSymbols) {
DEFAULT_CONFIG.lock().unwrap().symbols = x;
}
pub fn tree_config_symbols() -> TreeSymbols {
DEFAULT_CONFIG.lock().unwrap().symbols.clone()
}
pub fn update_tree_config<F: FnMut(&mut TreeConfig)>(mut update: F) {
let mut x = DEFAULT_CONFIG.lock().unwrap();
update(&mut x);
}
pub fn update_tree_config_symbols<F: FnMut(&mut TreeSymbols)>(mut update: F) {
let mut x = DEFAULT_CONFIG.lock().unwrap();
update(&mut x.symbols);
}