diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-01-03 23:12:08 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-01-03 23:12:08 -0500 |
commit | 1a281c87758ca559b4353fbd166dcd8e92a97f3a (patch) | |
tree | 32a01ce3487c98cafb325ab49fceff566a1a3d30 /src/env.rs | |
parent | 10717b2b1f29a1b35742ae0cb98e775ac55e248c (diff) | |
download | nbsh-1a281c87758ca559b4353fbd166dcd8e92a97f3a.tar.gz nbsh-1a281c87758ca559b4353fbd166dcd8e92a97f3a.zip |
propagate current directory changes back to the main process
Diffstat (limited to 'src/env.rs')
-rw-r--r-- | src/env.rs | 38 |
1 files changed, 34 insertions, 4 deletions
@@ -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<String>, 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<u8> { bincode::serialize(self).unwrap() } |