#![allow(missing_docs)]
use dracon_terminal_engine::framework::command::{BoundCommand, OutputParser};
use dracon_terminal_engine::framework::prelude::*;
use dracon_terminal_engine::framework::widget::WidgetId;
use dracon_terminal_engine::framework::widgets::{Gauge, KeyValueGrid, StatusBadge};
use ratatui::layout::Rect;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
fn main() -> std::io::Result<()> {
let mut app = App::new()?
.title("Command Dashboard")
.fps(30)
.theme(Theme::from_env_or(Theme::nord()));
let cpu_gauge = Gauge::new("CPU %").max(100.0).bind_command(
BoundCommand::new("cat /proc/loadavg")
.parser(OutputParser::Regex {
pattern: r"^([0-9.]+)".into(),
group: Some(0),
})
.refresh(2),
);
let mem_gauge = Gauge::new("Memory %").max(100.0).bind_command(
BoundCommand::new("free | grep Mem | awk '{print int($3/$2*100)}'").refresh(5),
);
let disk_gauge = Gauge::new("Disk %").max(100.0).bind_command(
BoundCommand::new("df -h / | tail -1 | awk '{print $5}' | tr -d '%'").refresh(30),
);
let kv_grid = KeyValueGrid::new().separator(" ").bind_command(
BoundCommand::new("uname -snr").refresh(0), );
let status = StatusBadge::new(WidgetId::default_id())
.with_status("OK")
.with_label("System");
app.add_widget(Box::new(cpu_gauge), Rect::new(0, 0, 26, 3));
app.add_widget(Box::new(mem_gauge), Rect::new(26, 0, 27, 3));
app.add_widget(Box::new(disk_gauge), Rect::new(53, 0, 27, 3));
app.add_widget(Box::new(kv_grid), Rect::new(0, 3, 60, 6));
app.add_widget(Box::new(status), Rect::new(60, 3, 20, 1));
let should_quit = Arc::new(AtomicBool::new(false));
let quit_check = Arc::clone(&should_quit);
app = app
.on_input(move |key| {
if key.code == KeyCode::Char('q') && key.modifiers.contains(KeyModifiers::CONTROL) && key.kind == KeyEventKind::Press {
should_quit.store(true, Ordering::SeqCst);
true
} else {
false
}
})
.on_tick(move |ctx, _| {
if quit_check.load(Ordering::SeqCst) {
ctx.stop();
}
});
app.run(|_ctx| {})
}