From 9cc1a64a91683abdf93a0b6b05802b63726f9d3f Mon Sep 17 00:00:00 2001 From: Oliver Atkinson Date: Tue, 26 Nov 2024 11:52:23 -0700 Subject: [PATCH] add 0 support --- src/command.rs | 14 ++++++++------ src/main.rs | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/command.rs b/src/command.rs index 42ecf5e..b534e26 100644 --- a/src/command.rs +++ b/src/command.rs @@ -46,7 +46,7 @@ impl Env { } pub type Args<'a, 'b> = &'b [String]; -trait Run = Fn(Args, &mut Env); +trait Run = Fn(Args, &mut Env) -> i32; type Cmd = Box; pub struct Command { @@ -69,7 +69,8 @@ impl Command { pub fn call(&self, words: Args, env: &mut Env) { let x = &self.cmd; - x(words, env) + let exit_code = x(words, env); + env.set("?", &exit_code.to_string()); } pub fn choose<'a>(text: &str, choices: &'a [Self]) -> Option<&'a Self> { @@ -125,10 +126,6 @@ impl Command { .envs(env.as_env()) .output() .expect("Failed to call external program"); - // TODO load into env - if let Some(status) = out.status.code() { - env.set("?", &status.to_string()); - } if !out.stdout.is_empty() { let output = String::from_utf8_lossy(&out.stdout); println!("{output}"); @@ -137,6 +134,7 @@ impl Command { let errout = String::from_utf8_lossy(&out.stderr); println!("{errout}"); } + out.status.code().unwrap_or(0) }), ); v.push(cmd); @@ -171,6 +169,7 @@ impl Command { .intersperse(" ".to_string()) .collect::(); println!("{msg}"); + 0 }), ), Command::init( @@ -182,6 +181,7 @@ impl Command { Box::new(|_, _| { // how do you know it's not random? println!("8"); + 0 }), ), Command::init( @@ -190,11 +190,13 @@ impl Command { Box::new(|_, _| { // how do you know it's not random? println!("hello world"); + 0 }), ), ], Box::new(|args, _| { println!("random's debug: {:?}", args); + 0 }), ), ]; diff --git a/src/main.rs b/src/main.rs index fd83d27..9ff7526 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ fn main() { let mut env= Env::new(); env.set("HOME", "/a/fake/dir"); + env.set("?", "0"); let commands = &Command::all_commands(&env); println!("Loaded {} commands", commands.len());