diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-11-10 13:14:44 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-11-10 13:14:44 -0500 |
commit | 747da41fd3f192038c2e3194b4de041f7969f7a8 (patch) | |
tree | 5436c8b77a9bfcbefc90885513ea4ed0ec5b5d51 /src/state.rs | |
parent | 847f8834ae051c1c6177e9166cac855ccff113f0 (diff) | |
download | nbsh-747da41fd3f192038c2e3194b4de041f7969f7a8.tar.gz nbsh-747da41fd3f192038c2e3194b4de041f7969f7a8.zip |
simplify
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..b979ed7 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,83 @@ +use textmode::Textmode as _; + +pub struct State { + readline: crate::readline::Readline, + history: crate::history::History, + focus: Focus, +} + +impl State { + pub fn new(actions: async_std::channel::Sender<Action>) -> Self { + let readline = crate::readline::Readline::new(actions.clone()); + let history = crate::history::History::new(actions); + let focus = Focus::Readline; + Self { + readline, + history, + focus, + } + } + + pub async fn render( + &self, + out: &mut textmode::Output, + ) -> anyhow::Result<()> { + out.clear(); + if let Focus::Readline = self.focus { + self.history.render(out, self.readline.lines()).await?; + self.readline.render(out).await?; + } else { + self.history.render(out, 0).await?; + } + out.refresh().await?; + Ok(()) + } + + pub async fn handle_action( + &mut self, + action: Action, + output: &mut textmode::Output, + ) { + match action { + Action::Render => { + self.render(output).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(); + } + } + } + + 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; + } + false + } +} + +#[derive(Copy, Clone)] +pub enum Focus { + Readline, + History(usize), +} + +pub enum Action { + Render, + Run(String), + UpdateFocus(Focus), +} |