summaryrefslogtreecommitdiffstats
path: root/src/interactive/widgets.rs
blob: c1cf0b9c7b66bbe41add1b124ffe5021b6dad0d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use super::{Traversal, Tree, TreeIndex};
use petgraph::Direction;
use tui::{
    buffer::Buffer,
    layout::{Corner, Rect},
    widgets::{Block, Borders, List, Text, Widget},
};

pub struct Entries<'a> {
    pub tree: &'a Tree,
    pub root: TreeIndex,
}

pub struct InitWindow<'a> {
    pub traversal: &'a Traversal,
}

impl<'a> Widget for InitWindow<'a> {
    fn draw(&mut self, area: Rect, buf: &mut Buffer) {
        let Self {
            traversal: Traversal {
                tree, root_index, ..
            },
        } = self;
        Entries {
            tree: tree,
            root: *root_index,
        }
        .draw(area, buf);
    }
}

impl<'a> Widget for Entries<'a> {
    fn draw(&mut self, area: Rect, buf: &mut Buffer) {
        let Self { tree, root } = self;
        List::new(
            tree.neighbors_directed(*root, Direction::Outgoing)
                .filter_map(|w| {
                    tree.node_weight(w).map(|w| {
                        Text::Raw(
                            format!("{} | ----- | {}", w.size, w.name.to_string_lossy()).into(),
                        )
                    })
                }),
        )
        .block(Block::default().borders(Borders::ALL).title("Entries"))
        .start_corner(Corner::TopLeft)
        .draw(area, buf);
    }
}