Skip to content

Commit b557c5a

Browse files
committed
use style function instead color option
1 parent aa3afdd commit b557c5a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packages/prompts/src/spinner.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { block, getColumns, settings } from '@clack/core';
22
import { wrapAnsi } from 'fast-wrap-ansi';
3-
import colors from 'picocolors';
4-
import type { Colors } from 'picocolors/types.d.ts';
3+
import color from 'picocolors';
54
import { cursor, erase } from 'sisteransi';
65
import {
76
type CommonOptions,
@@ -20,7 +19,7 @@ export interface SpinnerOptions extends CommonOptions {
2019
errorMessage?: string;
2120
frames?: string[];
2221
delay?: number;
23-
color?: keyof Omit<Colors, 'isColorSupported'>;
22+
style?: (frame: string) => string;
2423
}
2524

2625
export interface SpinnerResult {
@@ -30,6 +29,8 @@ export interface SpinnerResult {
3029
readonly isCancelled: boolean;
3130
}
3231

32+
const defaultStyleFn: SpinnerOptions['style'] = color.magenta;
33+
3334
export const spinner = ({
3435
indicator = 'dots',
3536
onCancel,
@@ -39,7 +40,7 @@ export const spinner = ({
3940
frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'],
4041
delay = unicode ? 80 : 120,
4142
signal,
42-
color = 'magenta',
43+
style,
4344
}: SpinnerOptions = {}): SpinnerResult => {
4445
const isCI = isCIFn();
4546

@@ -51,6 +52,7 @@ export const spinner = ({
5152
let _prevMessage: string | undefined;
5253
let _origin: number = performance.now();
5354
const columns = getColumns(output);
55+
const styleFn = style ?? defaultStyleFn;
5456

5557
const handleExit = (code: number) => {
5658
const msg =
@@ -127,7 +129,7 @@ export const spinner = ({
127129
unblock = block({ output });
128130
_message = removeTrailingDots(msg);
129131
_origin = performance.now();
130-
output.write(`${colors.gray(S_BAR)}\n`);
132+
output.write(`${color.gray(S_BAR)}\n`);
131133
let frameIndex = 0;
132134
let indicatorTimer = 0;
133135
registerHooks();
@@ -137,7 +139,7 @@ export const spinner = ({
137139
}
138140
clearPrevMessage();
139141
_prevMessage = _message;
140-
const frame = colors[color](frames[frameIndex]);
142+
const frame = styleFn(frames[frameIndex]);
141143
let outputMessage: string;
142144

143145
if (isCI) {
@@ -168,10 +170,10 @@ export const spinner = ({
168170
clearPrevMessage();
169171
const step =
170172
code === 0
171-
? colors.green(S_STEP_SUBMIT)
173+
? color.green(S_STEP_SUBMIT)
172174
: code === 1
173-
? colors.red(S_STEP_CANCEL)
174-
: colors.red(S_STEP_ERROR);
175+
? color.red(S_STEP_CANCEL)
176+
: color.red(S_STEP_ERROR);
175177
_message = msg ?? _message;
176178
if (indicator === 'timer') {
177179
output.write(`${step} ${_message} ${formatTimer(_origin)}\n`);

0 commit comments

Comments
 (0)