aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMroik <mroik@delayed.space>2025-01-27 15:36:57 +0100
committerMroik <mroik@delayed.space>2025-02-01 19:33:02 +0100
commit3c6073f6b08a7e812e2f1f22bb3882427e980663 (patch)
tree8ccba10fb8ce2e89aa37900a5abb9e144da96af0 /src
parent318a19429a34ff48dbcc1894373fc6430435e36f (diff)
Implement input handling
Diffstat (limited to 'src')
-rw-r--r--src/app.rs45
-rw-r--r--src/event.rs6
2 files changed, 48 insertions, 3 deletions
diff --git a/src/app.rs b/src/app.rs
index 87caf62..f0ffff0 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -1,15 +1,23 @@
-use std::io::{stdout, Stdout};
+use std::{
+ error::Error,
+ io::{stdout, Stdout},
+ time::Duration,
+};
+use crossterm::event::{poll, read, KeyCode, KeyModifiers};
use rand::Rng;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use crate::event::Event;
+const TICK_RATE: u64 = 1000 / 20;
+
pub struct App {
stdout: Stdout,
quote: Vec<String>,
event_tx: Sender<Event>,
event_rx: Receiver<Event>,
+ running: bool,
}
impl App {
@@ -22,10 +30,43 @@ impl App {
quote: quotes[chosen].clone(),
event_rx,
event_tx,
+ running: false,
}
}
- pub fn run() {
+ pub async fn run(&mut self) {
todo!()
}
+
+ // TODO
+ // - [ ] Pause on focus lost
+ // - [ ] Invalidate on paste
+ async fn handle_input(&mut self) -> Result<(), Box<dyn Error>> {
+ while self.running {
+ if poll(Duration::from_millis(TICK_RATE))? {
+ match read()? {
+ //crossterm::event::Event::FocusGained => todo!(),
+ //crossterm::event::Event::FocusLost => todo!(),
+ //crossterm::event::Event::Paste(_) => todo!(),
+ crossterm::event::Event::Key(key_event) => {
+ if key_event.code == KeyCode::Char('c')
+ && key_event.modifiers == KeyModifiers::CONTROL
+ {
+ self.event_tx.send(Event::Terminate).await?;
+ continue;
+ }
+ if key_event.code == KeyCode::Backspace {
+ self.event_tx.send(Event::Backspace).await?;
+ continue;
+ }
+ if let KeyCode::Char(c) = key_event.code {
+ self.event_tx.send(Event::KeyPress(c)).await?;
+ }
+ }
+ _ => (),
+ }
+ }
+ }
+ return Ok(());
+ }
}
diff --git a/src/event.rs b/src/event.rs
index 7c470eb..1320c68 100644
--- a/src/event.rs
+++ b/src/event.rs
@@ -1 +1,5 @@
-pub enum Event {}
+pub enum Event {
+ Terminate,
+ KeyPress(char),
+ Backspace,
+}
XMR address: 854DmXNrxULU3ZFJVs4Wc8PFhbq29RhqHhY8W6cdWrtFN3qmooKyyeYPcDzZTNRxphhJ5UzASQfAdEMwSteVqymk28aLhqj