-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
70 lines (57 loc) · 1.87 KB
/
index.ts
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
import cliProgress from 'cli-progress';
import colors from 'chalk';
import type { SingleBar, Options } from 'cli-progress';
const formatBar = function (progress: number, options: Options) {
const { barCompleteString, barIncompleteString, barsize, barGlue } = options;
// calculate barsize
const completeSize = Math.max(0, Math.round(progress * barsize));
const incompleteSize = Math.max(0, barsize - completeSize);
const barComplete = barCompleteString.slice(0, completeSize);
const barIncomplete = barIncompleteString.slice(0, incompleteSize);
// generate bar string by stripping the pre-rendered strings
const results = [
colors.greenBright(barComplete),
barGlue,
colors.gray.dim(barIncomplete),
];
return results.join('');
};
const formatTime = function (time: number) {
const milliseconds = Math.floor(time % 1000);
const seconds = Math.floor((time / 1000) % 60);
const minutes = Math.floor((time / (1000 * 60)) % 60);
const timeArray = [];
if (minutes > 0) {
timeArray.push(`${minutes}m`);
}
if (seconds > 0) {
timeArray.push(`${seconds}s`);
}
if (seconds === 0) {
timeArray.push(`0.${milliseconds}ms`);
}
return timeArray.join(' ');
};
const createHeader = (text: string) => {
const header = colors.bgBlue(` ${text} `);
console.log('\n');
console.log(header);
};
const createProgressBar = (prefix?: string): SingleBar => {
const formatString = [prefix, '{bar} {percentage}%'].filter(Boolean);
const bar = new cliProgress.SingleBar({
format: formatString.join(' '),
barCompleteChar: '━',
barIncompleteChar: '━',
formatBar,
});
const startTime = performance.now();
bar.on('stop', () => {
const endTime = performance.now();
const formattedTime = formatTime(endTime - startTime);
process.stdout.write(` ${colors.black.bgGreenBright(` ${formattedTime} `)}`);
});
bar.start(100, 0);
return bar;
};
export { createProgressBar, createHeader };