summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-11-17 21:45:25 -0500
committerJesse Luehrs <doy@tozt.net>2021-11-17 21:45:25 -0500
commit26bb4e54e9669b487817d09ebfa36836293d741d (patch)
treedfdedab0e497c3e62a900ca0e4ba94380bed67bd /src
parent35cdb6a27e25504f6eb368d48f1007085883635c (diff)
downloadnbsh-26bb4e54e9669b487817d09ebfa36836293d741d.tar.gz
nbsh-26bb4e54e9669b487817d09ebfa36836293d741d.zip
make action handling the main task
Diffstat (limited to 'src')
-rw-r--r--src/action.rs6
-rw-r--r--src/history.rs7
-rw-r--r--src/main.rs12
-rw-r--r--src/readline.rs5
-rw-r--r--src/state.rs16
5 files changed, 22 insertions, 24 deletions
diff --git a/src/action.rs b/src/action.rs
index 2e27393..c7f437c 100644
--- a/src/action.rs
+++ b/src/action.rs
@@ -5,6 +5,7 @@ pub enum Action {
Run(String),
UpdateFocus(crate::state::Focus),
Resize((u16, u16)),
+ Quit,
}
pub struct Debouncer {
@@ -46,7 +47,8 @@ impl Pending {
}
fn has_event(&self) -> bool {
- self.render.is_some()
+ self.done
+ || self.render.is_some()
|| self.force_redraw.is_some()
|| !self.run.is_empty()
|| self.focus.is_some()
@@ -85,7 +87,7 @@ impl Pending {
Some(Action::Run(cmd)) => self.run.push_back(cmd.to_string()),
Some(Action::UpdateFocus(focus)) => self.focus = Some(*focus),
Some(Action::Resize(size)) => self.size = Some(*size),
- None => self.done = true,
+ Some(Action::Quit) | None => self.done = true,
}
}
}
diff --git a/src/history.rs b/src/history.rs
index 3c15625..8549ab7 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -122,16 +122,11 @@ impl History {
Ok(self.entries.len() - 1)
}
- pub async fn handle_key(
- &mut self,
- key: textmode::Key,
- idx: usize,
- ) -> bool {
+ pub async fn handle_key(&mut self, key: textmode::Key, idx: usize) {
let entry = self.entries[idx].lock_arc().await;
if entry.running() {
entry.input.send(key.into_bytes()).await.unwrap();
}
- false
}
pub async fn render(
diff --git a/src/main.rs b/src/main.rs
index 6247d9f..05b889c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -47,17 +47,15 @@ async fn async_main() -> anyhow::Result<()> {
{
let state = async_std::sync::Arc::clone(&state);
async_std::task::spawn(async move {
- let debouncer = crate::action::debounce(action_r);
- while let Some(action) = debouncer.recv().await {
- state.lock_arc().await.handle_action(action).await;
+ while let Some(key) = input.read_key().await.unwrap() {
+ state.lock_arc().await.handle_input(key).await;
}
});
}
- while let Some(key) = input.read_key().await.unwrap() {
- if state.lock_arc().await.handle_input(key).await {
- break;
- }
+ let debouncer = crate::action::debounce(action_r);
+ while let Some(action) = debouncer.recv().await {
+ state.lock_arc().await.handle_action(action).await;
}
Ok(())
diff --git a/src/readline.rs b/src/readline.rs
index 9713a8a..093de3d 100644
--- a/src/readline.rs
+++ b/src/readline.rs
@@ -22,7 +22,7 @@ impl Readline {
}
}
- pub async fn handle_key(&mut self, key: textmode::Key) -> bool {
+ pub async fn handle_key(&mut self, key: textmode::Key) {
match key {
textmode::Key::String(s) => self.add_input(&s),
textmode::Key::Char(c) => {
@@ -30,7 +30,7 @@ impl Readline {
}
textmode::Key::Ctrl(b'c') => self.clear_input(),
textmode::Key::Ctrl(b'd') => {
- return true;
+ self.action.send(crate::action::Action::Quit).await.unwrap();
}
textmode::Key::Ctrl(b'l') => {
self.action
@@ -55,7 +55,6 @@ impl Readline {
.send(crate::action::Action::Render)
.await
.unwrap();
- false
}
pub fn lines(&self) -> usize {
diff --git a/src/state.rs b/src/state.rs
index f12fb8a..13fd3fd 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -90,15 +90,19 @@ impl State {
self.output.hard_refresh().await.unwrap();
self.render(false).await.unwrap();
}
+ crate::action::Action::Quit => {
+ // the debouncer should return None in this case
+ unreachable!();
+ }
}
}
- pub async fn handle_input(&mut self, key: textmode::Key) -> bool {
+ pub async fn handle_input(&mut self, key: textmode::Key) {
if self.escape {
- let mut ret = true;
+ let mut fallthrough = false;
match key {
textmode::Key::Ctrl(b'e') => {
- ret = false; // fall through and handle normally
+ fallthrough = true;
}
textmode::Key::Ctrl(b'l') => {
self.render(true).await.unwrap();
@@ -149,12 +153,12 @@ impl State {
_ => {}
}
self.escape = false;
- if ret {
- return false;
+ if !fallthrough {
+ return;
}
} else if key == textmode::Key::Ctrl(b'e') {
self.escape = true;
- return false;
+ return;
}
match self.focus {