From 98617b70d8e85053c1ca56f26c58d49d880a0ce1 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 8 Jan 2022 14:58:23 -0500 Subject: remove pipeline from the environment it was only really here for convenience, but it doesn't really make a lot of sense --- src/env.rs | 16 ---------------- src/pipeline/mod.rs | 21 ++++++++++++++------- src/shell/history/mod.rs | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/env.rs b/src/env.rs index 221182e..41484d0 100644 --- a/src/env.rs +++ b/src/env.rs @@ -9,7 +9,6 @@ pub enum Env { #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct V0 { - pipeline: Option, idx: usize, #[serde( serialize_with = "serialize_status", @@ -23,7 +22,6 @@ pub struct V0 { impl Env { pub fn new() -> Self { Self::V0(V0 { - pipeline: None, idx: 0, latest_status: std::process::ExitStatus::from_raw(0), pwd: std::env::current_dir().unwrap(), @@ -31,20 +29,6 @@ impl Env { }) } - pub fn pipeline(&self) -> Option<&str> { - match self { - Self::V0(env) => env.pipeline.as_deref(), - } - } - - pub fn set_pipeline(&mut self, pipeline: String) { - match self { - Self::V0(env) => { - env.pipeline = Some(pipeline); - } - } - } - pub fn idx(&self) -> usize { match self { Self::V0(env) => env.idx, diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index fab6793..b15e182 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -29,8 +29,8 @@ pub async fn run() -> anyhow::Result { io.set_stdout(stdout); io.set_stderr(stderr); - let mut env = read_data(shell_read).await?; - run_with_env(&mut env, &io, &shell_write).await?; + let (pipeline, mut env) = read_data(shell_read).await?; + run_with_env(&pipeline, &mut env, &io, &shell_write).await?; let status = *env.latest_status(); env.update()?; @@ -43,23 +43,30 @@ pub async fn run() -> anyhow::Result { } async fn run_with_env( + pipeline: &str, env: &mut Env, io: &builtins::Io, shell_write: &async_std::fs::File, ) -> anyhow::Result<()> { - let pipeline = - crate::parse::ast::Pipeline::parse(env.pipeline().unwrap())?; + let pipeline = crate::parse::ast::Pipeline::parse(pipeline)?; let (children, pg) = spawn_children(pipeline, env, io)?; let status = wait_children(children, pg, env, io, shell_write).await; env.set_status(status); Ok(()) } -async fn read_data(mut fh: async_std::fs::File) -> anyhow::Result { +async fn read_data( + mut fh: async_std::fs::File, +) -> anyhow::Result<(String, Env)> { let mut data = vec![]; fh.read_to_end(&mut data).await?; - let env = Env::from_bytes(&data); - Ok(env) + let pipeline = bincode::deserialize(&data).unwrap(); + let len: usize = bincode::serialized_size(&pipeline) + .unwrap() + .try_into() + .unwrap(); + let env = Env::from_bytes(&data[len..]); + Ok((pipeline, env)) } async fn write_event( diff --git a/src/shell/history/mod.rs b/src/shell/history/mod.rs index 9e1d1fd..55f2d8a 100644 --- a/src/shell/history/mod.rs +++ b/src/shell/history/mod.rs @@ -300,8 +300,14 @@ fn run_commands( }; for pipeline in ast.pipelines() { - env.set_pipeline(pipeline.input_string().to_string()); - match run_pipeline(&pty, &mut env, event_w.clone()).await { + match run_pipeline( + pipeline.input_string(), + &pty, + &mut env, + event_w.clone(), + ) + .await + { Ok((pipeline_status, done)) => { env.set_status(pipeline_status); if done { @@ -326,6 +332,7 @@ fn run_commands( } async fn run_pipeline( + pipeline: &str, pty: &pty::Pty, env: &mut Env, event_w: async_std::channel::Sender, @@ -352,6 +359,9 @@ async fn run_pipeline( // Safety: to_w was just opened above, was not used until now, and can't // be used after this because we rebound the variable let mut to_w = unsafe { async_std::fs::File::from_raw_fd(to_w) }; + to_w.write_all(&bincode::serialize(pipeline).unwrap()) + .await + .unwrap(); to_w.write_all(&env.as_bytes()).await.unwrap(); drop(to_w); -- cgit v1.2.3-54-g00ecf