From d0c26780d93a14be4d70bf2c784b79b3b0f06186 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Nov 2021 01:03:11 -0500 Subject: refactor --- src/history.rs | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'src/history.rs') diff --git a/src/history.rs b/src/history.rs index 339e53d..bf89ec1 100644 --- a/src/history.rs +++ b/src/history.rs @@ -2,21 +2,22 @@ use async_std::io::ReadExt as _; use pty_process::Command as _; use textmode::Textmode as _; -#[derive(Default)] pub struct History { entries: Vec>>, + action: async_std::channel::Sender, } impl History { - pub fn new() -> Self { - Self::default() + pub fn new( + action: async_std::channel::Sender, + ) -> Self { + Self { + entries: vec![], + action, + } } - pub async fn run( - &mut self, - cmd: &str, - render: async_std::channel::Sender<()>, - ) -> anyhow::Result<()> { + pub async fn run(&mut self, cmd: &str) -> anyhow::Result { let (exe, args) = parse_cmd(cmd); let mut process = async_process::Command::new(&exe); process.args(&args); @@ -27,7 +28,7 @@ impl History { HistoryEntry::new(cmd), )); let task_entry = async_std::sync::Arc::clone(&entry); - let task_render = render.clone(); + let task_action = self.action.clone(); async_std::task::spawn(async move { loop { let mut buf = [0_u8; 4096]; @@ -40,16 +41,32 @@ impl History { eprintln!("pty read failed: {:?}", e); } task_entry.lock_arc().await.running = false; - task_render.send(()).await.unwrap(); + task_action + .send(crate::nbsh::Action::Render) + .await + .unwrap(); break; } } - task_render.send(()).await.unwrap(); + task_action.send(crate::nbsh::Action::Render).await.unwrap(); } }); self.entries.push(entry); - render.send(()).await.unwrap(); - Ok(()) + self.action.send(crate::nbsh::Action::Render).await.unwrap(); + Ok(self.entries.len() - 1) + } + + pub async fn handle_key( + &mut self, + key: textmode::Key, + idx: usize, + ) -> bool { + if let textmode::Key::Bytes(b) = key { + self.send_process_input(idx, &b).await.unwrap(); + } else { + unreachable!(); + } + false } pub async fn render( @@ -97,6 +114,14 @@ impl History { } Ok(()) } + + async fn send_process_input( + &self, + idx: usize, + input: &[u8], + ) -> anyhow::Result<()> { + todo!() + } } struct HistoryEntry { -- cgit v1.2.3-54-g00ecf