use crate::lib::job::ValidModeItemList;
use crate::lib::{job::RawRow, logic::NodeIs};
pub struct TreeLast;
impl TreeLast {
pub const DIR_NO_CHILDREN: &'static str = "└───";
pub const DIR_WITH_CHILDREN: &'static str = "└──┬";
pub const FILE: &'static str = "└──•";
pub const INDENT: &'static str = " ";
}
pub struct TreeMid;
impl TreeMid {
pub const DIR_NO_CHILDREN: &'static str = "├───";
pub const DIR_WITH_CHILDREN: &'static str = "├──┬";
pub const FILE: &'static str = "├──•";
pub const INDENT: &'static str = "│ ";
}
pub struct DrawTree;
impl DrawTree {
pub const ITEM_BETWEEN: &'static str = " ├──•";
pub const ITEM_FIRST: &'static str = " ┌──•";
pub const ITEM_LAST: &'static str = " └──•";
pub const ITEM_ONEFOLD: &'static str = " ──•";
pub fn list(index: usize, total: usize) -> &'static str {
if total <= 1 {
Self::ITEM_ONEFOLD
} else if index == 0 {
Self::ITEM_FIRST
} else if index == total - 1 {
Self::ITEM_LAST
} else {
Self::ITEM_BETWEEN
}
}
}
pub fn draw_list(mode: &ValidModeItemList, index: usize, tab: &[RawRow], _tier_max: usize) -> String {
match mode {
ValidModeItemList::Flat => DrawTree::list(index, tab.len()).to_string(),
ValidModeItemList::Tree => {
let node = &tab[index].node;
let t = node.tier;
let is_dir = node.node == NodeIs::Dir;
let has_children = if is_dir {
if index + 1 < tab.len() { tab[index + 1].node.tier > t } else { false }
} else {
false
};
let mut is_last = vec![true; t + 1];
let mut active_levels = t;
for j in (index + 1)..tab.len() {
let next_tier = tab[j].node.tier;
if next_tier <= active_levels {
is_last[next_tier] = false;
active_levels = next_tier - 1;
}
if active_levels == 0 {
break;
}
}
let mut result = String::new();
for l in 1..t {
if is_last[l] {
result.push_str(TreeLast::INDENT);
} else {
result.push_str(TreeMid::INDENT);
}
}
let am_i_last = is_last[t];
let branch = if is_dir {
match (am_i_last, has_children) {
(true, true) => TreeLast::DIR_WITH_CHILDREN,
(false, true) => TreeMid::DIR_WITH_CHILDREN,
(true, false) => TreeLast::DIR_NO_CHILDREN,
(false, false) => TreeMid::DIR_NO_CHILDREN,
}
} else if am_i_last {
TreeLast::FILE
} else {
TreeMid::FILE
};
result.push_str(branch);
result
}
ValidModeItemList::None => unreachable!("None jest obsługiwane lokalnie przed wywołaniem"),
}
}