-
-
Notifications
You must be signed in to change notification settings - Fork 213
feat(prompts): add accessible mode to spinner
#596
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
dreyfus92
wants to merge
1
commit into
main
Choose a base branch
from
a11y/prompts-01
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
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,5 @@ | ||
| --- | ||
| "@clack/prompts": minor | ||
| --- | ||
|
|
||
| Add accessible mode to `spinner`: when enabled via the `accessible` option, the global setting, or the `ACCESSIBLE` env var, the spinner emits static, append-only, screen-reader friendly output, a plain start line, a periodic "still working" heartbeat configurable via `accessibleInterval`, and a plain final line. Instead of animated in-place repaints. |
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,121 @@ | ||
| import { settings, updateSettings } from '@clack/core'; | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; | ||
| import * as prompts from '../src/index.js'; | ||
| import { MockWritable } from './test-utils.js'; | ||
|
|
||
| // biome-ignore lint/suspicious/noControlCharactersInRegex: matching ANSI escape codes is the point | ||
| const ANSI_REGEX = /\x1b\[/; | ||
|
|
||
| describe('spinner (accessible)', () => { | ||
| let originalAccessibleEnv: string | undefined; | ||
| let originalCIEnv: string | undefined; | ||
| let output: MockWritable; | ||
|
|
||
| beforeEach(() => { | ||
| originalAccessibleEnv = process.env.ACCESSIBLE; | ||
| originalCIEnv = process.env.CI; | ||
| delete process.env.ACCESSIBLE; | ||
| output = new MockWritable(); | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| process.env.ACCESSIBLE = originalAccessibleEnv; | ||
| process.env.CI = originalCIEnv; | ||
| settings.accessible = undefined; | ||
| }); | ||
|
|
||
| test('renders static append-only output with no decorations', () => { | ||
| const result = prompts.spinner({ output, accessible: true, withGuide: true }); | ||
|
|
||
| result.start('Loading'); | ||
| result.message('Installing'); | ||
| result.message('Linking'); | ||
| vi.advanceTimersByTime(30_000); | ||
| result.stop('Installed'); | ||
| vi.advanceTimersByTime(60_000); | ||
|
|
||
| expect(output.buffer).toEqual(['Loading\n', 'still working: Linking\n', 'Installed\n']); | ||
| expect(output.buffer.join('')).not.toMatch(ANSI_REGEX); | ||
| }); | ||
|
|
||
| test('falls back to plain status words when stopped without a message', () => { | ||
| for (const [end, line] of [ | ||
| ['stop', 'Done\n'], | ||
| ['cancel', 'Canceled\n'], | ||
| ['error', 'Something went wrong\n'], | ||
| ] as const) { | ||
| output = new MockWritable(); | ||
| const result = prompts.spinner({ output, accessible: true }); | ||
| result.start('Working'); | ||
| result[end](); | ||
| expect(output.buffer).toEqual(['Working\n', line]); | ||
| } | ||
| }); | ||
|
|
||
| test('accessibleInterval configures the heartbeat and 0 disables it', () => { | ||
| const result = prompts.spinner({ output, accessible: true, accessibleInterval: 5000 }); | ||
| result.start('a'); | ||
| vi.advanceTimersByTime(5000); | ||
| result.clear(); | ||
| expect(output.buffer).toEqual(['a\n', 'still working: a\n']); | ||
|
|
||
| output = new MockWritable(); | ||
| const silent = prompts.spinner({ output, accessible: true, accessibleInterval: 0 }); | ||
| silent.start('a'); | ||
| vi.advanceTimersByTime(120_000); | ||
| silent.clear(); | ||
| expect(output.buffer).toEqual(['a\n']); | ||
| }); | ||
|
|
||
| test('abort signal cancels with a plain line', () => { | ||
| const controller = new AbortController(); | ||
| const onCancel = vi.fn(); | ||
| const result = prompts.spinner({ | ||
| output, | ||
| accessible: true, | ||
| signal: controller.signal, | ||
| onCancel, | ||
| }); | ||
|
|
||
| result.start('Working'); | ||
| controller.abort(); | ||
|
|
||
| expect(output.buffer).toEqual(['Working\n', 'Canceled\n']); | ||
| expect(result.isCancelled).toBe(true); | ||
| expect(onCancel).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| test('accessible takes precedence over CI mode', () => { | ||
| process.env.CI = 'true'; | ||
| const result = prompts.spinner({ output, accessible: true }); | ||
|
|
||
| result.start('Loading'); | ||
| vi.advanceTimersByTime(1000); | ||
| result.stop('Done'); | ||
|
|
||
| expect(output.buffer).toEqual(['Loading\n', 'Done\n']); | ||
| }); | ||
|
|
||
| test('enabled via ACCESSIBLE env var', () => { | ||
| process.env.ACCESSIBLE = '1'; | ||
| const result = prompts.spinner({ output }); | ||
|
|
||
| result.start('Loading'); | ||
| result.stop('Done'); | ||
|
|
||
| expect(output.buffer).toEqual(['Loading\n', 'Done\n']); | ||
| }); | ||
|
|
||
| test('accessible: false option overrides the global setting', () => { | ||
| updateSettings({ accessible: true }); | ||
| const result = prompts.spinner({ output, accessible: false }); | ||
|
|
||
| result.start('Loading'); | ||
| result.stop('Done'); | ||
|
|
||
| expect(output.buffer.join('')).toMatch(ANSI_REGEX); | ||
| }); | ||
| }); |
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.
who is expected to configure this? does it need to be configurable?
we should avoid adding noise to the public API if we don't need to