summaryrefslogtreecommitdiffstats
path: root/src/history.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-11-10 01:03:11 -0500
committerJesse Luehrs <doy@tozt.net>2021-11-10 01:03:11 -0500
commitd0c26780d93a14be4d70bf2c784b79b3b0f06186 (patch)
tree9008ea7fa0b7aad9e5d19515dbcf758fd0436c11 /src/history.rs
parentfbc4db18ecea9755199b945368c02f32c0481c95 (diff)
downloadnbsh-d0c26780d93a14be4d70bf2c784b79b3b0f06186.tar.gz
nbsh-d0c26780d93a14be4d70bf2c784b79b3b0f06186.zip
refactor
Diffstat (limited to 'src/history.rs')
-rw-r--r--src/history.rs51
1 files changed, 38 insertions, 13 deletions
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<async_std::sync::Arc<async_std::sync::Mutex<HistoryEntry>>>,
+ action: async_std::channel::Sender<crate::nbsh::Action>,
}
impl History {
- pub fn new() -> Self {
- Self::default()
+ pub fn new(
+ action: async_std::channel::Sender<crate::nbsh::Action>,
+ ) -> 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<usize> {
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 {