@beenotung/tslib
Version:
utils library in Typescript
142 lines • 4.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startTimer = exports.eraseChars = exports.catchMain = void 0;
const format_1 = require("./format");
function catchMain(p) {
p.catch(e => {
console.error(e);
process.exit(1);
});
}
exports.catchMain = catchMain;
function eraseChars(writeStream, n) {
if (n < 1) {
return;
}
writeStream.write(' '.repeat(n));
if (writeStream.moveCursor) {
writeStream.moveCursor(-n, 0);
}
}
exports.eraseChars = eraseChars;
const defaultWriteStream = () => process.stdout;
function startTimer(options) {
let name;
let writeStream;
let sampleOver;
let estimateTime = false;
if (typeof options === 'string') {
name = options;
writeStream = defaultWriteStream();
sampleOver = 1;
}
else {
name = options.name;
writeStream = options.writeStream || defaultWriteStream();
sampleOver = options.sampleOver || 1;
}
let msgLen = 0;
const print = (msg) => {
if (writeStream.moveCursor) {
writeStream.moveCursor(-msgLen, 0);
}
writeStream.write(msg);
const newMsgLen = msg.length;
eraseChars(writeStream, msgLen - newMsgLen);
msgLen = newMsgLen;
};
const start = () => {
writeStream.write(new Date().toLocaleString() + ': ' + name);
print(' ...');
// tslint:disable-next-line no-console
console.time(name);
};
start();
const end = () => {
if (!name) {
return; // already ended before
}
print('');
if (writeStream.moveCursor) {
writeStream.moveCursor(-name.length, 0);
}
// tslint:disable-next-line no-console
console.timeEnd(name);
name = undefined;
};
let totalTick = 0;
let currentTick = 0;
let startTick;
let startTime;
const progress = (msg) => {
print(msg);
};
const tickProgressWithoutEstimateTime = () => {
progress(` (${currentTick}/${totalTick})`);
};
const tickProgressWithEstimateTime = () => {
const tickLeft = totalTick - currentTick;
const tickedAmount = currentTick - startTick;
const tickedTime = Date.now() - startTime;
const tickSpeed = tickedAmount / tickedTime;
const timeLeft = tickLeft / tickSpeed;
const timeLeftText = format_1.format_time_duration(timeLeft);
progress(` (${currentTick}/${totalTick}, estimated time left: ${timeLeftText})`);
};
let tickProgress = estimateTime
? tickProgressWithEstimateTime
: tickProgressWithoutEstimateTime;
const setProgress = (totalTick_or_options, _initialTick, _sampleOver) => {
if (typeof totalTick_or_options === 'object') {
const option = totalTick_or_options;
totalTick = option.totalTick;
currentTick = option.initialTick || 0;
sampleOver = option.sampleOver || sampleOver;
if (typeof option.estimateTime === 'boolean') {
estimateTime = option.estimateTime;
tickProgress = estimateTime
? tickProgressWithEstimateTime
: tickProgressWithoutEstimateTime;
}
}
else {
totalTick = totalTick_or_options;
currentTick = _initialTick || 0;
if (_sampleOver) {
sampleOver = _sampleOver;
}
}
timer.tick = sampleOver === 1 ? tickWithOutSample : tickWithSample;
if (estimateTime) {
startTick = currentTick;
startTime = Date.now();
}
tickProgress();
};
const tickWithOutSample = (step = 1) => {
currentTick += step;
tickProgress();
};
const tickWithSample = (step = 1) => {
const oldMod = currentTick % sampleOver;
currentTick += step;
const newMod = currentTick % sampleOver;
if (newMod === 0 || newMod < oldMod) {
tickProgress();
}
};
const timer = {
end,
next(newName) {
end();
name = newName;
start();
},
progress,
setProgress,
tick: sampleOver === 1 ? tickWithOutSample : tickWithSample,
};
return timer;
}
exports.startTimer = startTimer;
//# sourceMappingURL=node.js.map