summaryrefslogtreecommitdiff
path: root/src/widgets/logview.rs
blob: df2ebcefdd95b634595bfe6f63494990c0fcba70 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use ratatui::{crossterm::event::KeyEvent, style::{Color, Style}, text::ToSpan, widgets::{Widget, Paragraph}};

use crate::lua::iobuffer::{iobuffer, BufferChunk};
use crate::config::GlobalConfig;
use crate::lua;

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,
    {
        let theme = GlobalConfig::get().theme.view.clone();

        self.bar.render(area, buf);
        let inner_area = self.bar.area(area);

        let chunks = {
            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;

        theme
            .background
            .get((), lua::get())
            .unwrap_or_default()
            .apply(Paragraph::default())
            .render(inner_area, buf);

        for chunk in chunks.into_iter().rev() {
            if line_area.y < inner_area.y {
                break;
            }

            let (lines, color) = match chunk {
                BufferChunk::StdOut(s) => (s, Color::White),
                BufferChunk::StdErr(s) => (s, Color::Red),
            };

            for line in lines.lines().rev() {
                if line_area.y < inner_area.y {
                    break;
                }

                let line = line.to_span().style(Style::default().fg(color));
                line.render(line_area, buf);

                line_area.y -= 1;
            }
        }
    }
}