feat: add --plain flag for unformatted terminal output
Rename StreamRenderer to Renderer and move stats rendering into the class. Support plain mode that skips ANSI formatting and glow preview when output is not a TTY.
This commit is contained in:
parent
e35e32f396
commit
9d599d98bf
2 changed files with 43 additions and 27 deletions
|
|
@ -1,19 +1,24 @@
|
|||
import os from "node:os";
|
||||
import type { WriteStream } from "node:tty";
|
||||
import type { AsyncIterableStream, TextStreamPart, ToolSet } from "ai";
|
||||
import type {
|
||||
AsyncIterableStream,
|
||||
LanguageModelUsage,
|
||||
TextStreamPart,
|
||||
ToolSet,
|
||||
} from "ai";
|
||||
import styles from "ansi-styles";
|
||||
import { supportsColorStderr } from "chalk";
|
||||
import { chalkStderr, supportsColorStderr } from "chalk";
|
||||
|
||||
const home = os.homedir();
|
||||
const reasoningFile = Bun.file(`${home}/.aicl_reasoning.md`);
|
||||
const reasoningWriter = reasoningFile.writer();
|
||||
|
||||
export default class StreamRenderer {
|
||||
export default class Renderer {
|
||||
private lineCount = 0;
|
||||
private charsSinceNL = 0;
|
||||
private memOk = true;
|
||||
|
||||
constructor() {
|
||||
constructor(private isPlain = false) {
|
||||
process.stdout.addListener("drain", () => {
|
||||
this.memOk = true;
|
||||
});
|
||||
|
|
@ -50,7 +55,7 @@ export default class StreamRenderer {
|
|||
this.charsSinceNL = this.charsSinceNL - columns;
|
||||
}
|
||||
|
||||
if (this.lineCount >= Math.floor(rows * 0.75)) {
|
||||
if (this.lineCount >= rows - 4) {
|
||||
this.moveCursor(-this.lineCount, pipe);
|
||||
this.clearScreenDown(pipe);
|
||||
this.resetCounts();
|
||||
|
|
@ -96,13 +101,17 @@ export default class StreamRenderer {
|
|||
}
|
||||
case "text-delta": {
|
||||
this.write(part.text);
|
||||
this.checkContainment(part.text);
|
||||
if (!this.isPlain) this.checkContainment(part.text);
|
||||
break;
|
||||
}
|
||||
case "text-end": {
|
||||
this.moveCursor(-this.lineCount - 1);
|
||||
this.clearScreenDown(process.stderr);
|
||||
this.resetCounts();
|
||||
if (!this.isPlain) {
|
||||
this.moveCursor(-this.lineCount - 1);
|
||||
this.clearScreenDown(process.stderr);
|
||||
this.resetCounts();
|
||||
} else {
|
||||
this.write("\n\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -110,4 +119,13 @@ export default class StreamRenderer {
|
|||
while (!this.memOk) await Bun.sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
renderStats(usage: LanguageModelUsage, start: number, stop: number) {
|
||||
const { inputTokens, outputTokens } = usage;
|
||||
const context = (inputTokens ?? 0) / 1000;
|
||||
const output = (outputTokens ?? 0) / 1000;
|
||||
const time = (stop - start) / 1000;
|
||||
const template = `Context: ${context.toFixed(1)}k, Output: ${output.toFixed(1)}k, Time: ${time.toFixed(2)}s\n`;
|
||||
process.stderr.write(chalkStderr.dim(template));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue