feat: use comark for final output

This commit is contained in:
mitchell 2026-04-17 21:21:48 -04:00
parent c1e03f2464
commit 8a23cf4d42
4 changed files with 169 additions and 11 deletions

View file

@ -1,5 +1,16 @@
import os from "node:os";
import type { WriteStream } from "node:tty";
import { render } from "@comark/ansi";
import highlight from "@comark/ansi/plugins/highlight";
import golang from "@shikijs/langs/go";
import java from "@shikijs/langs/java";
import kotlin from "@shikijs/langs/kotlin";
import objc from "@shikijs/langs/objc";
import proto from "@shikijs/langs/proto";
import swift from "@shikijs/langs/swift";
import xml from "@shikijs/langs/xml";
import kanagawaLotus from "@shikijs/themes/kanagawa-lotus";
import kanagawaWave from "@shikijs/themes/kanagawa-wave";
import type {
AsyncIterableStream,
LanguageModelUsage,
@ -58,7 +69,7 @@ export default class Renderer {
this.charsSinceNL = this.charsSinceNL - columns;
}
if (this.lineCount >= rows - 4) {
if (this.lineCount >= rows - 5) {
this.moveCursor(-this.lineCount, pipe);
this.clearScreenDown(pipe);
this.resetCounts();
@ -144,14 +155,23 @@ export default class Renderer {
process.stderr.write(chalkStderr.dim(template));
}
async renderFinal(textStream: AsyncIterableStream<string>) {
async renderFinal(text: PromiseLike<string>) {
if (this.isPlain) return;
const [width] = process.stdout.getWindowSize();
await Bun.spawn({
cmd: ["glow", "-w", `${width}`, "-"],
stdin: textStream,
stdout: "inherit",
}).exited;
const [columns] = process.stdout.getWindowSize();
this.write("\n");
this.write(
await render(await text, {
width: columns,
plugins: [
highlight({
themes: { dark: kanagawaWave, light: kanagawaLotus },
registerDefaultThemes: false,
languages: [golang, java, proto, xml, objc, swift, kotlin],
}),
],
}),
);
this.write("\n");
}
}