refactor: move containment logic into function
This commit is contained in:
parent
1f59cbca7b
commit
8aaa7f6931
1 changed files with 31 additions and 36 deletions
|
|
@ -5,63 +5,58 @@ const moveCursor = (x: number, y: number) =>
|
||||||
const clearScreenDown = () =>
|
const clearScreenDown = () =>
|
||||||
new Promise<void>((resolve) => process.stdout.clearScreenDown(resolve));
|
new Promise<void>((resolve) => process.stdout.clearScreenDown(resolve));
|
||||||
|
|
||||||
|
let lineCount = 0;
|
||||||
|
let charsSinceNL = 0;
|
||||||
|
|
||||||
|
function resetCounts() {
|
||||||
|
lineCount = 0;
|
||||||
|
charsSinceNL = 0;
|
||||||
|
}
|
||||||
|
|
||||||
export async function renderStream(
|
export async function renderStream(
|
||||||
stream: AsyncIterableStream<TextStreamPart<ToolSet>>,
|
stream: AsyncIterableStream<TextStreamPart<ToolSet>>,
|
||||||
) {
|
) {
|
||||||
let lineCount = 0;
|
|
||||||
let charsSinceNL = 0;
|
|
||||||
const [numColumns, numRows] = process.stdout.getWindowSize();
|
const [numColumns, numRows] = process.stdout.getWindowSize();
|
||||||
|
resetCounts();
|
||||||
|
|
||||||
for await (const part of stream) {
|
for await (const part of stream) {
|
||||||
if (part.type === "reasoning-start") {
|
if (part.type === "reasoning-start") {
|
||||||
process.stdout.write("\u001b[2m\n");
|
process.stdout.write("\u001b[2m\n");
|
||||||
} else if (part.type === "reasoning-delta") {
|
} else if (part.type === "reasoning-delta") {
|
||||||
process.stdout.write(part.text);
|
process.stdout.write(part.text);
|
||||||
|
await checkContainment(part.text, numColumns, numRows);
|
||||||
charsSinceNL += part.text.length;
|
|
||||||
if (part.text.includes("\n")) {
|
|
||||||
lineCount += part.text.match(/\n/g)?.length ?? 1;
|
|
||||||
charsSinceNL = 0;
|
|
||||||
} else if (charsSinceNL >= numColumns) {
|
|
||||||
lineCount++;
|
|
||||||
charsSinceNL = charsSinceNL - numColumns;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lineCount >= numRows / 2) {
|
|
||||||
await moveCursor(0, -lineCount);
|
|
||||||
await clearScreenDown();
|
|
||||||
lineCount = 0;
|
|
||||||
charsSinceNL = 0;
|
|
||||||
}
|
|
||||||
} else if (part.type === "reasoning-end") {
|
} else if (part.type === "reasoning-end") {
|
||||||
process.stdout.write("\n\u001b[22m");
|
process.stdout.write("\n\u001b[22m");
|
||||||
await moveCursor(0, -lineCount - 2);
|
await moveCursor(0, -lineCount - 2);
|
||||||
await clearScreenDown();
|
await clearScreenDown();
|
||||||
lineCount = 0;
|
resetCounts();
|
||||||
charsSinceNL = 0;
|
|
||||||
} else if (part.type === "text-start") {
|
} else if (part.type === "text-start") {
|
||||||
process.stdout.write("\n");
|
process.stdout.write("\n");
|
||||||
} else if (part.type === "text-delta") {
|
} else if (part.type === "text-delta") {
|
||||||
process.stdout.write(part.text);
|
process.stdout.write(part.text);
|
||||||
|
await checkContainment(part.text, numColumns, numRows);
|
||||||
charsSinceNL += part.text.length;
|
} else if (part.type === "text-end") {
|
||||||
if (part.text.includes("\n")) {
|
await moveCursor(0, -lineCount - 1);
|
||||||
lineCount += part.text.match(/\n/g)?.length ?? 1;
|
await clearScreenDown();
|
||||||
charsSinceNL = 0;
|
resetCounts();
|
||||||
} else if (charsSinceNL >= numColumns) {
|
}
|
||||||
lineCount++;
|
}
|
||||||
charsSinceNL = charsSinceNL - numColumns;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lineCount >= numRows / 2) {
|
async function checkContainment(text: string, columns: number, rows: number) {
|
||||||
|
charsSinceNL += text.length;
|
||||||
|
if (text.includes("\n")) {
|
||||||
|
lineCount += text.match(/\n/g)?.length ?? 1;
|
||||||
|
charsSinceNL = 0;
|
||||||
|
} else if (charsSinceNL >= columns) {
|
||||||
|
lineCount++;
|
||||||
|
charsSinceNL = charsSinceNL - columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lineCount >= rows / 2) {
|
||||||
await moveCursor(0, -lineCount);
|
await moveCursor(0, -lineCount);
|
||||||
await clearScreenDown();
|
await clearScreenDown();
|
||||||
lineCount = 0;
|
lineCount = 0;
|
||||||
charsSinceNL = 0;
|
charsSinceNL = 0;
|
||||||
}
|
}
|
||||||
} else if (part.type === "text-end") {
|
|
||||||
await moveCursor(0, -lineCount - 1);
|
|
||||||
await clearScreenDown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue