@@ -440,8 +440,9 @@ export class MarkdownStreamRenderer {
440440
441441 this . isShowingIndicator = false
442442
443- // Clear the indicator line (3 characters max width: '●●●' + 2 spaces for padding)
444- process . stderr . write ( '\r' + ' ' . repeat ( 5 ) + '\r' )
443+ // Clear the indicator line completely and move cursor to beginning of line
444+ // Use enough spaces to clear the longest possible indicator (5 chars + padding)
445+ process . stderr . write ( '\r' + ' ' . repeat ( 10 ) + '\r' )
445446
446447 // Clear timer
447448 if ( this . loadingIndicatorTimer ) {
@@ -630,29 +631,16 @@ export class MarkdownStreamRenderer {
630631
631632 let rendered = this . md . render ( mdWithPlaceholders )
632633
633- codeBlockIndex = 0
634634 rendered = rendered . replace (
635- / C O D E _ B L O C K _ P L A C E H O L D E R _ ( \d + ) / g,
636- ( match , idx , offset , str ) => {
637- const block = codeBlocks [ codeBlockIndex ++ ] || ''
635+ // Match optional newlines before and after the placeholder
636+ / \n * C O D E _ B L O C K _ P L A C E H O L D E R _ ( \d + ) \n * / g,
637+ ( match , idx ) => {
638+ // Use the captured index from the placeholder for robustness
639+ const block = codeBlocks [ + idx ] || ''
638640 if ( ! block ) return ''
639641
640- // Count surrounding newlines
641- let leftNL = 0
642- for ( let i = offset - 1 ; i >= 0 && str [ i ] === '\n' ; i -- ) leftNL ++
643- let rightNL = 0
644- for (
645- let i = offset + match . length ;
646- i < str . length && str [ i ] === '\n' ;
647- i ++
648- )
649- rightNL ++
650-
651- // Add padding: ensure at least one blank line before and after code blocks
652- const needLeft = leftNL < 2 ? 2 - leftNL : 0
653- const needRight = rightNL < 2 ? 2 - rightNL : 0
654-
655- return `${ '\n' . repeat ( needLeft ) } ${ block } ${ '\n' . repeat ( needRight ) } `
642+ // Ensure exactly one newline before and after the block
643+ return `\n${ block . trimEnd ( ) } \n`
656644 } ,
657645 )
658646
0 commit comments