cursive-tree 0.0.9

Tree view for the Cursive TUI library
Documentation
use {
    cursive::{view::*, views::*, *},
    cursive_tree::*,
};

// In this example we'll be using SimpleTreeBackend and manually adding nodes

fn main() -> Result<(), ()> {
    let mut cursive = default();

    let mut tree = SimpleTreeBackend::tree_view();

    // A few roots

    tree.model.add_root(NodeKind::Leaf, (), "Hello");
    tree.model.add_root(NodeKind::Branch, (), "World");

    // Add 10 children to "World"

    let world = tree.model.at_path_mut([1].into()).expect("at_path_mut");
    for i in 0..10 {
        world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1));
    }

    // Add 10 grandchildren each to "Child #5" -> "Child #10"
    // Note that we change them to branch in order to support having children

    let mut gc = 0;
    for c in 4..10 {
        let child = tree.model.at_path_mut([1, c].into()).expect("at_path_mut");
        child.kind = NodeKind::Branch;
        for _ in 0..10 {
            child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1));
            gc += 1;
        }
    }

    // Expand all nodes

    tree.model.expand(None)?;

    cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
    cursive.add_global_callback('q', |cursive| cursive.quit());

    cursive.run();

    Ok(())
}