summaryrefslogtreecommitdiff
path: root/src/widgets/logview.rs
blob: 4b80d7c2894be2e0d8480b50b8e56c4f7d1196cc (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 ratatui::{crossterm::event::KeyEvent, text::ToSpan, widgets::Widget};

use crate::lua::iobuffer::iobuffer;

use super::statusbar::StatusBar;

#[derive(Default)]
pub struct LogView {
    bar: StatusBar,
}

impl LogView {
    pub fn new() -> Self {
        Self {
            bar: StatusBar::new(),
        }
    }

    pub fn handle_key_event(&mut self, _event: KeyEvent) {}
}

impl Widget for &mut LogView {
    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
    where
        Self: Sized,
    {
        self.bar.render(area, buf);
        let inner_area = self.bar.area(area);

        let lines = {
            let buffer = iobuffer().read().unwrap();
            buffer.get()
        };

        let mut line_area = inner_area;
        line_area.height = 1;
        line_area.y = inner_area.y + inner_area.height - 1;

        for line in lines.lines().rev() {
            let line = line.to_span();
            line.render(line_area, buf);

            line_area.y -= 1;

            if !inner_area.contains(line_area.into()) {
                break;
            }
        }
    }
}