From 1d1a4af63079c1e42d3464f1dd9a1de689a9b84a Mon Sep 17 00:00:00 2001 From: James Norcutt Date: Sat, 21 Mar 2026 13:42:28 +0000 Subject: [PATCH] yaaasssss --- src/crossterm.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/crossterm.rs diff --git a/src/crossterm.rs b/src/crossterm.rs new file mode 100644 index 0000000..a9accb6 --- /dev/null +++ b/src/crossterm.rs @@ -0,0 +1,76 @@ +use std::error::Error; +use std::io; +use std::time::{Duration, Instant}; + +use crossterm::event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode}; +use crossterm::execute; +use crossterm::terminal::{ + disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, +}; +use ratatui::backend::{Backend, CrosstermBackend}; +use ratatui::Terminal; + +use crate::app::App; +use crate::ui; + +pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box> { + // setup terminal + enable_raw_mode()?; + let mut stdout = io::stdout(); + execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; + let backend = CrosstermBackend::new(stdout); + let mut terminal = Terminal::new(backend)?; + + // create app and run it + let app = App::new("Klipper-Tui", enhanced_graphics); + let app_result = run_app(&mut terminal, app, tick_rate); + + // restore terminal + disable_raw_mode()?; + execute!( + terminal.backend_mut(), + LeaveAlternateScreen, + DisableMouseCapture + )?; + terminal.show_cursor()?; + + if let Err(err) = app_result { + println!("{err:?}"); + } + + Ok(()) +} + +fn run_app( + terminal: &mut Terminal, + mut app: App, + tick_rate: Duration, +) -> Result<(), Box> +where + B::Error: 'static, +{ + let mut last_tick = Instant::now(); + loop { + terminal.draw(|frame| ui::render(frame, &mut app))?; + + let timeout = tick_rate.saturating_sub(last_tick.elapsed()); + if !event::poll(timeout)? { + app.on_tick(); + last_tick = Instant::now(); + continue; + } + if let Some(key) = event::read()?.as_key_press_event() { + match key.code { + KeyCode::Char('h') | KeyCode::Left => app.on_left(), + KeyCode::Char('j') | KeyCode::Down => app.on_down(), + KeyCode::Char('k') | KeyCode::Up => app.on_up(), + KeyCode::Char('l') | KeyCode::Right => app.on_right(), + KeyCode::Char(c) => app.on_key(c), + _ => {} + } + } + if app.should_quit { + return Ok(()); + } + } +}