Skip to main content

ez_tui/components/mock/
logs.rs

1use crate::{MockComponent, Props, Theme};
2use eztui_derive::MockProps;
3use ratatui::buffer::Buffer;
4use ratatui::layout::Rect;
5use ratatui::prelude::Widget;
6use ratatui::style::Style;
7use ratatui::style::palette::tailwind::{CYAN, GRAY, GREEN, ORANGE, RED};
8use std::fmt::{Debug, Formatter};
9use tui_logger::{TuiLoggerLevelOutput, TuiLoggerSmartWidget, TuiLoggerWidget, TuiWidgetState};
10
11/// A generic mock component to display logs inside the TUI
12///
13/// **This mock initializes `tui_logger` and so it should be instantiated only once.**
14#[derive(MockProps)]
15pub struct LogsMock {
16    props: Props,
17    pub(crate) state: TuiWidgetState,
18    smart: bool,
19}
20
21impl Default for LogsMock {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl LogsMock {
28    /// Create a new [`LogsMock`] component with the log level set via the [`CommonArg`] command line arguments.
29    #[must_use]
30    pub fn new() -> Self {
31        LogsMock {
32            props: Props::default(),
33            state: TuiWidgetState::default(),
34            smart: true,
35        }
36    }
37}
38
39impl MockComponent for LogsMock {
40    fn draw(&mut self, area: Rect, buf: &mut Buffer, _: &Theme) {
41        if self.smart {
42            TuiLoggerSmartWidget::default()
43                .state(&self.state)
44                .style_error(Style::default().fg(RED.c600))
45                .style_warn(Style::default().fg(ORANGE.c600))
46                .style_debug(Style::default().fg(GREEN.c600))
47                .style_trace(Style::default().fg(GRAY.c600))
48                .style_info(Style::default().fg(CYAN.c600))
49                .output_separator(' ')
50                .output_timestamp(Some("%H:%M:%S".to_string()))
51                .output_level(Some(TuiLoggerLevelOutput::Long))
52                .output_target(false)
53                .output_file(false)
54                .output_line(false)
55                .render(area, buf);
56        } else {
57            TuiLoggerWidget::default()
58                .state(&self.state)
59                .style_error(Style::default().fg(RED.c600))
60                .style_warn(Style::default().fg(ORANGE.c600))
61                .style_debug(Style::default().fg(GREEN.c600))
62                .style_trace(Style::default().fg(GRAY.c600))
63                .style_info(Style::default().fg(CYAN.c600))
64                .output_separator(' ')
65                .output_timestamp(Some("%H:%M:%S".to_string()))
66                .output_level(Some(TuiLoggerLevelOutput::Long))
67                .output_target(false)
68                .output_file(false)
69                .output_line(false)
70                .render(area, buf);
71        }
72    }
73}
74#[allow(clippy::missing_fields_in_debug)]
75impl Debug for LogsMock {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        f.debug_struct("LogsMock")
78            .field("props", &self.props)
79            .field("smart", &self.smart)
80            .finish()
81    }
82}