diff --git a/examples/function_piping.ion b/examples/function_piping.ion index 3c242a60b4a0e3b32c6581ed798a1b8bf109ac06..89aeaf1f6d7cd871b1eb7d31b7c435bd6fb4f79e 100644 --- a/examples/function_piping.ion +++ b/examples/function_piping.ion @@ -17,3 +17,15 @@ fn format_with pat end echo one two three four five | format_with "-" + +fn square + read x + echo $(( x * x )) +end + +fn mult x + read y + echo $(( y * x )) +end + +echo 5 | square | mult 3 diff --git a/src/parser/pipelines/mod.rs b/src/parser/pipelines/mod.rs index 27832c6163b3d0a082af2404887125c38c5ab2eb..b3039988c0a11abd27eb25063f53512df157cb0e 100644 --- a/src/parser/pipelines/mod.rs +++ b/src/parser/pipelines/mod.rs @@ -65,6 +65,13 @@ impl Pipeline { stdout.file = expand_string(stdout.file.as_str(), expanders, false).join(" "); } } + + pub fn requires_piping(&self) -> bool { + self.jobs.len() > 1 || + self.stdin != None || + self.stdout != None || + self.jobs.last().unwrap().kind == JobKind::Background + } } impl fmt::Display for Pipeline { diff --git a/src/shell/mod.rs b/src/shell/mod.rs index 22e2ed218b97bcf3b062c3673091b3de12f9756c..adb23a57302e842527d22f12798c43a8dd21904d 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -180,7 +180,7 @@ impl<'a> Shell<'a> { builtins.get(key) } { // Run the 'main' of the command and set exit_status - if pipeline.jobs.len() == 1 && pipeline.stdin == None && pipeline.stdout == None { + if !pipeline.requires_piping() { if self.flags & PRINT_COMMS != 0 { eprintln!("> {}", pipeline.to_string()); } let borrowed = &pipeline.jobs[0].args; let small: SmallVec<[&str; 4]> = borrowed.iter() @@ -192,7 +192,7 @@ impl<'a> Shell<'a> { } // Branch else if -> input == shell function and set the exit_status } else if let Some(function) = self.functions.get(&pipeline.jobs[0].command).cloned() { - if pipeline.jobs.len() == 1 { + if !pipeline.requires_piping() { let args: &[String] = pipeline.jobs[0].args.deref(); let args: Vec<&str> = args.iter().map(AsRef::as_ref).collect(); match function.execute(self, &args) {