UNPKG

5.77 kBTypeScriptView Raw
1/// <reference types="node"/>
2import {SpinnerName} from 'cli-spinners';
3
4declare namespace ora {
5 interface Spinner {
6 readonly interval?: number;
7 readonly frames: string[];
8 }
9
10 type Color =
11 | 'black'
12 | 'red'
13 | 'green'
14 | 'yellow'
15 | 'blue'
16 | 'magenta'
17 | 'cyan'
18 | 'white'
19 | 'gray';
20
21 interface Options {
22 /**
23 Text to display after the spinner.
24 */
25 readonly text?: string;
26
27 /**
28 Text to display before the spinner. No prefix text will be displayed if set to an empty string.
29 */
30 readonly prefixText?: string;
31
32 /**
33 Name of one of the provided spinners. See [`example.js`](https://github.com/BendingBender/ora/blob/master/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 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 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.
97
98 This has no effect on Windows as there's no good way to implement discarding stdin properly there.
99
100 @default true
101 */
102 readonly discardStdin?: boolean;
103 }
104
105 interface PersistOptions {
106 /**
107 Symbol to replace the spinner with.
108
109 @default ' '
110 */
111 readonly symbol?: string;
112
113 /**
114 Text to be persisted after the symbol.
115
116 Default: Current `text`.
117 */
118 readonly text?: string;
119
120 /**
121 Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
122
123 Default: Current `prefixText`.
124 */
125 readonly prefixText?: string;
126 }
127
128 interface Ora {
129 /**
130 A boolean of whether the instance is currently spinning.
131 */
132 readonly isSpinning: boolean;
133
134 /**
135 Change the text after the spinner.
136 */
137 text: string;
138
139 /**
140 Change the text before the spinner. No prefix text will be displayed if set to an empty string.
141 */
142 prefixText: string;
143
144 /**
145 Change the spinner color.
146 */
147 color: Color;
148
149 /**
150 Change the spinner.
151 */
152 spinner: SpinnerName | Spinner;
153
154 /**
155 Change the spinner indent.
156 */
157 indent: number;
158
159 /**
160 Start the spinner.
161
162 @param text - Set the current text.
163 @returns The spinner instance.
164 */
165 start(text?: string): Ora;
166
167 /**
168 Stop and clear the spinner.
169
170 @returns The spinner instance.
171 */
172 stop(): Ora;
173
174 /**
175 Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided.
176
177 @param text - Will persist text if provided.
178 @returns The spinner instance.
179 */
180 succeed(text?: string): Ora;
181
182 /**
183 Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided.
184
185 @param text - Will persist text if provided.
186 @returns The spinner instance.
187 */
188 fail(text?: string): Ora;
189
190 /**
191 Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided.
192
193 @param text - Will persist text if provided.
194 @returns The spinner instance.
195 */
196 warn(text?: string): Ora;
197
198 /**
199 Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided.
200
201 @param text - Will persist text if provided.
202 @returns The spinner instance.
203 */
204 info(text?: string): Ora;
205
206 /**
207 Stop the spinner and change the symbol or text.
208
209 @returns The spinner instance.
210 */
211 stopAndPersist(options?: PersistOptions): Ora;
212
213 /**
214 Clear the spinner.
215
216 @returns The spinner instance.
217 */
218 clear(): Ora;
219
220 /**
221 Manually render a new frame.
222
223 @returns The spinner instance.
224 */
225 render(): Ora;
226
227 /**
228 Get a new frame.
229
230 @returns The spinner instance text.
231 */
232 frame(): string;
233 }
234}
235
236declare const ora: {
237 /**
238 Elegant terminal spinner.
239
240 @param options - If a string is provided, it is treated as a shortcut for `options.text`.
241
242 @example
243 ```
244 import ora = require('ora');
245
246 const spinner = ora('Loading unicorns').start();
247
248 setTimeout(() => {
249 spinner.color = 'yellow';
250 spinner.text = 'Loading rainbows';
251 }, 1000);
252 ```
253 */
254 (options?: ora.Options | string): ora.Ora;
255
256 /**
257 Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects.
258
259 @param action - The promise to start the spinner for.
260 @param options - If a string is provided, it is treated as a shortcut for `options.text`.
261 @returns The spinner instance.
262 */
263 promise(
264 action: PromiseLike<unknown>,
265 options?: ora.Options | string
266 ): ora.Ora;
267};
268
269export = ora;