-
Notifications
You must be signed in to change notification settings - Fork 0
/
color-bar.js
151 lines (129 loc) · 3.32 KB
/
color-bar.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
const process = require("process")
const rdl = require("readline")
const l = console.log
const std = process.stdout
const colors = {
"yellow": [33, 89],
"blue": [34, 89],
"green": [32, 89],
"red": [35,89],
"cyan": [31,89],
"magenta": [36,89]
}
function shuffle(arr) {
let what;
if (shuffle.lastIndex != undefined) {
if (shuffle.lastIndex >= arr.length - 1) {
shuffle.lastIndex = 0
what = 0
} else {
what = shuffle.lastIndex + 1
shuffle.lastIndex = shuffle.lastIndex + 1
}
} else {
shuffle.lastIndex = 0
return arr[0]
}
return arr[what]
}
class LoadingBar {
constructor(size, type = "bar") {
this.type = type
this.size = size
this.sizeToRender = 0
this.percent = 0
this.sizeNow = 0
this.cursor = 0
this.random=false
this.timer = null
this.str = "\u2588"
this._color = {
start: "\x1b[93m",
stop: "\x1b[39m\x1b[0m"
}
this.resetColor = "\x1b[0m"
}
render() {
if(this.random)
this.randomise()
this.cursor = 1
rdl.cursorTo(std, this.cursor, 0);
for (let i = 0; i < this.sizeToRender; i++) {
std.write(this._color.start + this.str + this._color.stop)
rdl.cursorTo(std, 52, 0);
this.calcPercent()
std.write(this.percent + "%")
this.cursor += 1
rdl.cursorTo(std, this.cursor, 0);
}
}
setColor(color) {
let {0: start,1:stop} = color
start = "\x1b[" + start+"m"
stop = "\x1b[" + stop+ "m\x1b[0m"
this._color.start = start
this._color.stop = stop
}
randomise() {
if(!this.random)
this.random=true
const colorKeys = Object.keys(colors)
const colorToSelect = shuffle(colorKeys)
const color = colors[colorToSelect]
this.setColor(color)
return this
}
color(colorName) {
const color = colors[colorName]
this.setColor(color)
return this
}
start() {
std.write("\x1B[?25l")
std.write("[")
for (let i = 0; i < 50; i++) {
std.write("\u2591")
}
std.write("]")
this.cursor += 1
std.write(this.calcPercent() + "%")
rdl.cursorTo(std, this.cursor, 0);
}
calcPercent() {
this.sizeToRender = this.sizeNow / this.size * 50
this.percent = this.sizeToRender / 50 * 100
return this.sizeToRender
}
incr(val) {
if (typeof val === 'undefined') {
this.sizeNow += 1
} else
this.sizeNow = val
this.calcPercent()
this.render()
}
stop() {
clearInterval(this.timer)
}
}
const cl = new LoadingBar(10)
cl.randomise().start()
// cl.color('green').start()
let count = 0
let timer = setInterval(() => {
if (count == 10) {
clearInterval(timer)
} else {
cl.incr()
count++
}
}, 500)
module.exports = LoadingBar
/* this.timer = setInterval(() => {
this.render()
}, 100)*/
/*std.write(this.color.start + this.str + this.color.stop)
this.cursor++;
if (this.cursor >= this.size) {
clearTimeout(this.timer)
}*/