clide/src/tui.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

2026-01-17 11:40:40 -05:00
use anyhow::{Context, Result};
use ratatui::{DefaultTerminal, Frame};
2025-04-13 12:17:11 -04:00
use std::error::Error;
2026-01-17 11:40:40 -05:00
use std::time::Duration;
use ratatui::crossterm::event;
use ratatui::crossterm::event::{Event, KeyCode};
use ratatui::widgets::Paragraph;
2025-04-13 12:17:11 -04:00
2026-01-17 11:40:40 -05:00
pub fn start(root_path: std::path::PathBuf) -> Result<()> {
2025-04-13 12:17:11 -04:00
println!("Starting the TUI editor at {:?}", root_path);
2026-01-17 11:40:40 -05:00
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;
}
}
2025-04-13 12:17:11 -04:00
Ok(())
}
2026-01-17 11:40:40 -05:00
fn should_quit() -> Result<bool> {
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());
}