-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressbar.js
More file actions
53 lines (44 loc) · 1.22 KB
/
progressbar.js
File metadata and controls
53 lines (44 loc) · 1.22 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
52
export default function progressBar(value, item, color, font, type) {
const max = 100;
const progress = Math.min(Math.max(value, 0), max);
const percent = Math.round((progress / max) * 100);
// Create progress bar string
const barLength = 20;
let progressBar = '';
let mod = percent % 5
let filledLength = barLength * ((percent - mod)/100)
// Map partial steps to fit 100 unique outputs within the 20 character constraint
const partial = {
1: '▎',
2: '▌',
3: '▋',
4: '▊'
}
// Fill the progress bar with '#' characters
for (let i = 0; i < filledLength; i++) {
progressBar += '█';
}
// Use mod to add a partial if necessary
if (mod!= 0) {
progressBar += partial[mod]
}
while (progressBar.length < barLength) {
progressBar += '░';
}
// Not sure why we need to check for nan here
if (isNaN(percent)) { return }
let head = ''
let tail = ''
if (font) {
head += `[font=${font}]`
tail = `[/font]${tail}`
}
tail = ` ${percent}%${tail}`
if (color) {
head += `[color=${color}]`
tail = `[/color]${tail}`
}
head += `[${type}=${item}]`
const progressBarString = `${head} ${progressBar} ${tail}`;
return (progressBarString);
}