Skip to main content

extract_last_shell_block

Function extract_last_shell_block 

Source
pub fn extract_last_shell_block(text: &str) -> Option<String>
Expand description

Extract the last executable-shell fenced code block body from a message.

Why: When a message contains multiple shell blocks, “grab the most recently shown command” needs the last one, not the first. What: Walks the lines, tracks fence state, and returns Some(body) of the last completed bash/sh/zsh/fish block. Body lines are joined with \n. Returns None if no executable shell block is found, or the block was opened but never closed (an unclosed fence is discarded, not inferred to end at EOF — a truncated response should not be pasted as a partial command). Test: tests::extract_last_shell_block_finds_bash, tests::extract_last_shell_block_returns_last_when_multiple, tests::extract_last_shell_block_ignores_non_shell, tests::extract_last_shell_block_none_without_block, tests::extract_last_shell_block_unclosed_fence_returns_none, tests::extract_last_shell_block_closed_then_unclosed_returns_closed.