UNPKG

7.49 kBTypeScriptView Raw
1import {type SpinnerName} from 'cli-spinners';
2
3export type Spinner = {
4 readonly interval?: number;
5 readonly frames: string[];
6};
7
8export type Color =
9 | 'black'
10 | 'red'
11 | 'green'
12 | 'yellow'
13 | 'blue'
14 | 'magenta'
15 | 'cyan'
16 | 'white'
17 | 'gray';
18
19export type PrefixTextGenerator = () => string;
20
21export type SuffixTextGenerator = () => string;
22
23export type Options = {
24 /**
25 The text to display next to the spinner.
26 */
27 readonly text?: string;
28
29 /**
30 Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string.
31 */
32 readonly prefixText?: string | PrefixTextGenerator;
33
34 /**
35 Text or a function that returns text to display after the spinner text. No suffix text will be displayed if set to an empty string.
36 */
37 readonly suffixText?: string | SuffixTextGenerator;
38
39 /**
40 The name of one of the provided spinners. See [`example.js`](https://github.com/BendingBender/ora/blob/main/example.js) in this repo if you want to test out different spinners. On Windows (expect for Windows Terminal), it will always use the line spinner as the Windows command-line doesn't have proper Unicode support.
41
42 @default 'dots'
43
44 Or an object like:
45
46 @example
47 ```
48 {
49 frames: ['-', '+', '-'],
50 interval: 80 // Optional
51 }
52 ```
53 */
54 readonly spinner?: SpinnerName | Spinner;
55
56 /**
57 The color of the spinner.
58
59 @default 'cyan'
60 */
61 readonly color?: Color;
62
63 /**
64 Set to `false` to stop Ora from hiding the cursor.
65
66 @default true
67 */
68 readonly hideCursor?: boolean;
69
70 /**
71 Indent the spinner with the given number of spaces.
72
73 @default 0
74 */
75 readonly indent?: number;
76
77 /**
78 Interval between each frame.
79
80 Spinners provide their own recommended interval, so you don't really need to specify this.
81
82 Default: Provided by the spinner or `100`.
83 */
84 readonly interval?: number;
85
86 /**
87 Stream to write the output.
88
89 You could for example set this to `process.stdout` instead.
90
91 @default process.stderr
92 */
93 readonly stream?: NodeJS.WritableStream;
94
95 /**
96 Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment.
97
98 Note that `{isEnabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text.
99 */
100 readonly isEnabled?: boolean;
101
102 /**
103 Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
104
105 @default false
106 */
107 readonly isSilent?: boolean;
108
109 /**
110 Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on `Enter` key presses, and prevents buffering of input while the spinner is running.
111
112 This has no effect on Windows as there's no good way to implement discarding stdin properly there.
113
114 @default true
115 */
116 readonly discardStdin?: boolean;
117};
118
119export type PersistOptions = {
120 /**
121 Symbol to replace the spinner with.
122
123 @default ' '
124 */
125 readonly symbol?: string;
126
127 /**
128 Text to be persisted after the symbol.
129
130 Default: Current `text`.
131 */
132 readonly text?: string;
133
134 /**
135 Text or a function that returns text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
136
137 Default: Current `prefixText`.
138 */
139 readonly prefixText?: string | PrefixTextGenerator;
140
141 /**
142 Text or a function that returns text to be persisted after the text after the symbol. No suffix text will be displayed if set to an empty string.
143
144 Default: Current `suffixText`.
145 */
146 readonly suffixText?: string | SuffixTextGenerator;
147};
148
149export type PromiseOptions<T> = {
150 /**
151 The new text of the spinner when the promise is resolved.
152
153 Keeps the existing text if `undefined`.
154 */
155 successText?: string | ((result: T) => string) | undefined;
156
157 /**
158 The new text of the spinner when the promise is rejected.
159
160 Keeps the existing text if `undefined`.
161 */
162 failText?: string | ((error: Error) => string) | undefined;
163} & Options;
164
165// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
166export interface Ora {
167 /**
168 Change the text after the spinner.
169 */
170 text: string;
171
172 /**
173 Change the text or function that returns text before the spinner.
174
175 No prefix text will be displayed if set to an empty string.
176 */
177 prefixText: string;
178
179 /**
180 Change the text or function that returns text after the spinner text.
181
182 No suffix text will be displayed if set to an empty string.
183 */
184 suffixText: string;
185
186 /**
187 Change the spinner color.
188 */
189 color: Color;
190
191 /**
192 Change the spinner indent.
193 */
194 indent: number;
195
196 /**
197 Get the spinner.
198 */
199 get spinner(): Spinner;
200
201 /**
202 Set the spinner.
203 */
204 set spinner(spinner: SpinnerName | Spinner);
205
206 /**
207 A boolean indicating whether the instance is currently spinning.
208 */
209 get isSpinning(): boolean;
210
211 /**
212 The interval between each frame.
213
214 The interval is decided by the chosen spinner.
215 */
216 get interval(): number;
217
218 /**
219 Start the spinner.
220
221 @param text - Set the current text.
222 @returns The spinner instance.
223 */
224 start(text?: string): this;
225
226 /**
227 Stop and clear the spinner.
228
229 @returns The spinner instance.
230 */
231 stop(): this;
232
233 /**
234 Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided.
235
236 @param text - Will persist text if provided.
237 @returns The spinner instance.
238 */
239 succeed(text?: string): this;
240
241 /**
242 Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided.
243
244 @param text - Will persist text if provided.
245 @returns The spinner instance.
246 */
247 fail(text?: string): this;
248
249 /**
250 Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided.
251
252 @param text - Will persist text if provided.
253 @returns The spinner instance.
254 */
255 warn(text?: string): this;
256
257 /**
258 Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided.
259
260 @param text - Will persist text if provided.
261 @returns The spinner instance.
262 */
263 info(text?: string): this;
264
265 /**
266 Stop the spinner and change the symbol or text.
267
268 @returns The spinner instance.
269 */
270 stopAndPersist(options?: PersistOptions): this;
271
272 /**
273 Clear the spinner.
274
275 @returns The spinner instance.
276 */
277 clear(): this;
278
279 /**
280 Manually render a new frame.
281
282 @returns The spinner instance.
283 */
284 render(): this;
285
286 /**
287 Get a new frame.
288
289 @returns The spinner instance text.
290 */
291 frame(): string;
292}
293
294/**
295Elegant terminal spinner.
296
297@param options - If a string is provided, it is treated as a shortcut for `options.text`.
298
299@example
300```
301import ora from 'ora';
302
303const spinner = ora('Loading unicorns').start();
304
305setTimeout(() => {
306 spinner.color = 'yellow';
307 spinner.text = 'Loading rainbows';
308}, 1000);
309```
310*/
311export default function ora(options?: string | Options): Ora;
312
313/**
314Starts a spinner for a promise or promise-returning function. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects.
315
316@param action - The promise to start the spinner for or a promise-returning function.
317@param options - If a string is provided, it is treated as a shortcut for `options.text`.
318@returns The given promise.
319
320@example
321```
322import {oraPromise} from 'ora';
323
324await oraPromise(somePromise);
325```
326*/
327export function oraPromise<T>(
328 action: PromiseLike<T> | ((spinner: Ora) => PromiseLike<T>),
329 options?: string | PromiseOptions<T>
330): Promise<T>;
331
332export {default as spinners} from 'cli-spinners';
333
\No newline at end of file