Add textarea support

This commit is contained in:
2025-10-13 00:08:56 +08:00
parent 4a0fa965c6
commit bcc60c5203
8 changed files with 201 additions and 78 deletions

View File

@@ -1,29 +1,28 @@
use color_eyre::eyre::{Result, eyre};
use crossterm;
use crossterm::event;
use futures::FutureExt;
use futures::StreamExt;
use color_eyre::eyre::{Result, eyre};
use std::time::Duration;
use crossterm::event::EventStream;
use futures::StreamExt;
use tokio::sync::mpsc::UnboundedSender;
use tokio::task::JoinHandle;
#[derive(Clone)]
pub(crate) enum Event {
pub(crate) enum AppEvent {
Error,
Tick,
Key(event::KeyEvent),
Raw(crossterm::event::Event),
}
pub(crate) struct EventHandler {
_tx: UnboundedSender<Event>,
rx: tokio::sync::mpsc::UnboundedReceiver<Event>,
task: Option<JoinHandle<()>>,
_tx: UnboundedSender<AppEvent>,
rx: tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
pub task: JoinHandle<()>
}
impl EventHandler {
pub fn new(tick_rate: Duration) -> Self {
let mut interval = tokio::time::interval(tick_rate);
let mut event_reader = event::EventStream::new();
let mut event_reader = EventStream::new();
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let _tx = tx.clone();
@@ -35,28 +34,23 @@ impl EventHandler {
tokio::select! {
maybe_event = crossterm_event => {
if let Some(Err(_)) = maybe_event {
tx.send(Event::Error).unwrap()
}
else if let Some(Ok(event)) = maybe_event &&
let event::Event::Key(key) = event
{
tx.send(Event::Key(key)).unwrap()
tx.send(AppEvent::Error).unwrap()
} else if let Some(Ok(event)) = maybe_event {
tx.send(AppEvent::Raw(event)).unwrap();
}
}
_ = delay => {
tx.send(Event::Tick).unwrap()
tx.send(AppEvent::Tick).unwrap()
}
}
}
});
Self {
_tx,
rx,
task: Some(task),
_tx, rx, task
}
}
pub(crate) async fn next(&mut self) -> Result<Event> {
pub(crate) async fn next(&mut self) -> Result<AppEvent> {
self.rx.recv().await.ok_or(eyre!("Unable to get event"))
}
}