Skip to content
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

fix(cli): reduce flicker in spinner render function #4835

Merged
merged 4 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cli/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@
// Updates the spinner after the given interval.
const updateFrame = () => {
const color = this.#color ?? "";
Deno.stdout.writeSync(LINE_CLEAR);
const frame = encoder.encode(
noColor
? this.#spinner[i] + " " + this.message
: color + this.#spinner[i] + COLOR_RESET + " " + this.message,
);
Deno.stdout.writeSync(frame);
// call writeSync once to reduce flickering
const writeData = new Uint8Array(LINE_CLEAR.length + frame.length);
writeData.set(LINE_CLEAR);
writeData.set(frame, LINE_CLEAR.length);
Deno.stdout.writeSync(writeData);

Check warning on line 148 in cli/spinner.ts

View check run for this annotation

Codecov / codecov/patch

cli/spinner.ts#L144-L148

Added lines #L144 - L148 were not covered by tests
Comment on lines +145 to +148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative using concat():

Suggested change
const writeData = new Uint8Array(LINE_CLEAR.length + frame.length);
writeData.set(LINE_CLEAR);
writeData.set(frame, LINE_CLEAR.length);
Deno.stdout.writeSync(writeData);
Deno.stdout.writeSync(concat([LINE_CLEAR, frame]));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that this was in a separate package. I'm not sure it's worth causing another package load to occur when importing this module.

i = (i + 1) % this.#spinner.length;
};
this.#intervalId = setInterval(updateFrame, this.#interval);
Expand Down