From 70f48c3ef7dde34546b6d646818333505d0e4777 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 17 Nov 2021 22:29:05 -0500 Subject: refactor --- src/readline.rs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'src/readline.rs') diff --git a/src/readline.rs b/src/readline.rs index 093de3d..529c102 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -6,23 +6,22 @@ pub struct Readline { prompt: String, input_line: String, pos: usize, - action: async_std::channel::Sender, } impl Readline { - pub fn new( - action: async_std::channel::Sender, - ) -> Self { + pub fn new() -> Self { Self { size: (24, 80), prompt: "$ ".into(), input_line: "".into(), pos: 0, - action, } } - pub async fn handle_key(&mut self, key: textmode::Key) { + pub async fn handle_key( + &mut self, + key: textmode::Key, + ) -> Option { match key { textmode::Key::String(s) => self.add_input(&s), textmode::Key::Char(c) => { @@ -30,20 +29,15 @@ impl Readline { } textmode::Key::Ctrl(b'c') => self.clear_input(), textmode::Key::Ctrl(b'd') => { - self.action.send(crate::action::Action::Quit).await.unwrap(); + return Some(crate::action::Action::Quit); } textmode::Key::Ctrl(b'l') => { - self.action - .send(crate::action::Action::ForceRedraw) - .await - .unwrap(); + return Some(crate::action::Action::ForceRedraw); } textmode::Key::Ctrl(b'm') => { - self.action - .send(crate::action::Action::Run(self.input())) - .await - .unwrap(); + let cmd = self.input(); self.clear_input(); + return Some(crate::action::Action::Run(cmd)); } textmode::Key::Ctrl(b'u') => self.clear_backwards(), textmode::Key::Backspace => self.backspace(), @@ -51,10 +45,7 @@ impl Readline { textmode::Key::Right => self.cursor_right(), _ => {} } - self.action - .send(crate::action::Action::Render) - .await - .unwrap(); + Some(crate::action::Action::Render) } pub fn lines(&self) -> usize { -- cgit v1.2.3-54-g00ecf