From 1a281c87758ca559b4353fbd166dcd8e92a97f3a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 3 Jan 2022 23:12:08 -0500 Subject: propagate current directory changes back to the main process --- src/env.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'src/env.rs') diff --git a/src/env.rs b/src/env.rs index 7b1d79c..1708c06 100644 --- a/src/env.rs +++ b/src/env.rs @@ -1,12 +1,12 @@ use serde::Deserialize as _; use std::os::unix::process::ExitStatusExt as _; -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub enum Env { V0(V0), } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct V0 { pipeline: Option, idx: usize, @@ -15,14 +15,16 @@ pub struct V0 { deserialize_with = "deserialize_status" )] latest_status: async_std::process::ExitStatus, + pwd: std::path::PathBuf, } impl Env { - pub fn new(idx: usize) -> Self { + pub fn new() -> Self { Self::V0(V0 { pipeline: None, - idx, + idx: 0, latest_status: std::process::ExitStatus::from_raw(0), + pwd: std::env::current_dir().unwrap(), }) } @@ -40,6 +42,12 @@ impl Env { } } + pub fn set_idx(&mut self, idx: usize) { + match self { + Self::V0(env) => env.idx = idx, + } + } + pub fn set_status(&mut self, status: async_std::process::ExitStatus) { match self { Self::V0(env) => { @@ -48,6 +56,20 @@ impl Env { } } + pub fn set_current_dir(&mut self, pwd: std::path::PathBuf) { + match self { + Self::V0(env) => { + env.pwd = pwd; + } + } + } + + pub fn current_dir(&self) -> &std::path::Path { + match self { + Self::V0(env) => &env.pwd, + } + } + pub fn pipeline(&self) -> Option<&str> { match self { Self::V0(env) => env.pipeline.as_deref(), @@ -60,6 +82,14 @@ impl Env { } } + pub fn apply(&self, cmd: &mut pty_process::Command) { + match self { + Self::V0(env) => { + cmd.current_dir(&env.pwd); + } + } + } + pub fn as_bytes(&self) -> Vec { bincode::serialize(self).unwrap() } -- cgit v1.2.3-54-g00ecf