-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspinner.js
73 lines (47 loc) · 1.72 KB
/
spinner.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
exports.default = class Spinner {
constructor(key, consoleFunc, consoleType, logMessage, cliSpinner, buildLogStringFunc) {
this.key = key
this.consoleFunc = consoleFunc
this.consoleType = Object.assign({}, consoleType)
this.logMessage = new String(logMessage)
this.cliSpinner = cliSpinner
this.buildLogString = buildLogStringFunc
this.nextFrameIndex = 0
this.stopRendering = false
this.getKey = this.getKey.bind(this)
this.render = this.render.bind(this)
this.start = this.start.bind(this)
this.stop = this.stop.bind(this)
this.update = this.update.bind(this)
}
getKey() { return this.key }
render(firstTime) {
if (this.stopRendering) {
return
}
// advance frame
this.consoleType.bullet.chars = this.cliSpinner.frames[this.nextFrameIndex++]
// build render string
let logString = this.buildLogString(this.consoleType, this.logMessage, !firstTime)
// render
this.consoleFunc(logString)
if (this.nextFrameIndex === this.cliSpinner.frames.length) {
// loop the frames (i.e. reset)
this.nextFrameIndex = 0
}
setTimeout(this.render, this.cliSpinner.interval)
}
start() { this.render(true) }
stop() { this.stopRendering = true }
update (message) { this.logMessage = new String(message) }
}
//
//
// private helper methods
//
//
const escapeColor = function(color) { return `\x1b[${color}m` }
const prepend = function(prependObj, logString, colors) {
const { color = 39, chars = '', leftPadding = 0, rightPadding = 1 } = prependObj
return `${escapeColor(color)}${' '.repeat(leftPadding)}${chars}${' '.repeat(rightPadding)}${logString}`
}