datui_lib/widgets/
debug.rs1use 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 pub last_action: String,
16 pub enabled: bool,
17 pub show_help_at_render: bool,
19 pub schema_load: Option<String>,
21 pub times: crate::loading::measurements::LoopTimes,
23}
24
25impl DebugState {
26 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}