-
Notifications
You must be signed in to change notification settings - Fork 8
/
printprogress.js
27 lines (23 loc) · 899 Bytes
/
printprogress.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
const readline = require("readline");
let lastUpdate = 0;
// Updates the single line progress indicator on screen
function printprogress(start, tilesComplete, totalTiles) {
const now = new Date();
if (now - lastUpdate < 1000) return; // Update frequency < once per second
const elapsedMs = new Date() - start;
const progress = (100 * tilesComplete) / totalTiles;
const remainMs = (elapsedMs / tilesComplete) * (totalTiles - tilesComplete);
const tilespersec = (tilesComplete / elapsedMs) * 1000;
const est = new Date(Date.now() + remainMs);
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
process.stdout.write(
`${tilesComplete}/${totalTiles} (${progress.toFixed(
2
)}%)\t${tilespersec.toFixed(
2
)} tiles/sec\t Completion est: ${est.toLocaleTimeString()}`
);
lastUpdate = now;
}
module.exports = printprogress;