diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-11-10 13:29:43 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-11-10 13:29:43 -0500 |
commit | 0d0380af5a90503e30ae6678c54d481eb81ceec1 (patch) | |
tree | 39d6ca743726364aa5e09e96407e8bc3165de108 | |
parent | 84e8e53d3384402490c654d28f9c4182bd5f9afb (diff) | |
download | nbsh-0d0380af5a90503e30ae6678c54d481eb81ceec1.tar.gz nbsh-0d0380af5a90503e30ae6678c54d481eb81ceec1.zip |
simplify
-rw-r--r-- | src/main.rs | 14 | ||||
-rw-r--r-- | src/state.rs | 52 |
2 files changed, 26 insertions, 40 deletions
diff --git a/src/main.rs b/src/main.rs index 666b936..fe259fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,25 +19,21 @@ async fn async_main() -> anyhow::Result<()> { let (action_w, action_r) = async_std::channel::unbounded(); - let state = util::mutex(state::State::new(action_w)); + let mut state = state::State::new(action_w, output); + state.render().await.unwrap(); - state.lock_arc().await.render(&mut output).await.unwrap(); + let state = util::mutex(state); { let state = async_std::sync::Arc::clone(&state); async_std::task::spawn(async move { while let Ok(action) = action_r.recv().await { - state - .lock_arc() - .await - .handle_action(action, &mut output) - .await; + state.lock_arc().await.handle_action(action).await; } }); } - loop { - let key = input.read_key().await.unwrap(); + while let Some(key) = input.read_key().await.unwrap() { if state.lock_arc().await.handle_input(key).await { break; } diff --git a/src/state.rs b/src/state.rs index 55b8c9e..6051329 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,10 +4,14 @@ pub struct State { readline: crate::readline::Readline, history: crate::history::History, focus: Focus, + output: textmode::Output, } impl State { - pub fn new(actions: async_std::channel::Sender<Action>) -> Self { + pub fn new( + actions: async_std::channel::Sender<Action>, + output: textmode::Output, + ) -> Self { let readline = crate::readline::Readline::new(actions.clone()); let history = crate::history::History::new(actions); let focus = Focus::Readline; @@ -15,58 +19,44 @@ impl State { readline, history, focus, + output, } } - pub async fn render( - &self, - out: &mut textmode::Output, - ) -> anyhow::Result<()> { - out.clear(); + pub async fn render(&mut self) -> anyhow::Result<()> { + self.output.clear(); if let Focus::Readline = self.focus { - self.history.render(out, self.readline.lines()).await?; - self.readline.render(out).await?; + self.history + .render(&mut self.output, self.readline.lines()) + .await?; + self.readline.render(&mut self.output).await?; } else { - self.history.render(out, 0).await?; + self.history.render(&mut self.output, 0).await?; } - out.refresh().await?; + self.output.refresh().await?; Ok(()) } - pub async fn handle_action( - &mut self, - action: Action, - output: &mut textmode::Output, - ) { + pub async fn handle_action(&mut self, action: Action) { match action { Action::Render => { - self.render(output).await.unwrap(); + self.render().await.unwrap(); } Action::Run(ref cmd) => { self.history.run(cmd).await.unwrap(); } Action::UpdateFocus(new_focus) => { self.focus = new_focus; - self.render(output).await.unwrap(); + self.render().await.unwrap(); } } } - pub async fn handle_input(&mut self, key: Option<textmode::Key>) -> bool { - if let Some(key) = key { - let quit = match self.focus { - Focus::Readline => self.readline.handle_key(key).await, - Focus::History(idx) => { - self.history.handle_key(key, idx).await - } - }; - if quit { - return true; - } - } else { - return true; + pub async fn handle_input(&mut self, key: textmode::Key) -> bool { + match self.focus { + Focus::Readline => self.readline.handle_key(key).await, + Focus::History(idx) => self.history.handle_key(key, idx).await, } - false } } |