-
Notifications
You must be signed in to change notification settings - Fork 185
feat: rework spinner to use Prompt api
#479
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
Open
43081j
wants to merge
10
commits into
main
Choose a base branch
from
good-spinner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f95d408
wip: move spinner to use prompt API
43081j 4d48890
Merge branch 'main' into good-spinner
43081j c6c2768
test: add core tests
43081j 505e7ff
chore: fix rendering order
43081j 3d2ca73
fix: add guide prefix
43081j b53162c
fix: remove extra newline
43081j 6f4ae32
chore: update snaps and ci
43081j 247c9dc
test: default exit code to 0
43081j 780cb4c
chore: add changeset
43081j 22a798b
Merge branch 'main' into good-spinner
43081j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@clack/prompts": patch | ||
| "@clack/core": patch | ||
| --- | ||
|
|
||
| Rework the spinner prompt to use the `Prompt` base class for rendering. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import { settings } from '../utils/index.js'; | ||
| import Prompt, { type PromptOptions } from './prompt.js'; | ||
|
|
||
| const removeTrailingDots = (msg: string): string => { | ||
| return msg.replace(/\.+$/, ''); | ||
| }; | ||
|
|
||
| export interface SpinnerOptions extends PromptOptions<undefined, SpinnerPrompt> { | ||
| indicator?: 'dots' | 'timer'; | ||
| onCancel?: () => void; | ||
| cancelMessage?: string; | ||
| errorMessage?: string; | ||
| frames: string[]; | ||
| delay: number; | ||
| styleFrame?: (frame: string) => string; | ||
| } | ||
|
|
||
| export default class SpinnerPrompt extends Prompt<undefined> { | ||
| #isCancelled = false; | ||
| #isActive = false; | ||
| #startTime: number = 0; | ||
| #frameIndex: number = 0; | ||
| #indicatorTimer: number = 0; | ||
| #intervalId: ReturnType<typeof setInterval> | undefined; | ||
| #delay: number; | ||
| #frames: string[]; | ||
| #cancelMessage: string; | ||
| #errorMessage: string; | ||
| #onCancel?: () => void; | ||
| #message: string = ''; | ||
| #silentExit: boolean = false; | ||
| #exitCode: number = 0; | ||
|
|
||
| constructor(opts: SpinnerOptions) { | ||
| super(opts); | ||
| this.#delay = opts.delay; | ||
| this.#frames = opts.frames; | ||
| this.#cancelMessage = opts.cancelMessage ?? settings.messages.cancel; | ||
| this.#errorMessage = opts.errorMessage ?? settings.messages.error; | ||
| this.#onCancel = opts.onCancel; | ||
|
|
||
| this.on('cancel', () => this.#onExit(1)); | ||
| } | ||
|
|
||
| start(msg?: string): void { | ||
| if (this.#isActive) { | ||
| this.#reset(); | ||
| } | ||
| this.#isActive = true; | ||
| this.#message = removeTrailingDots(msg ?? ''); | ||
| this.#startTime = performance.now(); | ||
| this.#frameIndex = 0; | ||
| this.#indicatorTimer = 0; | ||
|
|
||
| if (Number.isFinite(this.#delay)) { | ||
| this.#intervalId = setInterval(() => this.#onInterval(), this.#delay); | ||
| } else { | ||
| this.render(); | ||
| } | ||
|
|
||
| this.#addGlobalListeners(); | ||
| } | ||
|
|
||
| stop(msg?: string, exitCode?: number, silent?: boolean): void { | ||
| if (!this.#isActive) { | ||
| return; | ||
| } | ||
|
|
||
| this.#reset(); | ||
| this.#silentExit = silent === true; | ||
| this.#exitCode = exitCode ?? 0; | ||
|
|
||
| if (msg !== undefined) { | ||
| this.#message = msg; | ||
| } | ||
|
|
||
| this.state = 'cancel'; | ||
|
Member
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. Should we do |
||
| this.render(); | ||
| this.close(); | ||
| } | ||
|
|
||
| get isCancelled(): boolean { | ||
| return this.#isCancelled; | ||
| } | ||
|
|
||
| get message(): string { | ||
| return this.#message; | ||
| } | ||
|
|
||
| set message(msg: string) { | ||
| this.#message = removeTrailingDots(msg); | ||
| } | ||
|
|
||
| get exitCode(): number | undefined { | ||
| return this.#exitCode; | ||
| } | ||
|
|
||
| get frameIndex(): number { | ||
| return this.#frameIndex; | ||
| } | ||
|
|
||
| get indicatorTimer(): number { | ||
| return this.#indicatorTimer; | ||
| } | ||
|
|
||
| get isActive(): boolean { | ||
| return this.#isActive; | ||
| } | ||
|
|
||
| get silentExit(): boolean { | ||
| return this.#silentExit; | ||
| } | ||
|
|
||
| getFormattedTimer(): string { | ||
| const duration = (performance.now() - this.#startTime) / 1000; | ||
| const min = Math.floor(duration / 60); | ||
| const secs = Math.floor(duration % 60); | ||
| return min > 0 ? `[${min}m ${secs}s]` : `[${secs}s]`; | ||
| } | ||
|
|
||
| #reset(): void { | ||
| this.#isActive = false; | ||
| this.#exitCode = 0; | ||
|
|
||
| if (this.#intervalId) { | ||
| clearInterval(this.#intervalId); | ||
| this.#intervalId = undefined; | ||
| } | ||
|
|
||
| this.#removeGlobalListeners(); | ||
| } | ||
|
|
||
| #onInterval(): void { | ||
| this.render(); | ||
|
|
||
| this.#frameIndex = this.#frameIndex + 1 < this.#frames.length ? this.#frameIndex + 1 : 0; | ||
| // indicator increase by 1 every 8 frames | ||
| this.#indicatorTimer = this.#indicatorTimer < 4 ? this.#indicatorTimer + 0.125 : 0; | ||
| } | ||
|
|
||
| #onProcessError: () => void = () => { | ||
| this.#onExit(2); | ||
| }; | ||
|
|
||
| #onProcessSignal: () => void = () => { | ||
| this.#onExit(1); | ||
| }; | ||
|
|
||
| #onExit: (exitCode: number) => void = (exitCode) => { | ||
| this.#exitCode = exitCode; | ||
| if (exitCode > 1) { | ||
| this.#message = this.#errorMessage; | ||
| } else { | ||
| this.#message = this.#cancelMessage; | ||
| } | ||
| this.#isCancelled = exitCode === 1; | ||
| if (this.#isActive) { | ||
| this.stop(this.#message, exitCode); | ||
| if (this.#isCancelled && this.#onCancel) { | ||
| this.#onCancel(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| #addGlobalListeners(): void { | ||
| // Reference: https://nodejs.org/api/process.html#event-uncaughtexception | ||
| process.on('uncaughtExceptionMonitor', this.#onProcessError); | ||
| // Reference: https://nodejs.org/api/process.html#event-unhandledrejection | ||
| process.on('unhandledRejection', this.#onProcessError); | ||
| // Reference Signal Events: https://nodejs.org/api/process.html#signal-events | ||
| process.on('SIGINT', this.#onProcessSignal); | ||
| process.on('SIGTERM', this.#onProcessSignal); | ||
| process.on('exit', this.#onExit); | ||
| } | ||
|
|
||
| #removeGlobalListeners(): void { | ||
| process.removeListener('uncaughtExceptionMonitor', this.#onProcessError); | ||
| process.removeListener('unhandledRejection', this.#onProcessError); | ||
| process.removeListener('SIGINT', this.#onProcessSignal); | ||
| process.removeListener('SIGTERM', this.#onProcessSignal); | ||
| process.removeListener('exit', this.#onExit); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { cursor } from 'sisteransi'; | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
| import { default as SpinnerPrompt } from '../../src/prompts/spinner.js'; | ||
| import { MockReadable } from '../mock-readable.js'; | ||
| import { MockWritable } from '../mock-writable.js'; | ||
|
|
||
| describe('SpinnerPrompt', () => { | ||
| let input: MockReadable; | ||
| let output: MockWritable; | ||
| let instance: SpinnerPrompt; | ||
|
|
||
| beforeEach(() => { | ||
| input = new MockReadable(); | ||
| output = new MockWritable(); | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| instance.stop(); | ||
| }); | ||
|
|
||
| test('renders render() result', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.prompt(); | ||
| expect(output.buffer).to.deep.equal([cursor.hide, 'foo']); | ||
| }); | ||
|
|
||
| describe('start', () => { | ||
| test('starts the spinner and updates frames', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.start('Loading'); | ||
| expect(instance.message).to.equal('Loading'); | ||
| expect(instance.frameIndex).to.equal(0); | ||
| expect(instance.indicatorTimer).to.equal(0); | ||
| vi.advanceTimersByTime(5); | ||
| expect(instance.frameIndex).to.equal(1); | ||
| expect(instance.indicatorTimer).to.equal(0.125); | ||
| vi.advanceTimersByTime(5); | ||
| expect(instance.frameIndex).to.equal(2); | ||
| expect(instance.indicatorTimer).to.equal(0.25); | ||
| vi.advanceTimersByTime(5); | ||
| expect(instance.frameIndex).to.equal(3); | ||
| expect(instance.indicatorTimer).to.equal(0.375); | ||
| }); | ||
|
|
||
| test('starting again resets the spinner', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.start('Loading'); | ||
| vi.advanceTimersByTime(10); | ||
| expect(instance.frameIndex).to.equal(2); | ||
| expect(instance.indicatorTimer).to.equal(0.25); | ||
| expect(instance.message).to.equal('Loading'); | ||
| instance.start('Loading again'); | ||
| expect(instance.message).to.equal('Loading again'); | ||
| expect(instance.frameIndex).to.equal(0); | ||
| expect(instance.indicatorTimer).to.equal(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('stop', () => { | ||
| test('stops the spinner and sets message', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.start('Loading'); | ||
| vi.advanceTimersByTime(10); | ||
| instance.stop('Done'); | ||
| expect(instance.message).to.equal('Canceled'); | ||
| expect(instance.isActive).to.equal(false); | ||
| expect(instance.isCancelled).to.equal(true); | ||
| expect(instance.silentExit).to.equal(false); | ||
| expect(instance.exitCode).to.equal(1); | ||
| expect(instance.state).to.equal('cancel'); | ||
| expect(output.buffer).to.deep.equal([cursor.hide, 'foo', '\n']); | ||
| }); | ||
|
|
||
| test('does nothing if spinner is not active', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.stop('Done'); | ||
| expect(instance.message).to.equal(''); | ||
| expect(instance.isActive).to.equal(false); | ||
| expect(instance.silentExit).to.equal(false); | ||
| expect(instance.exitCode).to.equal(0); | ||
| expect(instance.state).to.equal('initial'); | ||
| expect(output.buffer).to.deep.equal([]); | ||
| }); | ||
| }); | ||
|
|
||
| test('message strips trailing dots', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.start('Loading...'); | ||
| expect(instance.message).to.equal('Loading'); | ||
|
|
||
| instance.message = 'Still loading....'; | ||
| expect(instance.message).to.equal('Still loading'); | ||
| }); | ||
|
|
||
| describe('getFormattedTimer', () => { | ||
| test('formats timer correctly', () => { | ||
| instance = new SpinnerPrompt({ | ||
| input, | ||
| output, | ||
| frames: ['J', 'A', 'M', 'E', 'S'], | ||
| delay: 5, | ||
| render: () => 'foo', | ||
| }); | ||
| instance.start(); | ||
| expect(instance.getFormattedTimer()).to.equal('[0s]'); | ||
| vi.advanceTimersByTime(1500); | ||
| expect(instance.getFormattedTimer()).to.equal('[1s]'); | ||
| vi.advanceTimersByTime(600_000); | ||
| expect(instance.getFormattedTimer()).to.equal('[10m 1s]'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should be okay as an internal refactor, but since this technically alters the raw-mode behavior we might want to do this in a minor to be safe?