Skip to main content

cai_tui/
lib.rs

1//! CAI TUI - Terminal UI
2//!
3//! Interactive terminal interface for querying and exploring AI coding history.
4//!
5//! # Features
6//!
7//! - Query input with history navigation
8//! - Scrollable results table with sorting
9//! - Real-time status updates
10//! - Keyboard shortcuts (q=quit, /=search, etc.)
11//!
12//! # Example
13//!
14//! ```rust,no_run
15//! use cai_tui::run;
16//!
17//! // Create a storage implementation (MemoryStorage, SqliteStorage, etc.)
18//! // and pass it to the run function
19//! // let storage = ...;
20//! // run(storage).await?;
21//! ```
22
23#![warn(missing_docs, unused_crate_dependencies)]
24
25pub use cai_core::Result;
26
27mod app;
28mod event;
29mod ui;
30
31pub use app::{App, AppState, Column, Mode, SortOrder};
32pub use event::{Event, EventHandler};
33
34use cai_storage::Storage;
35use std::sync::Arc;
36use tokio::sync::RwLock;
37
38/// Run the TUI application
39///
40/// # Errors
41///
42/// Returns an error if the terminal setup fails or a runtime error occurs
43pub async fn run<S>(storage: Arc<S>) -> Result<()>
44where
45    S: Storage + 'static,
46{
47    // Initialize terminal
48    let mut terminal = ui::init_terminal()?;
49
50    // Create application
51    let app = App::new(storage);
52    let mut app = Arc::new(RwLock::new(app));
53
54    // Create event handler
55    let event_handler = EventHandler::new(100);
56
57    // Run application
58    let res = ui::run_app(&mut terminal, &mut app, event_handler).await;
59
60    // Restore terminal
61    ui::restore_terminal(terminal)?;
62
63    // Convert error type
64    res.map_err(|e| cai_core::Error::Message(e.to_string()))
65}