From 13f8a089ddee0d6b0ecb8b2d172573722e0d641b Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Wed, 11 Dec 2024 22:58:05 +0100 Subject: [PATCH] Add the ability to add argument to the interpreter When you have a script that you want to run with a custom interpreter you can now add arguments to the interpreter. --- README.md | 18 ++++++++++++++++-- src/main.rs | 31 ++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b4ba362..58b8fa3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ ![image](https://github.com/chmouel/raffi/assets/98980/04d6af0f-2a80-47d5-a2ec-95443a629305) -*(This uses my Fuzzel configuration, see below for more details)* +> [!NOTE] +> This uses my Fuzzel configuration, see below for more details ## Description @@ -177,7 +178,20 @@ hello_script: import os print("hello world and show me your env") print(os.environ) - description: "Hello Script" + description: "Hello Python script" + icon: "script" +``` + +If you want to add some specific arguments to the interpreter, you can do it like this: + +```yaml +hello_script: + binary: sh + args: ["-xv"] + script: | + echo "hello world and show me your env" + env + description: "Hello debug" icon: "script" ``` diff --git a/src/main.rs b/src/main.rs index 50c9c4b..bba56ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,6 +111,13 @@ fn is_valid_config(mc: &mut RaffiConfig, args: &Args) -> bool { return false; } mc.binary = Some(args.default_script_shell.clone()); + if let Some(binary_args) = &mc.args { + mc.binary = Some(format!( + "{} {}", + mc.binary.as_deref().unwrap_or_default(), + binary_args.join(" ") + )); + } } else if let Some(binary) = &mc.binary { if !find_binary(binary) { return false; @@ -247,6 +254,10 @@ fn make_fuzzel_input(rafficonfigs: &[RaffiConfig], no_icons: bool) -> Result Result<()> { + let interpreter_parts: Vec<&str> = interpreter.split_whitespace().collect(); + let interpreter_cmd = interpreter_parts.get(0).context("No interpreter found")?; + let interpreter_args = &interpreter_parts[1..]; + if args.print_only { if let Some(script) = &mc.script { println!("#!/usr/bin/env {}\n{}", interpreter, script); @@ -264,17 +275,19 @@ fn execute_chosen_command(mc: &RaffiConfig, args: &Args, interpreter: &str) -> R tempfile::NamedTempFile::new().context("Failed to create temp script file")?; writeln!(temp_script, "#!/usr/bin/env {}\n{}", interpreter, script) .context("Failed to write to temp script file")?; - let mut child = Command::new("/usr/bin/env") - .arg(interpreter) - .arg(temp_script.path()) - .spawn() - .context("cannot launch script")?; + let mut command = Command::new("/usr/bin/env"); + command + .arg(interpreter_cmd) + .args(interpreter_args) + .arg(temp_script.path()); + let mut child = command.spawn().context("cannot launch script")?; child.wait().context("cannot wait for child")?; } else { - let mut child = Command::new(mc.binary.as_deref().context("Binary not found")?) - .args(mc.args.as_deref().unwrap_or(&[])) - .spawn() - .context("cannot launch binary")?; + let mut command = Command::new(mc.binary.as_deref().context("Binary not found")?); + if let Some(binary_args) = &mc.args { + command.args(binary_args); + } + let mut child = command.spawn().context("cannot launch binary")?; child.wait().context("cannot wait for child")?; } Ok(())