xonecas.io
6533
Yo

yoctomarkdown / commit bb0c575

fix: properly clear multi-line partial output during streaming

This resolves the issue where text longer than the terminal width was being duplicated multiple times on the console during a live stream.

Also includes newly added custom command.

xonecas2026-03-15 bb0c575091457b14faddf225f6887bd4e8a5eb67 parent 446ada8

3 files changed

.agents/release.md+31−0
@@ -0,0 +1,31 @@
1+---
2+description: Create a new tagged npm release (patch/minor/major)
3+---
4+
5+Create a new npm release for this repo. The bump type is: $1 (default to "patch" if not provided).
6+
7+Steps to follow exactly:
8+
9+1. Normalize bump type to one of `patch|minor|major`; if empty, use `patch`.
10+2. Verify git state before doing anything:
11+ - current branch must be `main`
12+ - working tree must be clean
13+ - run `git fetch origin --tags` and ensure `HEAD` equals `origin/main`
14+ If any check fails, stop and report.
15+3. Run the full repo check suit — stop and report failures.
16+4. Read `package.json` and capture both `name` and current `version`.
17+5. Compute the next semver version by bumping the selected part.
18+6. Guardrails before mutating files:
19+ - ensure git tag `v<new-version>` does not exist locally or on origin
20+ - ensure npm version does not already exist: `npm view <package-name>@<new-version> version`
21+ If either exists, stop and report.
22+7. Update the `version` field in `package.json` to `<new-version>`.
23+8. Run `bun run build`.
24+9. Commit all staged and unstaged changes with message: `chore: release v<new-version>`.
25+10. Create annotated tag: `git tag -a v<new-version> -m "<new-version>\n<small-changelog>"`.
26+11. Push explicitly to main and the exact tag:
27+ - `git push origin main`
28+ - `git push origin v<new-version>`
29+12. Post-push verification: run `npm view <package-name>@<new-version> version`.
30+ - If it already exists, report that publish already happened and do **not** ask user to publish again.
31+ - If it does not exist, ask user to run `npm publish` (passkey auth step).
src/index.ts+24−1
@@ -128,6 +128,7 @@ export function createHighlighter(options?: Options): Highlighter {
128128 }
129129
130130 let hasPartial = false;
131+ let partialLength = 0;
131132
132133 return {
133134 write(chunk: string | Uint8Array): string {
@@ -140,7 +141,15 @@ export function createHighlighter(options?: Options): Highlighter {
140141
141142 let out = "";
142143 if (hasPartial) {
144+ const columns =
145+ typeof process !== "undefined" && process.stdout?.columns
146+ ? process.stdout.columns
147+ : 80;
148+ const linesToClear = Math.ceil(partialLength / columns) || 1;
143149 out += "\x1b[2K\r";
150+ if (linesToClear > 1) {
151+ out += "\x1b[1A\x1b[2K".repeat(linesToClear - 1);
152+ }
144153 }
145154
146155 if (lines.length > 0) {
@@ -150,8 +159,10 @@ export function createHighlighter(options?: Options): Highlighter {
150159 if (buffer.length > 0) {
151160 out += parseLine(buffer, true);
152161 hasPartial = true;
162+ partialLength = buffer.length;
153163 } else {
154164 hasPartial = false;
165+ partialLength = 0;
155166 }
156167
157168 return out;
@@ -160,7 +171,19 @@ export function createHighlighter(options?: Options): Highlighter {
160171 if (buffer.length > 0) {
161172 const out = parseLine(buffer, false);
162173 buffer = "";
163- return (hasPartial ? "\x1b[2K\r" : "") + out;
174+ if (hasPartial) {
175+ const columns =
176+ typeof process !== "undefined" && process.stdout?.columns
177+ ? process.stdout.columns
178+ : 80;
179+ const linesToClear = Math.ceil(partialLength / columns) || 1;
180+ let clearSeq = "\x1b[2K\r";
181+ if (linesToClear > 1) {
182+ clearSeq += "\x1b[1A\x1b[2K".repeat(linesToClear - 1);
183+ }
184+ return clearSeq + out;
185+ }
186+ return out;
164187 }
165188 return "";
166189 },
tests/index.test.ts+30−0
@@ -81,4 +81,34 @@ describe("Programmatic Usage: createHighlighter (Streaming)", () => {
8181
8282 expectStreamMatch(res, markdown);
8383 });
84+
85+ test("should clear multiple lines when wrapped", () => {
86+ // Mock columns
87+ const originalColumns = process.stdout.columns;
88+ Object.defineProperty(process.stdout, "columns", {
89+ value: 10,
90+ configurable: true,
91+ });
92+
93+ const highlighter = createHighlighter({ theme: "none" });
94+ // 25 chars, columns = 10, so it takes 3 visual lines
95+ const chunk1 = "1234567890123456789012345";
96+ const chunk2 = "67890\n";
97+
98+ let res = highlighter.write(chunk1);
99+ res += highlighter.write(chunk2);
100+ res += highlighter.end();
101+
102+ // The first chunk is written, then cleared. Since it takes 3 lines,
103+ // clear sequence should have \x1b[1A\x1b[2K repeated twice.
104+ expect(res).toContain("\x1b[1A\x1b[2K\x1b[1A\x1b[2K");
105+
106+ // Restore
107+ if (originalColumns !== undefined) {
108+ Object.defineProperty(process.stdout, "columns", {
109+ value: originalColumns,
110+ configurable: true,
111+ });
112+ }
113+ });
84114 });