use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, prelude::Backend, Terminal};
use std::io;
use std::time::{Duration, Instant};
pub mod app;
mod agents_view;
pub mod headless;
mod inference_view;
mod ui;
use app::PeripheralCache;
pub use app::{AcceleratorInfo, AcceleratorType, App};
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut app = App::new_fast()?;
terminal.draw(|f| ui::draw(f, &app))?;
let res = run_app(&mut terminal, &mut app);
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Err(err) = res {
eprintln!("Error: {:?}", err);
}
Ok(())
}
fn run_app<B: Backend>(
terminal: &mut Terminal<B>,
app: &mut App,
) -> Result<(), Box<dyn std::error::Error>>
where
<B as Backend>::Error: 'static,
{
const TICK_MS: u64 = 250;
let tick_duration = Duration::from_millis(TICK_MS);
const FAST_UPDATE_TICKS: u64 = 2; const PROCESS_TICKS: u64 = 4;
let mut tick_count: u64 = 0;
let mut last_tick = Instant::now();
let mut needs_render = true;
let peripheral_data = std::sync::Arc::new(std::sync::Mutex::new(PeripheralCache::default()));
{
let pd = std::sync::Arc::clone(&peripheral_data);
std::thread::Builder::new()
.name("peripheral-refresh".into())
.spawn(move || loop {
let mut cache = PeripheralCache::default();
cache.refresh();
if let Ok(mut shared) = pd.lock() {
*shared = cache;
}
std::thread::sleep(Duration::from_secs(10));
})
.ok();
}
let mut last_peripheral_sync = Instant::now();
const MIN_RENDER_INTERVAL_MS: u64 = 16; let mut last_render = Instant::now() - Duration::from_millis(MIN_RENDER_INTERVAL_MS);
loop {
let elapsed = last_tick.elapsed();
let timeout = tick_duration.saturating_sub(elapsed);
let mut events_processed = false;
if crossterm::event::poll(timeout)? {
loop {
if let Ok(Event::Key(key)) = event::read() {
if key.kind == KeyEventKind::Press {
events_processed = true;
if app.handle_key(key.code, key.modifiers) {
return Ok(());
}
}
}
if !crossterm::event::poll(Duration::ZERO)? {
break;
}
}
}
if events_processed {
needs_render = true;
}
if needs_render && last_render.elapsed() >= Duration::from_millis(MIN_RENDER_INTERVAL_MS) {
terminal.draw(|f| ui::draw(f, app))?;
last_render = Instant::now();
needs_render = false;
}
if last_tick.elapsed() >= tick_duration {
tick_count = tick_count.wrapping_add(1);
last_tick = Instant::now();
app.check_background_init();
if tick_count.is_multiple_of(FAST_UPDATE_TICKS) {
let _ = app.update_fast();
if app.snapshot_changed_since_render() {
needs_render = true;
}
}
if tick_count.is_multiple_of(PROCESS_TICKS) {
let _ = app.update_processes_only();
needs_render = true;
}
if last_peripheral_sync.elapsed() >= Duration::from_secs(2) {
if let Ok(data) = peripheral_data.try_lock() {
if app.peripheral_cache.audio_info != data.audio_info
|| app.peripheral_cache.usb_info != data.usb_info
{
app.peripheral_cache = data.clone();
needs_render = true;
}
}
last_peripheral_sync = Instant::now();
}
if tick_count.is_multiple_of(4) {
needs_render = true;
}
}
}
}