Skip to content

Commit c01a71b

Browse files
committed
better progress bar
1 parent c0eae6a commit c01a71b

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path';
44
import Utils from './utils';
55
import { writeJSON, confirm } from './helper';
66
import * as logger from './logger';
7+
import ProgressBar from './progressbar';
78

89
process.on('SIGINT', () => {
910
process.exit();
@@ -34,6 +35,7 @@ if (config.render.advancements) {
3435
const output = path.join(config.BASEPATH, config.render.output);
3536
logger.Default.info('CREATE OUTPUT DIR', output);
3637

38+
3739
(async () => {
3840
const prompt = await confirm('Do you want to clean the output folder?');
3941
if (prompt) {
@@ -54,6 +56,9 @@ logger.Default.info('CREATE OUTPUT DIR', output);
5456
}
5557
playerlist = playerlist.sort(() => 0.5 - Math.random());
5658

59+
const totalTasks = playerlist.length;
60+
const progress = new ProgressBar(totalTasks);
61+
progress.start();
5762
for (const uuid of playerlist) {
5863
let banned = false;
5964
if (config.render['render-banned']) {
@@ -63,18 +68,18 @@ logger.Default.info('CREATE OUTPUT DIR', output);
6368
try {
6469
data = await utils.createPlayerData(uuid, banned); // eslint-disable-line
6570
} catch (error) {
71+
progress.tick(uuid);
6672
continue;
6773
}
68-
if (!data.data) {
69-
console.log(data);
70-
}
7174
players.push({
7275
uuid: data.data.uuid_short,
7376
playername: data.data.playername,
7477
names: data.data.names,
7578
seen: data.data.seen,
7679
});
80+
progress.tick(uuid);
7781
}
82+
progress.stop();
7883

7984
players.sort((a, b) => b.seen - a.seen); // eslint-disable-line
8085
writeJSON(

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"dependencies": {
2828
"bignum": "^0.12.5",
2929
"fs-extra": "^2.0.0",
30+
"gauge": "^2.7.4",
3031
"inquirer": "^5.0.0",
3132
"js-yaml": "^3.8.1",
3233
"log4js": "^2.4.1",

progressbar.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Gauge from 'gauge';
2+
3+
export default class ProgressBar {
4+
constructor(total) {
5+
this.gauge = new Gauge();
6+
this.total = total;
7+
this.completed = 0;
8+
this.startTime = 0;
9+
this.pulse = '';
10+
}
11+
12+
tick(text) {
13+
this.completed += 1;
14+
this.gauge.show(text, this.completed / this.total);
15+
const now = (new Date()).valueOf();
16+
const avgTime = (now - this.startTime) / this.completed;
17+
const time = Math.floor((this.total - this.completed) * avgTime / 1000);
18+
this.pulse = `${this.completed}/${this.total} ${time}s left`;
19+
}
20+
21+
start() {
22+
this.gauge.show();
23+
this.timer = setInterval(() => {
24+
this.gauge.pulse(this.pulse);
25+
}, 100);
26+
this.startTime = (new Date()).valueOf();
27+
}
28+
29+
stop() {
30+
clearInterval(this.timer);
31+
this.gauge.hide();
32+
}
33+
}

0 commit comments

Comments
 (0)