add 0 support
This commit is contained in:
		@@ -46,7 +46,7 @@ impl Env {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub type Args<'a, 'b> = &'b [String];
 | 
					pub type Args<'a, 'b> = &'b [String];
 | 
				
			||||||
trait Run = Fn(Args, &mut Env);
 | 
					trait Run = Fn(Args, &mut Env) -> i32;
 | 
				
			||||||
type Cmd = Box<dyn Run>;
 | 
					type Cmd = Box<dyn Run>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Command {
 | 
					pub struct Command {
 | 
				
			||||||
@@ -69,7 +69,8 @@ impl Command {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    pub fn call(&self, words: Args, env: &mut Env) {
 | 
					    pub fn call(&self, words: Args, env: &mut Env) {
 | 
				
			||||||
        let x = &self.cmd;
 | 
					        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> {
 | 
					    pub fn choose<'a>(text: &str, choices: &'a [Self]) -> Option<&'a Self> {
 | 
				
			||||||
@@ -125,10 +126,6 @@ impl Command {
 | 
				
			|||||||
                                        .envs(env.as_env())
 | 
					                                        .envs(env.as_env())
 | 
				
			||||||
                                        .output()
 | 
					                                        .output()
 | 
				
			||||||
                                        .expect("Failed to call external program");
 | 
					                                        .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() {
 | 
					                                    if !out.stdout.is_empty() {
 | 
				
			||||||
                                        let output = String::from_utf8_lossy(&out.stdout);
 | 
					                                        let output = String::from_utf8_lossy(&out.stdout);
 | 
				
			||||||
                                        println!("{output}");
 | 
					                                        println!("{output}");
 | 
				
			||||||
@@ -137,6 +134,7 @@ impl Command {
 | 
				
			|||||||
                                        let errout = String::from_utf8_lossy(&out.stderr);
 | 
					                                        let errout = String::from_utf8_lossy(&out.stderr);
 | 
				
			||||||
                                        println!("{errout}");
 | 
					                                        println!("{errout}");
 | 
				
			||||||
                                    }
 | 
					                                    }
 | 
				
			||||||
 | 
					                                    out.status.code().unwrap_or(0)
 | 
				
			||||||
                                }),
 | 
					                                }),
 | 
				
			||||||
                            );
 | 
					                            );
 | 
				
			||||||
                            v.push(cmd);
 | 
					                            v.push(cmd);
 | 
				
			||||||
@@ -171,6 +169,7 @@ impl Command {
 | 
				
			|||||||
                        .intersperse(" ".to_string())
 | 
					                        .intersperse(" ".to_string())
 | 
				
			||||||
                        .collect::<String>();
 | 
					                        .collect::<String>();
 | 
				
			||||||
                    println!("{msg}");
 | 
					                    println!("{msg}");
 | 
				
			||||||
 | 
					                    0
 | 
				
			||||||
                }),
 | 
					                }),
 | 
				
			||||||
            ),
 | 
					            ),
 | 
				
			||||||
            Command::init(
 | 
					            Command::init(
 | 
				
			||||||
@@ -182,6 +181,7 @@ impl Command {
 | 
				
			|||||||
                        Box::new(|_, _| {
 | 
					                        Box::new(|_, _| {
 | 
				
			||||||
                            // how do you know it's not random?
 | 
					                            // how do you know it's not random?
 | 
				
			||||||
                            println!("8");
 | 
					                            println!("8");
 | 
				
			||||||
 | 
					                            0
 | 
				
			||||||
                        }),
 | 
					                        }),
 | 
				
			||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                    Command::init(
 | 
					                    Command::init(
 | 
				
			||||||
@@ -190,11 +190,13 @@ impl Command {
 | 
				
			|||||||
                        Box::new(|_, _| {
 | 
					                        Box::new(|_, _| {
 | 
				
			||||||
                            // how do you know it's not random?
 | 
					                            // how do you know it's not random?
 | 
				
			||||||
                            println!("hello world");
 | 
					                            println!("hello world");
 | 
				
			||||||
 | 
					                            0
 | 
				
			||||||
                        }),
 | 
					                        }),
 | 
				
			||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                Box::new(|args, _| {
 | 
					                Box::new(|args, _| {
 | 
				
			||||||
                    println!("random's debug: {:?}", args);
 | 
					                    println!("random's debug: {:?}", args);
 | 
				
			||||||
 | 
					                    0
 | 
				
			||||||
                }),
 | 
					                }),
 | 
				
			||||||
            ),
 | 
					            ),
 | 
				
			||||||
        ];
 | 
					        ];
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,7 @@ fn main() {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    let mut env= Env::new();
 | 
					    let mut env= Env::new();
 | 
				
			||||||
    env.set("HOME", "/a/fake/dir");
 | 
					    env.set("HOME", "/a/fake/dir");
 | 
				
			||||||
 | 
					    env.set("?", "0");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let commands = &Command::all_commands(&env);
 | 
					    let commands = &Command::all_commands(&env);
 | 
				
			||||||
    println!("Loaded {} commands", commands.len());
 | 
					    println!("Loaded {} commands", commands.len());
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user