-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
76 lines (68 loc) · 1.66 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
'use strict'
const readline = require('readline')
const ansi = require('ansi')
// Braille patterns from:
// http://symbolcodes.tlt.psu.edu/bylanguage/braillechart.html
const PATTERNS = {
small: [
'⠟',
'⠯',
'⠷',
'⠾',
'⠽',
'⠻'
],
large: [
'⡿',
'⣟',
'⣯',
'⣷',
'⣾',
'⣽',
'⣻',
'⢿'
]
}
function LoadingIndicator (attributes) {
attributes = attributes || {}
this.size = attributes.size || 'large'
this.patterns = PATTERNS[this.size]
this.interval = attributes.interval || 70
this.cursor = ansi(process.stdout)
this.format = attributes.format || function (pattern) { return pattern }
if (attributes.rotation === 'cw') {
this.patterns.reverse()
}
}
LoadingIndicator.prototype.start = function () {
this.readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
})
this.cursor.hide()
this.patternIndex = 0
this._animationInterval = setInterval(animate.bind(this), this.interval)
}
LoadingIndicator.prototype.stop = function () {
resetLineAndCursor()
this.cursor.show()
this.readlineInterface.close()
if (this._animationInterval) {
clearInterval(this._animationInterval)
this._animationInterval = null
}
}
function animate () {
resetLineAndCursor()
this.readlineInterface.output.write(this.format(this.patterns[this.patternIndex], this.patternIndex))
if (this.patternIndex < this.patterns.length - 1) {
this.patternIndex++
} else {
this.patternIndex = 0
}
}
function resetLineAndCursor () {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
}
module.exports = LoadingIndicator