Module kubectl_view_allocations::tree[][src]

Expand description

module to make tree-table or tree from a list, by computing the string prefix that contains line of to link item of the list like a tree (multi-root)

use kubectl_view_allocations::tree::provide_prefix;
 
let items = vec![
    "1/2",
    "1/2/3",
    "1/2/3/4",
    "1/2/5",
    "6",
    "7",
    "7/8",
    "7/9",
];

let prefixes = provide_prefix(&items, |parent, item| {
    let pi = item.split("/");
    let pp = parent.split("/");
    (pi.count() == pp.count() + 1) && item.starts_with(parent)
});

let mut actual = String::new();
prefixes.iter().zip(items).for_each(|(p, i)|
    actual.push_str(&format!("{} {}\n", p, i))
);

let expected = r#" 1/2
 ├─ 1/2/3
 │  └─ 1/2/3/4
 └─ 1/2/5
 6
 7
 ├─ 7/8
 └─ 7/9
"#;
//dbg!(&actual);
assert_eq!(actual, expected);

Functions

provide_prefix

Generate a list of prefix to display items as a tree (multi-root)