spin.js
1.17 KB
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
module.exports = spinner
function spinner(opt) {
opt = opt || {}
var str = opt.stream || process.stderr
var tty = typeof opt.tty === 'boolean' ? opt.tty : true
var string = opt.string || '/-\\|'
var ms = typeof opt.interval === 'number' ? opt.interval : 50
if (ms < 0) ms = 0
if (tty && !str.isTTY) return false
var CR = str.isTTY ? '\u001b[0G' : '\u000d';
var CLEAR = str.isTTY ? '\u001b[2K' : '\u000d \u000d';
var s = 0
var sprite = string.split('')
var wrote = false
var delay = typeof opt.delay === 'number' ? opt.delay : 2
var interval = setInterval(function() {
if (--delay >= 0) return
s = ++s % sprite.length
var c = sprite[s]
str.write(c + CR)
wrote = true
}, ms)
var unref = typeof opt.unref === 'boolean' ? opt.unref : true
if (unref && typeof interval.unref === 'function') {
interval.unref()
}
var cleanup = typeof opt.cleanup === 'boolean' ? opt.cleanup : true
if (cleanup) {
process.on('exit', function() {
if (wrote) {
str.write(CLEAR);
}
})
}
module.exports.clear = function () {
str.write(CLEAR);
};
return interval
}
module.exports.clear = function () {};