-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
132 lines (95 loc) · 2.6 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
var readline = require('readline');
var defaultSpinnerString = 0;
var defaultSpinnerDelay = 60;
function defaultOnTick(msg) {
this.clearLine(this.stream);
this.stream.write(msg);
};
var Spinner = function(options){
if(!(this instanceof Spinner)) return new Spinner(options)
if(typeof options === "string"){
options = { text: options };
} else if(!options){
options = {};
}
this.text = options.text || '';
this.setSpinnerString(defaultSpinnerString);
this.setSpinnerDelay(defaultSpinnerDelay);
this.onTick = options.onTick || defaultOnTick;
this.stream = options.stream || process.stdout;
};
Spinner.spinners = require('./spinners.json');
Spinner.setDefaultSpinnerString = function(value) {
defaultSpinnerString = value;
return this;
};
Spinner.setDefaultSpinnerDelay = function(value) {
defaultSpinnerDelay = value;
return this;
};
Spinner.prototype.start = function() {
if(this.stream === process.stdout && this.stream.isTTY !== true) {
return this;
}
var current = 0;
var self = this;
var iteration = function() {
var msg = self.text.indexOf('%s') > -1
? self.text.replace('%s', self.chars[current])
: self.chars[current] + ' ' + self.text;
self.onTick(msg);
current = ++current % self.chars.length;
};
iteration();
this.id = setInterval(iteration, this.delay);
return this;
};
Spinner.prototype.isSpinning = function() {
return this.id !== undefined;
}
Spinner.prototype.setSpinnerDelay = function(n) {
this.delay = n;
return this;
};
Spinner.prototype.setSpinnerString = function(str) {
const map = mapToSpinner(str, this.spinners);
this.chars = Array.isArray(map) ? map : map.split('');
return this;
};
Spinner.prototype.setSpinnerTitle = function(str) {
this.text = str;
return this;
}
Spinner.prototype.stop = function(clear) {
if(this.isSpinning === false) {
return this;
}
clearInterval(this.id);
this.id = undefined;
if (clear) {
this.clearLine(this.stream);
}
return this;
};
Spinner.prototype.clearLine = function(stream) {
readline.clearLine(stream, 0);
readline.cursorTo(stream, 0);
return this;
}
// Helpers
function isInt(value) {
return (typeof value==='number' && (value%1)===0);
}
function mapToSpinner(value, spinners) {
// Not an integer, return as strng
if (!isInt(value)) {
return value + '';
}
var length = Spinner.spinners.length;
// Check if index is within bounds
value = (value >= length) ? 0 : value;
// If negative, count from the end
value = (value < 0) ? length + value : value;
return Spinner.spinners[value];
}
exports.Spinner = Spinner;