From b7a0201d42995e34df4f541b23826ff7ac42dd9f Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 13 Nov 2021 16:26:04 -0500 Subject: move escape character handling to the top level input handling --- src/history.rs | 54 +++++++----------------------------------------------- src/state.rs | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/history.rs b/src/history.rs index 86fed66..5397c68 100644 --- a/src/history.rs +++ b/src/history.rs @@ -6,7 +6,6 @@ use textmode::Textmode as _; pub struct History { size: (u16, u16), entries: Vec>, - escape: bool, action: async_std::channel::Sender, } @@ -17,7 +16,6 @@ impl History { Self { size: (24, 80), entries: vec![], - escape: false, action, } } @@ -134,36 +132,13 @@ impl History { key: textmode::Key, idx: usize, ) -> bool { - if self.escape { - match key { - textmode::Key::Ctrl(b'e') => { - self.send_process_input(idx, &key.into_bytes()) - .await - .unwrap(); - } - textmode::Key::Char('r') => { - self.action - .send(crate::action::Action::UpdateFocus( - crate::state::Focus::Readline, - )) - .await - .unwrap(); - } - _ => {} - } - self.escape = false; - } else { - match key { - textmode::Key::Ctrl(b'e') => { - self.escape = true; - } - key => { - self.send_process_input(idx, &key.into_bytes()) - .await - .unwrap(); - } - } - } + self.entries[idx] + .lock_arc() + .await + .input + .send(key.into_bytes()) + .await + .unwrap(); false } @@ -219,21 +194,6 @@ impl History { } } } - - async fn send_process_input( - &self, - idx: usize, - input: &[u8], - ) -> anyhow::Result<()> { - self.entries[idx] - .lock_arc() - .await - .input - .send(input.to_vec()) - .await - .unwrap(); - Ok(()) - } } struct HistoryEntry { diff --git a/src/state.rs b/src/state.rs index ff26a75..193d1af 100644 --- a/src/state.rs +++ b/src/state.rs @@ -5,6 +5,7 @@ pub struct State { history: crate::history::History, focus: Focus, output: textmode::Output, + escape: bool, action: async_std::channel::Sender, } @@ -21,6 +22,7 @@ impl State { history, focus, output, + escape: false, action, } } @@ -65,6 +67,27 @@ impl State { } pub async fn handle_input(&mut self, key: textmode::Key) -> bool { + if self.escape { + let mut ret = true; + match key { + textmode::Key::Ctrl(b'e') => { + ret = false; // fall through and handle normally + } + textmode::Key::Char('r') => { + self.focus = Focus::Readline; + self.render().await.unwrap(); + } + _ => {} + } + self.escape = false; + if ret { + return false; + } + } else if key == textmode::Key::Ctrl(b'e') { + self.escape = true; + return false; + } + match self.focus { Focus::Readline => self.readline.handle_key(key).await, Focus::History(idx) => self.history.handle_key(key, idx).await, -- cgit v1.2.3-54-g00ecf