summaryrefslogtreecommitdiffstats
path: root/src/state.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-11 17:54:10 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-11 17:55:19 -0500
commit62fe2477fb8bbbfbca34779b992979f9c1a8ba53 (patch)
treea4c12df90fefa5b1f2b81905ed3f6ca2d1dcd079 /src/state.rs
parent6ca82f5be15f1c6c936c82266d1f6dc13185346c (diff)
downloadnbsh-62fe2477fb8bbbfbca34779b992979f9c1a8ba53.tar.gz
nbsh-62fe2477fb8bbbfbca34779b992979f9c1a8ba53.zip
move key handling into the main event loop
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs253
1 files changed, 129 insertions, 124 deletions
diff --git a/src/state.rs b/src/state.rs
index 6ecfa07..f086334 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -23,130 +23,6 @@ impl State {
}
}
- pub async fn handle_key(
- &mut self,
- key: textmode::Key,
- ) -> Option<crate::action::Action> {
- if self.escape {
- self.escape = false;
- self.handle_key_escape(key).await
- } else if key == textmode::Key::Ctrl(b'e') {
- self.escape = true;
- None
- } else {
- match self.focus {
- crate::action::Focus::Readline => {
- self.readline
- .handle_key(key, self.history.entry_count())
- .await
- }
- crate::action::Focus::History(idx) => {
- self.history.handle_key(key, idx).await;
- None
- }
- crate::action::Focus::Scrolling(_) => {
- self.handle_key_escape(key).await
- }
- }
- }
- }
-
- async fn handle_key_escape(
- &mut self,
- key: textmode::Key,
- ) -> Option<crate::action::Action> {
- match key {
- textmode::Key::Ctrl(b'd') => {
- return Some(crate::action::Action::Quit);
- }
- textmode::Key::Ctrl(b'e') => {
- self.set_focus(
- crate::action::Focus::Scrolling(self.focus_idx()),
- None,
- )
- .await;
- }
- textmode::Key::Ctrl(b'l') => {
- return Some(crate::action::Action::ForceRedraw);
- }
- textmode::Key::Ctrl(b'm') => {
- let idx = self.focus_idx();
- let (focus, entry) = if let Some(idx) = idx {
- let entry = self.history.entry(idx).await;
- (entry.running(), Some(entry))
- } else {
- (true, None)
- };
- if focus {
- self.set_focus(
- idx.map_or(crate::action::Focus::Readline, |idx| {
- crate::action::Focus::History(idx)
- }),
- entry,
- )
- .await;
- }
- }
- textmode::Key::Char(' ') => {
- if let Some(idx) = self.focus_idx() {
- let entry = self.history.entry(idx).await;
- self.readline.set_input(&entry.cmd());
- self.set_focus(
- crate::action::Focus::Readline,
- Some(entry),
- )
- .await;
- }
- }
- textmode::Key::Char('e') => {
- if let crate::action::Focus::History(idx) = self.focus {
- self.history
- .handle_key(textmode::Key::Ctrl(b'e'), idx)
- .await;
- }
- }
- textmode::Key::Char('f') => {
- if let Some(idx) = self.focus_idx() {
- let mut entry = self.history.entry(idx).await;
- let mut focus = crate::action::Focus::History(idx);
- if let crate::action::Focus::Scrolling(_) = self.focus {
- entry.set_fullscreen(true);
- } else {
- entry.toggle_fullscreen();
- if !entry.should_fullscreen() && !entry.running() {
- focus =
- crate::action::Focus::Scrolling(Some(idx));
- }
- }
- self.set_focus(focus, Some(entry)).await;
- }
- }
- textmode::Key::Char('j') | textmode::Key::Down => {
- self.set_focus(
- crate::action::Focus::Scrolling(
- self.scroll_down(self.focus_idx()),
- ),
- None,
- )
- .await;
- }
- textmode::Key::Char('k') | textmode::Key::Up => {
- self.set_focus(
- crate::action::Focus::Scrolling(
- self.scroll_up(self.focus_idx()),
- ),
- None,
- )
- .await;
- }
- textmode::Key::Char('r') => {
- self.set_focus(crate::action::Focus::Readline, None).await;
- }
- _ => {}
- }
- Some(crate::action::Action::Render)
- }
-
pub async fn render(
&self,
out: &mut textmode::Output,
@@ -246,6 +122,11 @@ impl State {
) {
let mut hard_refresh = false;
match action {
+ crate::action::Action::Key(key) => {
+ if let Some(action) = self.handle_key(key).await {
+ action_w.send(action).await.unwrap();
+ }
+ }
crate::action::Action::Render => {
// for instance, if we are rerendering because of command
// output, that output could increase the number of lines of
@@ -295,6 +176,130 @@ impl State {
self.render(out, hard_refresh).await.unwrap();
}
+ async fn handle_key(
+ &mut self,
+ key: textmode::Key,
+ ) -> Option<crate::action::Action> {
+ if self.escape {
+ self.escape = false;
+ self.handle_key_escape(key).await
+ } else if key == textmode::Key::Ctrl(b'e') {
+ self.escape = true;
+ None
+ } else {
+ match self.focus {
+ crate::action::Focus::Readline => {
+ self.readline
+ .handle_key(key, self.history.entry_count())
+ .await
+ }
+ crate::action::Focus::History(idx) => {
+ self.history.handle_key(key, idx).await;
+ None
+ }
+ crate::action::Focus::Scrolling(_) => {
+ self.handle_key_escape(key).await
+ }
+ }
+ }
+ }
+
+ async fn handle_key_escape(
+ &mut self,
+ key: textmode::Key,
+ ) -> Option<crate::action::Action> {
+ match key {
+ textmode::Key::Ctrl(b'd') => {
+ return Some(crate::action::Action::Quit);
+ }
+ textmode::Key::Ctrl(b'e') => {
+ self.set_focus(
+ crate::action::Focus::Scrolling(self.focus_idx()),
+ None,
+ )
+ .await;
+ }
+ textmode::Key::Ctrl(b'l') => {
+ return Some(crate::action::Action::ForceRedraw);
+ }
+ textmode::Key::Ctrl(b'm') => {
+ let idx = self.focus_idx();
+ let (focus, entry) = if let Some(idx) = idx {
+ let entry = self.history.entry(idx).await;
+ (entry.running(), Some(entry))
+ } else {
+ (true, None)
+ };
+ if focus {
+ self.set_focus(
+ idx.map_or(crate::action::Focus::Readline, |idx| {
+ crate::action::Focus::History(idx)
+ }),
+ entry,
+ )
+ .await;
+ }
+ }
+ textmode::Key::Char(' ') => {
+ if let Some(idx) = self.focus_idx() {
+ let entry = self.history.entry(idx).await;
+ self.readline.set_input(&entry.cmd());
+ self.set_focus(
+ crate::action::Focus::Readline,
+ Some(entry),
+ )
+ .await;
+ }
+ }
+ textmode::Key::Char('e') => {
+ if let crate::action::Focus::History(idx) = self.focus {
+ self.history
+ .handle_key(textmode::Key::Ctrl(b'e'), idx)
+ .await;
+ }
+ }
+ textmode::Key::Char('f') => {
+ if let Some(idx) = self.focus_idx() {
+ let mut entry = self.history.entry(idx).await;
+ let mut focus = crate::action::Focus::History(idx);
+ if let crate::action::Focus::Scrolling(_) = self.focus {
+ entry.set_fullscreen(true);
+ } else {
+ entry.toggle_fullscreen();
+ if !entry.should_fullscreen() && !entry.running() {
+ focus =
+ crate::action::Focus::Scrolling(Some(idx));
+ }
+ }
+ self.set_focus(focus, Some(entry)).await;
+ }
+ }
+ textmode::Key::Char('j') | textmode::Key::Down => {
+ self.set_focus(
+ crate::action::Focus::Scrolling(
+ self.scroll_down(self.focus_idx()),
+ ),
+ None,
+ )
+ .await;
+ }
+ textmode::Key::Char('k') | textmode::Key::Up => {
+ self.set_focus(
+ crate::action::Focus::Scrolling(
+ self.scroll_up(self.focus_idx()),
+ ),
+ None,
+ )
+ .await;
+ }
+ textmode::Key::Char('r') => {
+ self.set_focus(crate::action::Focus::Readline, None).await;
+ }
+ _ => {}
+ }
+ Some(crate::action::Action::Render)
+ }
+
async fn default_scene(
&self,
focus: crate::action::Focus,