refactor: move final rendering into Renderer class

This commit is contained in:
mitchell 2026-04-17 22:52:09 -04:00
parent cdfafc9d5f
commit c1e03f2464
2 changed files with 19 additions and 11 deletions

View file

@ -91,16 +91,9 @@ async function main() {
convo.messages.push({ role: "assistant", content: await text }); convo.messages.push({ role: "assistant", content: await text });
await convoFile.write(JSON.stringify(convo)); await convoFile.write(JSON.stringify(convo));
if (!plainOut) { await renderer.renderFinal(textStream);
const [width] = process.stdout.getWindowSize();
await Bun.spawn({
cmd: ["glow", "-w", `${width}`, "-"],
stdin: textStream,
stdout: "inherit",
}).exited;
}
renderer.renderStats(await totalUsage, start, stop); await renderer.renderStats(totalUsage, start, stop);
} }
await main(); await main();

View file

@ -131,12 +131,27 @@ export default class Renderer {
} }
} }
renderStats(usage: LanguageModelUsage, start: number, stop: number) { async renderStats(
const { inputTokens, outputTokens } = usage; usage: PromiseLike<LanguageModelUsage>,
start: number,
stop: number,
) {
const { inputTokens, outputTokens } = await usage;
const context = (inputTokens ?? 0) / 1000; const context = (inputTokens ?? 0) / 1000;
const output = (outputTokens ?? 0) / 1000; const output = (outputTokens ?? 0) / 1000;
const time = (stop - start) / 1000; const time = (stop - start) / 1000;
const template = `Context: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\n`; const template = `Context: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\n`;
process.stderr.write(chalkStderr.dim(template)); process.stderr.write(chalkStderr.dim(template));
} }
async renderFinal(textStream: AsyncIterableStream<string>) {
if (this.isPlain) return;
const [width] = process.stdout.getWindowSize();
await Bun.spawn({
cmd: ["glow", "-w", `${width}`, "-"],
stdin: textStream,
stdout: "inherit",
}).exited;
}
} }