refactor: spinner handling and markdown line wrapping

This commit is contained in:
mitchell 2026-05-03 22:15:27 -04:00
parent 9da57fe2fc
commit 5e733a4e14
2 changed files with 14 additions and 14 deletions

View file

@ -89,18 +89,14 @@ export default class Renderer {
this.resetCounts(); this.resetCounts();
const spin = spinner({ withGuide: false, indicator: "timer" }); const spin = spinner({ withGuide: false, indicator: "timer" });
let spinning = false;
if (!this.isPlain) { if (!this.isPlain) {
spinning = true;
spin.start("Waiting for response"); spin.start("Waiting for response");
} }
for await (const part of stream) { for await (const part of stream) {
switch (part.type) { switch (part.type) {
case "reasoning-start": { case "reasoning-start": {
if (spinning) { spin.clear();
spin.clear();
}
if (supportsColorStderr) if (supportsColorStderr)
this.write( this.write(
`${styles.dim.open}${styles.italic.open}`, `${styles.dim.open}${styles.italic.open}`,
@ -132,9 +128,7 @@ export default class Renderer {
break; break;
} }
case "text-start": { case "text-start": {
if (spinning) { spin.clear();
spin.clear();
}
this.write("\n"); this.write("\n");
break; break;
} }

View file

@ -46,15 +46,19 @@ export async function renderMarkdown(
chalk.greenBright, chalk.greenBright,
]; ];
const color = colors[level - 1] ?? chalk.white; 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) => { blockquote: (children) => {
const lines = children.split("\n"); const lines = children.split("\n");
return lines const toRender = lines
.map((line) => `${chalk.dim("> ")}${line}`) .map((line) => `${chalk.dim("> ")}${line}`)
.join("\n") .join("\n")
.replace(/\n$/, "\n\n"); .replace(/\n$/, "\n\n");
return Bun.wrapAnsi(toRender, columns);
}, },
code: (children, meta) => { code: (children, meta) => {
let toRender = children; let toRender = children;
@ -73,7 +77,10 @@ export async function renderMarkdown(
return `\`\`\`${chalk.blue(meta?.language ?? "")}\n${toRender}\`\`\`\n`; return `\`\`\`${chalk.blue(meta?.language ?? "")}\n${toRender}\`\`\`\n`;
}, },
list: (children, { depth }) => 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 }) => { listItem: (children, { index, depth, ordered, start, checked }) => {
const indent = " ".repeat(depth); const indent = " ".repeat(depth);
let marker = ""; let marker = "";
@ -119,6 +126,5 @@ export async function renderMarkdown(
html: () => "", html: () => "",
}); });
const replaced = rendered.replace(/\n\n$/, "\n"); return rendered.replace(/\n\n$/, "\n");
return Bun.wrapAnsi(replaced, columns, { trim: false });
} }