const slog = require('single-line-log').stdout;
class ProgressBar {
constructor(description, barLength) {
this.description = description || 'Progress';
this.barLength = barLength || 25;
}
render (options) {
const percent = (options.completed / options.total).toFixed(4);
const cell_num = Math.floor(percent * this.barLength);
let cell = '';
for (let i = 0; i < cell_num; i++) {
cell += '█';
}
let empty = '';
for (let j = 0; j < this.barLength - cell_num; j++) {
empty += '░';
}
const cmdText = this.description + ': ' + (100 * percent).toFixed(2) + '% ' + cell + empty;
slog(cmdText);
}
}
const awaitTime = async (time) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time)
});
};
const start = async () => {
let progressBar = null;
let num = 0;
let total = 100;
const handle = async (data, beforeMessage) => {
if (num <= total) {
data.render({ completed: num, total: total });
num++;
return new Promise((resolve) => {
setTimeout(async () => {
await handle(data, beforeMessage)
resolve();
}, 30)
});
}
await awaitTime(300);
slog(beforeMessage);
}
progressBar = new ProgressBar('检查发射环境', 50);
await handle(progressBar, '发射环境检查完毕... 开始启动发射程序...');
await awaitTime(1000);
total = 100;
num = 0;
progressBar = new ProgressBar('启动发射程序', 50);
await handle(progressBar, '启动发射程序完毕... 核弹即将发射...');
setTimeout(() => { return }, 1000)
launch();
}
start();
- 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