Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(help): render a code block whose fence is never closed (#4183)
Worktrunk Bot0c8116a69bf5
diff --git a/src/md_help.rs b/src/md_help.rsindex c59e70a66..713dba413 100644--- a/src/md_help.rs+++ b/src/md_help.rs@@ -108,61 +108,14 @@ fn render_markdown(help: &str, width: Option<usize>, code_blocks: CodeBlocks) ->code_block_lines.clear();in_code_block = true;} else {- // Closing fence — render the collected code block. Flush mode- // dims each line with no gutter and no language-specific- // highlighting, so the description pane stays gutter-free and- // flush-left; the gutter mode quotes it in the house bar.- let content = code_block_lines.join("\n");- let formatted = if code_blocks == CodeBlocks::Flush {- // Flush code feeds the gutter-free `pr`/comments panes. Wrap each- // line to width (word-wrap, preserving indent) and dim every- // resulting piece, so a long line doesn't overflow the pane — and,- // when the comments pane re-quotes this in the house gutter, every- // wrapped piece already fits, keeping the gutter's own wrap a no-op- // and the dim consistent rather than landing only on the first line.- let dim = Style::new().dimmed();- code_block_lines- .iter()- .flat_map(|l| {- width.map_or_else(|| vec![l.to_string()], |w| wrap_styled_text(l, w))- })- .map(|piece| format!("{dim}{piece}{dim:#}"))- .collect::<Vec<_>>()- .join("\n")- } else {- match code_block_lang.as_str() {- "toml" => format_toml(&content),- "console" => {- // Strip `$ ` prompt from console blocks for copy-paste.- // The prefix is preserved in source for web docs.- let stripped = content- .lines()- .map(|l| l.strip_prefix("$ ").unwrap_or(l))- .collect::<Vec<_>>()- .join("\n");- // Captured `wt list` tables (chop_next_block) are chopped to- // width; hand-authored command sessions still word-wrap.- if chop_next_block {- format_bash_with_gutter_chopped(&stripped)- } else {- format_bash_with_gutter(&stripped)- }- }- "bash" | "sh" => format_bash_with_gutter(&content),- _ => {- // Dim the content before adding gutter (format_with_gutter- // doesn't style text; bash/toml formatters handle their own)- let dim = Style::new().dimmed();- let dimmed = code_block_lines- .iter()- .map(|l| format!("{dim}{l}{dim:#}"))- .collect::<Vec<_>>()- .join("\n");- format_with_gutter(&dimmed, None)- }- }- };- result.push_str(&formatted);+ // Closing fence — render the collected code block.+ result.push_str(&render_code_block(+ &code_block_lines,+ &code_block_lang,+ chop_next_block,+ width,+ code_blocks,+ ));result.push('\n');in_code_block = false;// A marker applies only to the block right after it.@@ -255,10 +208,88 @@ fn render_markdown(help: &str, width: Option<usize>, code_blocks: CodeBlocks) ->i += 1;}+ // An opening fence with no closing one: render what it collected rather+ // than dropping it. Help pages are authored and balanced, but the picker's+ // `pr` and comments panes render arbitrary forge markdown, where an+ // unterminated fence would otherwise take the rest of the body with it.+ if in_code_block {+ result.push_str(&render_code_block(+ &code_block_lines,+ &code_block_lang,+ chop_next_block,+ width,+ code_blocks,+ ));+ result.push('\n');+ }+// Color status symbols to match their descriptionscolorize_status_symbols(&result)}+/// Render one fenced code block's collected lines.+///+/// Flush mode dims each line with no gutter and no language-specific+/// highlighting, so the description pane stays gutter-free and flush-left; the+/// gutter mode quotes it in the house bar. Shared by the closing fence and by+/// the end-of-input flush, so an unterminated block renders exactly as the+/// same lines would with a closing fence.+fn render_code_block(+ code_block_lines: &[&str],+ code_block_lang: &str,+ chop_block: bool,+ width: Option<usize>,+ code_blocks: CodeBlocks,+) -> String {+ let content = code_block_lines.join("\n");+ if code_blocks == CodeBlocks::Flush {+ // Flush code feeds the gutter-free `pr`/comments panes. Wrap each+ // line to width (word-wrap, preserving indent) and dim every+ // resulting piece, so a long line doesn't overflow the pane — and,+ // when the comments pane re-quotes this in the house gutter, every+ // wrapped piece already fits, keeping the gutter's own wrap a no-op+ // and the dim consistent rather than landing only on the first line.+ let dim = Style::new().dimmed();+ return code_block_lines+ .iter()+ .flat_map(|l| width.map_or_else(|| vec![l.to_string()], |w| wrap_styled_text(l, w)))+ .map(|piece| format!("{dim}{piece}{dim:#}"))+ .collect::<Vec<_>>()+ .join("\n");+ }+ match code_block_lang {+ "toml" => format_toml(&content),+ "console" => {+ // Strip `$ ` prompt from console blocks for copy-paste.+ // The prefix is preserved in source for web docs.+ let stripped = content+ .lines()+ .map(|l| l.strip_prefix("$ ").unwrap_or(l))+ .collect::<Vec<_>>()+ .join("\n");+ // Captured `wt list` tables (chop_block) are chopped to+ // width; hand-authored command sessions still word-wrap.+ if chop_block {+ format_bash_with_gutter_chopped(&stripped)+ } else {+ format_bash_with_gutter(&stripped)+ }+ }+ "bash" | "sh" => format_bash_with_gutter(&content),+ _ => {+ // Dim the content before adding gutter (format_with_gutter+ // doesn't style text; bash/toml formatters handle their own)+ let dim = Style::new().dimmed();+ let dimmed = code_block_lines+ .iter()+ .map(|l| format!("{dim}{l}{dim:#}"))+ .collect::<Vec<_>>()+ .join("\n");+ format_with_gutter(&dimmed, None)+ }+ }+}+/// Render a markdown table using termimad (for help text, no indent)fn render_table(lines: &[&str], max_width: Option<usize>) -> String {render_table_with_termimad(lines, "", max_width)@@ -733,6 +764,30 @@ mod tests {");}+ #[test]+ fn test_unterminated_fence_still_renders_its_content() {+ // A PR description or comment can open a fence and never close it. The+ // panes render that markdown verbatim from the forge, so the block is+ // flushed at end of input rather than swallowing the rest of the body.+ let result = render_markdown_flush("before\n```sh\nwt list\nwt merge", None);+ assert_snapshot!(result, @"+ before+ [2mwt list[0m+ [2mwt merge[0m+ ");+ }++ #[test]+ fn test_unterminated_fence_keeps_the_gutter_form_too() {+ // The same flush in gutter mode, so both render contexts are pinned:+ // the block keeps its language handling rather than being dropped.+ let result = render_markdown_in_help("before\n```\ncode here");+ assert_snapshot!(result, @"+ before+ [107m [0m [2mcode here[0m+ ");+ }+#[test]fn test_render_markdown_in_help_plain_text() {let result = render_markdown_in_help("Just plain text");