use anyhow::{Context, Result}; use ratatui::{DefaultTerminal, Frame}; use std::error::Error; use std::time::Duration; use ratatui::crossterm::event; use ratatui::crossterm::event::{Event, KeyCode}; use ratatui::widgets::Paragraph; pub fn start(root_path: std::path::PathBuf) -> Result<()> { println!("Starting the TUI editor at {:?}", root_path); let terminal = ratatui::init(); let app_result = run(terminal, root_path).context("Failed to start the TUI editor."); ratatui::restore(); app_result } pub fn run( mut terminal: DefaultTerminal, root_path: std::path::PathBuf, ) -> Result<()> { loop { terminal.draw(draw)?; if should_quit()? { break; } } Ok(()) } fn should_quit() -> Result { if event::poll(Duration::from_millis(250)).context("event poll failed")? { if let Event::Key(key) = event::read().context("event read failed")? { return Ok(KeyCode::Char('q') == key.code); } } Ok(false) } fn draw(frame: &mut Frame) { let greeting = Paragraph::new("Hello World! (press 'q' to quit)"); frame.render_widget(greeting, frame.area()); }