UNPKG

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