Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spawn task for read and write to lang process #365

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/shrs_mux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ repository.workspace = true
[dependencies]
shrs = { path = "../../crates/shrs", version = "^0.0.3" }
clap = { version = "4.1", features = ["derive"] }
tokio = { version = "1.35", features = ["full"] }
272 changes: 0 additions & 272 deletions plugins/shrs_mux/src/lang.rs

This file was deleted.

86 changes: 86 additions & 0 deletions plugins/shrs_mux/src/lang/bash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::{
cell::RefCell,
collections::HashMap,
fmt::format,
io::{BufRead, BufReader, Read, Write},
ops::Add,
os::unix::process::ExitStatusExt,
process::{Child, ChildStderr, ChildStdin, ChildStdout, Command, ExitStatus, Stdio},
sync::Arc,
};

use shrs::{
lang::{Lexer, Token},
prelude::*,
};

use crate::{
interpreter::{read_err, read_out},
MuxState,
};

pub struct BashLang {
instance: RefCell<Child>,
}

impl BashLang {
pub fn new() -> Self {
Self {
instance: RefCell::new(
Command::new("bash")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to start bash process"),
),
}
}
}

impl Lang for BashLang {
fn eval(
&self,
sh: &Shell,
ctx: &mut Context,
rt: &mut Runtime,
cmd: String,
) -> shrs::anyhow::Result<CmdOutput> {
let mut instance = self.instance.borrow_mut();
let stdin = instance.stdin.as_mut().expect("Failed to open stdin");

let cd_statement = format!("cd {}\n", rt.working_dir.to_string_lossy());

stdin
.write_all(cd_statement.as_bytes())
.expect("unable to set var");

for (k, v) in rt.env.iter() {
let export_statement = format!("export {}={:?}\n", k, v);
stdin
.write_all(export_statement.as_bytes())
.expect("unable to set var");
}
stdin
.write_all((cmd + ";echo $?'\x1A'; echo '\x1A' >&2\n").as_bytes())
.expect("Bash command failed");

let stdout_reader =
BufReader::new(instance.stdout.as_mut().expect("Failed to open stdout"));
let status = read_out(ctx, stdout_reader)?;

let stderr_reader =
BufReader::new(instance.stderr.as_mut().expect("Failed to open stdout"));
read_err(ctx, stderr_reader)?;

Ok(CmdOutput::new(status))
}

fn name(&self) -> String {
"bash".to_string()
}

fn needs_line_check(&self, cmd: String) -> bool {
false
}
}
Loading