Repository navigation
fix: stop accumulating prevLineCount between frames #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import assert from "node:assert"; | ||
| import { afterEach, beforeEach, describe, it } from "vitest"; | ||
| import LogUpdate from "../src/utils/log-update"; | ||
|
|
||
| const ERASE_LINE = "\u001B[2K"; | ||
| const countErase = (chunk) => chunk.split(ERASE_LINE).length - 1; | ||
|
|
||
| describe("log-update", () => { | ||
| let chunks; | ||
| let originalWrite; | ||
|
|
||
| beforeEach(() => { | ||
| chunks = []; | ||
| originalWrite = process.stderr.write; | ||
|
|
||
| process.stderr.write = function (data) { | ||
| chunks.push(String(data)); | ||
| return true; | ||
| }; | ||
|
|
||
| Object.defineProperty(process.stderr, "columns", { | ||
| value: 80, | ||
| configurable: true, | ||
| writable: true, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.stderr.write = originalWrite; | ||
| }); | ||
|
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Stop each The first test leaves its stdout wrapper installed. In the second test, 🤖 Prompt for AI Agents |
||
|
|
||
| it("erases exactly the previous frame height after external output", () => { | ||
| const logUpdate = new LogUpdate(); | ||
|
|
||
| logUpdate.render("line1\nline2"); | ||
| const frameHeight = logUpdate.prevLineCount; | ||
| assert.ok(frameHeight > 0, "first frame should occupy at least one line"); | ||
| assert.equal(countErase(chunks[0]), 0, "first frame has nothing to erase"); | ||
|
|
||
| // Simulate third-party output (e.g. console.log) landing between renders. | ||
| chunks.length = 0; | ||
| process.stderr.write("external log\n"); | ||
| logUpdate.render("line1\nline2"); | ||
|
|
||
| assert.equal( | ||
| countErase(chunks.at(-1)), | ||
| frameHeight, | ||
| "second frame must erase exactly the previous frame height", | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps the erase height in sync across repeated external output", () => { | ||
| const logUpdate = new LogUpdate(); | ||
|
|
||
| logUpdate.render("progress"); | ||
| let expectedHeight = logUpdate.prevLineCount; | ||
|
|
||
| for (let index = 0; index < 5; index++) { | ||
| chunks.length = 0; | ||
| process.stderr.write(`log ${index}\n`); | ||
| logUpdate.render("progress"); | ||
|
|
||
| assert.equal( | ||
| countErase(chunks.at(-1)), | ||
| expectedHeight, | ||
| `frame ${index + 2} must erase exactly the previous frame height`, | ||
| ); | ||
|
|
||
| expectedHeight = logUpdate.prevLineCount; | ||
| } | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: unjs/webpackbar
Length of output: 4991
🏁 Script executed:
Repository: unjs/webpackbar
Length of output: 10515
Include intervening newlines in the next erase.
After
render("line1\nline2"),prevLineCountis 3. Ifprocess.stderr.write("log\n")runs before the next render, it advances the cursor by one row. Erasing only 3 rows then leavesline1visible.Track intervening newlines separately. Add that count to the next erase, then reset it after the render. The current render recomputes
prevLineCountfrom the payload, so restoring an increment toprevLineCountin_onDatawould not create the claimed cumulative offset. Update the regression test to check the resulting display, not only the erase-code count.🐛 Suggested fix
🤖 Prompt for AI Agents