Skip to content

Commit

Permalink
feat: Supports custom status types (#1587)
Browse files Browse the repository at this point in the history
Fixes #1582
  • Loading branch information
malavshah9 authored Nov 1, 2024
1 parent 6afcf39 commit fb143e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ describe('createPrompt()', () => {
prefix: {
idle: '?',
done: '✔',
unicorn: '🦄',
},
});
const [status, setStatus] = useState<Status>('loading');
Expand All @@ -396,6 +397,9 @@ describe('createPrompt()', () => {
if (isEnterKey(event)) {
done('');
}
if (isSpaceKey(event)) {
setStatus('unicorn');
}
});

return `${prefix} ${config.message}`;
Expand All @@ -420,6 +424,9 @@ describe('createPrompt()', () => {
vi.advanceTimersByTime(totalDuration);
expect(getScreen()).toMatchInlineSnapshot(`"✔ Question"`);

events.keypress('space');
expect(getScreen()).toMatchInlineSnapshot(`"🦄 Question"`);

vi.useRealTimers();

events.keypress('enter');
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/lib/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import type { Prettify } from '@inquirer/type';
* - `'idle'`: The prompt is loaded and currently waiting for the user to
* submit an answer.
* - `'done'`: The user has submitted an answer and the prompt is finished.
* - `string`: Any other string: The prompt is in a custom state.
*/
export type Status = 'loading' | 'idle' | 'done';
export type Status = 'loading' | 'idle' | 'done' | (string & {});

type DefaultTheme = {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/use-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ export function usePrefix({

// There's a delay before we show the loader. So we want to ignore `loading` here, and pass idle instead.
const iconName = status === 'loading' ? 'idle' : status;
return typeof prefix === 'string' ? prefix : prefix[iconName];
return typeof prefix === 'string' ? prefix : (prefix[iconName] ?? prefix['idle']);
}

0 comments on commit fb143e2

Please sign in to comment.