use crate::CommandGroup;
use crate::core::ScriptBlock::{self, Command, Output, Prompt};
pub(crate) fn group_blocks(blocks: &[ScriptBlock]) -> Vec<CommandGroup> {
let mut result = Vec::new();
let mut i = 0;
if !blocks.is_empty() {
if let Output(initial_output) = &blocks[0] {
result.push(CommandGroup {
prompt: String::new(),
command: String::new(),
output: initial_output.clone(),
});
i = 1; }
}
while i < blocks.len() {
match &blocks[i] {
Prompt(prompt) => {
if i + 1 < blocks.len() {
if let Command(cmd) = &blocks[i + 1] {
let mut output = vec![];
if i + 2 < blocks.len() {
if let Output(out) = &blocks[i + 2] {
output = out.clone();
i += 3; } else {
i += 2; }
} else {
i += 2; }
result.push(CommandGroup {
prompt: prompt.clone(),
command: cmd.clone(),
output,
});
} else {
result.push(CommandGroup {
prompt: prompt.clone(),
command: String::new(),
output: vec![],
});
i += 1;
}
} else {
result.push(CommandGroup {
prompt: prompt.clone(),
command: String::new(),
output: vec![],
});
i += 1;
}
}
_ => {
i += 1;
}
}
}
result
}