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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::iter::repeat;
use tui::{
    buffer::Buffer,
    layout::Rect,
    widgets::{Block, Paragraph, Text, Widget},
};

pub fn fill_background_to_right(mut s: String, entire_width: u16) -> String {
    match (s.len(), entire_width as usize) {
        (x, y) if x >= y => s,
        (x, y) => {
            s.extend(repeat(' ').take(y - x));
            s
        }
    }
}

#[derive(Default)]
pub struct ReactList {
    /// The index at which the list last started. Used for scrolling
    offset: usize,
}

impl ReactList {
    fn list_offset_for(&self, entry_in_view: Option<usize>, height: usize) -> usize {
        match entry_in_view {
            Some(pos) => match height as usize {
                h if self.offset + h - 1 < pos => pos - h + 1,
                _ if self.offset > pos => pos,
                _ => self.offset,
            },
            None => 0,
        }
    }
}

#[derive(Default)]
pub struct ReactListProps<'b> {
    pub block: Option<Block<'b>>,
    pub entry_in_view: Option<usize>,
}

impl ReactList {
    pub fn render<'a, 't>(
        &mut self,
        props: ReactListProps<'a>,
        items: impl IntoIterator<Item = Vec<Text<'t>>>,
        area: Rect,
        buf: &mut Buffer,
    ) {
        let ReactListProps {
            block,
            entry_in_view,
        } = props;

        let list_area = match block {
            Some(mut b) => {
                b.draw(area, buf);
                b.inner(area)
            }
            None => area,
        };
        self.offset = self.list_offset_for(entry_in_view, list_area.height as usize);

        if list_area.width < 1 || list_area.height < 1 {
            return;
        }

        for (i, text_iterator) in items
            .into_iter()
            .skip(self.offset)
            .enumerate()
            .take(list_area.height as usize)
        {
            let (x, y) = (list_area.left(), list_area.top() + i as u16);
            Paragraph::new(text_iterator.iter()).draw(
                Rect {
                    x,
                    y,
                    width: list_area.width,
                    height: 1,
                },
                buf,
            );
        }
    }
}