From 5e733a4e14c42189ef87c1b265c47fff74decac0 Mon Sep 17 00:00:00 2001 From: mitchell Date: Sun, 3 May 2026 22:15:27 -0400 Subject: [PATCH] refactor: spinner handling and markdown line wrapping --- src/render/index.ts | 10 ++-------- src/render/markdown.ts | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/render/index.ts b/src/render/index.ts index b3b5f69..de6ca96 100644 --- a/src/render/index.ts +++ b/src/render/index.ts @@ -89,18 +89,14 @@ export default class Renderer { this.resetCounts(); const spin = spinner({ withGuide: false, indicator: "timer" }); - let spinning = false; if (!this.isPlain) { - spinning = true; spin.start("Waiting for response"); } for await (const part of stream) { switch (part.type) { case "reasoning-start": { - if (spinning) { - spin.clear(); - } + spin.clear(); if (supportsColorStderr) this.write( `${styles.dim.open}${styles.italic.open}`, @@ -132,9 +128,7 @@ export default class Renderer { break; } case "text-start": { - if (spinning) { - spin.clear(); - } + spin.clear(); this.write("\n"); break; } diff --git a/src/render/markdown.ts b/src/render/markdown.ts index ac855a8..bd4d777 100644 --- a/src/render/markdown.ts +++ b/src/render/markdown.ts @@ -46,15 +46,19 @@ export async function renderMarkdown( chalk.greenBright, ]; const color = colors[level - 1] ?? chalk.white; - return color.bold(`${"#".repeat(level)} ${children}\n\n`); + return Bun.wrapAnsi( + color.bold(`${"#".repeat(level)} ${children}\n\n`), + columns, + ); }, - paragraph: (children) => `${children}\n\n`, + paragraph: (children) => Bun.wrapAnsi(`${children}\n\n`, columns), blockquote: (children) => { const lines = children.split("\n"); - return lines + const toRender = lines .map((line) => `${chalk.dim("> ")}${line}`) .join("\n") .replace(/\n$/, "\n\n"); + return Bun.wrapAnsi(toRender, columns); }, code: (children, meta) => { let toRender = children; @@ -73,7 +77,10 @@ export async function renderMarkdown( return `\`\`\`${chalk.blue(meta?.language ?? "")}\n${toRender}\`\`\`\n`; }, list: (children, { depth }) => - `${depth > 0 ? "\n" : ""}${children.trimEnd()}${depth === 0 ? "\n\n" : ""}`, + Bun.wrapAnsi( + `${depth > 0 ? "\n" : ""}${children.trimEnd()}${depth === 0 ? "\n\n" : ""}`, + columns, + ), listItem: (children, { index, depth, ordered, start, checked }) => { const indent = " ".repeat(depth); let marker = ""; @@ -119,6 +126,5 @@ export async function renderMarkdown( html: () => "", }); - const replaced = rendered.replace(/\n\n$/, "\n"); - return Bun.wrapAnsi(replaced, columns, { trim: false }); + return rendered.replace(/\n\n$/, "\n"); }