45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
use async_trait::async_trait;
|
|
use owlen_core::ui::UiController;
|
|
use tokio::sync::{mpsc, oneshot};
|
|
|
|
/// A request sent from the UiController to the TUI event loop.
|
|
#[derive(Debug)]
|
|
pub enum TuiRequest {
|
|
Confirm {
|
|
prompt: String,
|
|
tx: oneshot::Sender<bool>,
|
|
},
|
|
}
|
|
|
|
/// An implementation of the UiController trait for the TUI.
|
|
/// It uses channels to communicate with the main ChatApp event loop.
|
|
pub struct TuiController {
|
|
tx: mpsc::UnboundedSender<TuiRequest>,
|
|
}
|
|
|
|
impl TuiController {
|
|
pub fn new(tx: mpsc::UnboundedSender<TuiRequest>) -> Self {
|
|
Self { tx }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl UiController for TuiController {
|
|
async fn confirm(&self, prompt: &str) -> bool {
|
|
let (tx, rx) = oneshot::channel();
|
|
let request = TuiRequest::Confirm {
|
|
prompt: prompt.to_string(),
|
|
tx,
|
|
};
|
|
|
|
if self.tx.send(request).is_err() {
|
|
// Receiver was dropped, so we can't get confirmation.
|
|
// Default to false for safety.
|
|
return false;
|
|
}
|
|
|
|
// Wait for the response from the TUI.
|
|
rx.await.unwrap_or(false)
|
|
}
|
|
}
|