UNPKG

@0xtld/tair-node

Version:

A Node.js package for Tair functionality with configuration, core, and helper modules.

74 lines (73 loc) 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CountdownTimer = void 0; const colors_1 = require("./colors"); const colors = new colors_1.ColorTheme(); class CountdownTimer { constructor(options = {}) { this.options = Object.assign({ showCursor: false, colors: { message: colors.colors.timerCount, timer: colors.colors.timerWarn, reset: colors.colors.reset, }, format: 'HH:mm:ss', message: 'Remaining time: ', clearOnComplete: true }, options); } formatTime(timeInSeconds, format = this.options.format) { const hours = Math.floor(timeInSeconds / 3600); const minutes = Math.floor((timeInSeconds % 3600) / 60); const seconds = timeInSeconds % 60; const padNumber = (num) => num.toString().padStart(2, '0'); switch (format.toUpperCase()) { case 'HH:MM:SS': return `${padNumber(hours)}:${padNumber(minutes)}:${padNumber(seconds)}`; case 'MM:SS': return `${padNumber(minutes)}:${padNumber(seconds)}`; case 'SS': return padNumber(seconds); case 'FULL': return `${hours}h ${minutes}m ${seconds}s`; case 'COMPACT': return hours > 0 ? `${hours}h${minutes}m` : minutes > 0 ? `${minutes}m${seconds}s` : `${seconds}s`; default: return `${padNumber(hours)}:${padNumber(minutes)}:${padNumber(seconds)}`; } } async start(seconds, options = {}) { var _a, _b, _c; const config = Object.assign(Object.assign({}, this.options), options); if (!config.showCursor) { process.stdout.write('\x1B[?25l'); } const { message, } = config; const messageColor = ((_a = config.colors) === null || _a === void 0 ? void 0 : _a.message) || colors.colors.message; const timerColor = ((_b = config.colors) === null || _b === void 0 ? void 0 : _b.timer) || colors.colors.timerWarn; const reset = ((_c = config.colors) === null || _c === void 0 ? void 0 : _c.reset) || colors.colors.reset; try { for (let i = seconds; i > 0; i--) { process.stdout.clearLine(0); process.stdout.cursorTo(0); const timeString = this.formatTime(i, config.format); process.stdout.write(`${messageColor}${message}${timerColor}${timeString}${reset}`); await new Promise((resolve) => setTimeout(resolve, 1000)); } if (config.clearOnComplete) { process.stdout.clearLine(0); process.stdout.cursorTo(0); } } finally { if (!config.showCursor) { process.stdout.write('\x1B[?25h'); } } } static async countdown(seconds, options = {}) { const timer = new CountdownTimer(options); await timer.start(seconds); } } exports.CountdownTimer = CountdownTimer; exports.default = CountdownTimer;