forked from sindresorhus/ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (152 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const chalk = require('chalk');
const cliCursor = require('cli-cursor');
const cliSpinners = require('cli-spinners');
const logSymbols = require('log-symbols');
const stripAnsi = require('strip-ansi');
const wcwidth = require('wcwidth');
const TEXT = Symbol('text');
class Ora {
constructor(options) {
if (typeof options === 'string') {
options = {
text: options
};
}
this.options = Object.assign({
text: '',
color: 'cyan',
stream: process.stderr
}, options);
const sp = this.options.spinner;
this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line no-nested-ternary
if (this.spinner.frames === undefined) {
throw new Error('Spinner must define `frames`');
}
this.color = this.options.color;
this.hideCursor = this.options.hideCursor !== false;
this.interval = this.options.interval || this.spinner.interval || 100;
this.stream = this.options.stream;
this.id = null;
this.frameIndex = 0;
this.enabled = typeof this.options.enabled === 'boolean' ? this.options.enabled : ((this.stream && this.stream.isTTY) && !process.env.CI);
// Set *after* `this.stream`
this.text = this.options.text;
this.linesToClear = 0;
}
get text() {
return this[TEXT];
}
get isSpinning() {
return this.id !== null;
}
set text(value) {
this[TEXT] = value;
const columns = this.stream.columns || 80;
this.lineCount = stripAnsi('--' + value).split('\n').reduce((count, line) => {
return count + Math.max(1, Math.ceil(wcwidth(line) / columns));
}, 0);
}
frame() {
const {frames} = this.spinner;
let frame = frames[this.frameIndex];
if (this.color) {
frame = chalk[this.color](frame);
}
this.frameIndex = ++this.frameIndex % frames.length;
return frame + ' ' + this.text;
}
clear() {
if (!this.enabled) {
return this;
}
for (let i = 0; i < this.linesToClear; i++) {
if (i > 0) {
this.stream.moveCursor(0, -1);
}
this.stream.clearLine();
this.stream.cursorTo(0);
}
this.linesToClear = 0;
return this;
}
render() {
this.clear();
this.stream.write(this.frame());
this.linesToClear = this.lineCount;
return this;
}
start(text) {
if (text) {
this.text = text;
}
if (!this.enabled || this.isSpinning) {
console.log(`${logSymbols.info} ${this.text}`);
return this;
}
if (this.hideCursor) {
cliCursor.hide(this.stream);
}
this.render();
this.id = setInterval(this.render.bind(this), this.interval);
return this;
}
stop() {
if (!this.enabled) {
return this;
}
clearInterval(this.id);
this.id = null;
this.frameIndex = 0;
this.clear();
if (this.hideCursor) {
cliCursor.show(this.stream);
}
return this;
}
succeed(text) {
return this.stopAndPersist({symbol: logSymbols.success, text});
}
fail(text) {
return this.stopAndPersist({symbol: logSymbols.error, text});
}
warn(text) {
return this.stopAndPersist({symbol: logSymbols.warning, text});
}
info(text) {
return this.stopAndPersist({symbol: logSymbols.info, text});
}
stopAndPersist(options) {
options = options || {};
// TODO: Remove in the next major version
if (typeof options === 'string') {
throw new TypeError('This argument now accepts an options object, not a string');
}
if (!this.enabled) {
console.log(`${options.symbol || ' '} ${options.text || this.text}`);
return this;
}
this.stop();
this.stream.write(`${options.symbol || ' '} ${options.text || this.text}\n`);
return this;
}
}
module.exports = function (opts) {
return new Ora(opts);
};
module.exports.promise = (action, options) => {
if (typeof action.then !== 'function') {
throw new TypeError('Parameter `action` must be a Promise');
}
const spinner = new Ora(options);
spinner.start();
action.then(
() => {
spinner.succeed();
},
() => {
spinner.fail();
}
);
return spinner;
};