Skip to main content

datui_lib/widgets/
debug.rs

1use ratatui::{
2    buffer::Buffer,
3    layout::Rect,
4    widgets::{Paragraph, Widget},
5};
6
7#[derive(Default)]
8pub struct DebugState {
9    pub num_events: usize,
10    pub num_frames: usize,
11    pub num_key_events: usize,
12    pub last_key_event_name: String,
13    pub last_type_name: String,
14    /// Last action taken (e.g. "scroll_left") for debugging key handling.
15    pub last_action: String,
16    pub enabled: bool,
17    /// Snapshot of main help flag at render time (set by App when enabled). Used by the debug overlay (DATUI_DEBUG=1) to verify help state.
18    pub show_help_at_render: bool,
19    /// Schema load path taken by the open's schema phase (one-file vs full scan); set when loading Parquet.
20    pub schema_load: Option<String>,
21    /// How long frames and event handlers take.
22    pub times: crate::loading::measurements::LoopTimes,
23}
24
25impl DebugState {
26    /// Name the action a key took, when the overlay is on to show it.
27    pub fn action(&mut self, name: impl FnOnce() -> String) {
28        if self.enabled {
29            self.last_action = name();
30        }
31    }
32
33    pub fn on_key(&mut self, event: &crossterm::event::KeyEvent) {
34        self.num_key_events += 1;
35        self.last_key_event_name = format!("{:?}", event.code);
36        self.last_type_name = format!("{:?}", event.kind);
37    }
38}
39
40impl Widget for &DebugState {
41    fn render(self, area: Rect, buf: &mut Buffer) {
42        let schema = self.schema_load.as_deref().unwrap_or("-");
43        Paragraph::new(format!(
44            "events={} keys={} last_key={} kind={} last_action={} help={} frames={} schema={} frame: {} handler: {}",
45            self.num_events,
46            self.num_key_events,
47            self.last_key_event_name,
48            self.last_type_name,
49            self.last_action,
50            self.show_help_at_render,
51            self.num_frames,
52            schema,
53            self.times.frames.summary(),
54            self.times.handlers.summary()
55        ))
56        .render(area, buf);
57    }
58}