| 1 | #![cfg(feature = "std")] |
| 2 | #![cfg(feature = "expand")] |
| 3 | |
| 4 | use io_process::{ |
| 5 | command::Command, |
| 6 | coroutines::spawn_then_wait_with_output::{ |
| 7 | SpawnThenWaitWithOutput, SpawnThenWaitWithOutputResult, |
| 8 | }, |
| 9 | runtimes::std::handle, |
| 10 | }; |
| 11 | |
| 12 | fn main() { |
| 13 | env_logger::init(); |
| 14 | |
| 15 | let mut command = Command::new("echo"); |
| 16 | command.expand = true; |
| 17 | command.env("HELLO", "world"); |
| 18 | command.arg("-n").arg("$USER").arg("$HELLO").arg("~"); |
| 19 | |
| 20 | println!("spawn: {command:#?}"); |
| 21 | println!(); |
| 22 | |
| 23 | let mut arg = None; |
| 24 | let mut spawn = SpawnThenWaitWithOutput::new(command); |
| 25 | |
| 26 | let output = loop { |
| 27 | match spawn.resume(arg.take()) { |
| 28 | SpawnThenWaitWithOutputResult::Ok(output) => break output, |
| 29 | SpawnThenWaitWithOutputResult::Io(io) => arg = Some(handle(io).unwrap()), |
| 30 | SpawnThenWaitWithOutputResult::Err(err) => panic!("{err}"), |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | println!("output: {output:#?}") |
| 35 | } |