From 4d90d83b67f2f7e71e547bc289a59877a79e9d25 Mon Sep 17 00:00:00 2001 From: shanmite Date: Thu, 20 Jan 2022 17:51:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A5=E5=BF=97=E7=BB=86=E8=8A=82?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 下载进度条 - iso+08 - 结构调整缓存变量 --- lib/utils.js | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 8bc6f34..424b411 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -103,11 +103,7 @@ const utils = { */ delay(time = 1000) { utils.log.info('时延', `${~~time}ms`); - return new Promise(resolve => { - setTimeout(() => { - resolve(); - }, time); - }); + return new Promise(resolve => setTimeout(resolve, time)); }, /** * 计数器 0..Infinity @@ -185,13 +181,19 @@ const utils = { }, /**日志 */ log: { - level: 3, + _level: 3, + _colors: [ + chalk.hex('#64B3FF'), chalk.grey, chalk.hex('#FFA500'), + chalk.hex('#0070BB'), chalk.hex('#48BB31'), chalk.hex('#BBBB23'), chalk.hex('#FF0006') + ], + _iso_time: () => new Date(Date.now() + 288e5).toISOString().slice(0, -1) + '+08' + , /** * 初始化默认level为3 */ init() { let _level = Number(process.env.LOTTERY_LOG_LEVEL); - this.level = isNaN(_level) ? 3 : _level; + this._level = isNaN(_level) ? 3 : _level; }, /** * @param {String|Array} msg @@ -211,26 +213,36 @@ const utils = { const colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta'], colorsCount = colors.length; - this.proPrint(msg.map(it => it.split('').map((l, i) => chalk[colors[i % colorsCount]](l)).join('')), '\n') }, + /** + * @param {number} done + * @param {number} total + * @param {number} size + */ + progress_bar(done, total, size = 30) { + let perc = done >= total ? 1 : done / total, + bar = ~~(perc * size), + status_bar = `\r[${"=".repeat(bar) + ">" + " ".repeat(size - bar)}] ${(perc * 100 + ' ').slice(0, 4)}%` + process.stdout.write(status_bar) + }, debug(context, msg) { - if (this.level > 3) { + if (this._level > 3) { if (msg instanceof Object) msg = JSON.stringify(msg, null, 4); - this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Debug]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#0070BB')(`[\n${msg}\n]`)]) + this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Debug]"), this._colors[2](`[${context}]`), this._colors[3](`[\n${msg}\n]`)]) } }, info(context, msg) { - if (this.level > 2) - this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Info]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#48BB31')(`[${msg}]`)]) + if (this._level > 2) + this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Info]"), this._colors[2](`[${context}]`), this._colors[4](`[${msg}]`)]) }, warn(context, msg) { - if (this.level > 1) - this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Warn]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#BBBB23')(`[\n${msg}\n]`)]) + if (this._level > 1) + this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Warn]"), this._colors[2](`[${context}]`), this._colors[5](`[\n${msg}\n]`)]) }, error(context, msg) { - if (this.level > 0) - this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Error]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#FF0006')(`[\n${msg}\n]`)]) + if (this._level > 0) + this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Error]"), this._colors[2](`[${context}]`), this._colors[6](`[\n${msg}\n]`)]) } }, /** @@ -249,12 +261,12 @@ const utils = { retry: false }, success: ({ headers, resStream }) => { - const total_len = Number(headers["content-length"]); + const total_len = Number(headers["content-length"]) || 16000000; let recv_length = 0; const wtbs = fs.createWriteStream(file_name); resStream.on('data', chuck => { recv_length += chuck.length - utils.log.proPrint(total_len ? `已下载${(recv_length * 100 / total_len + '').slice(0, 5)}%` : `已收到:${recv_length} Bytes`) + utils.log.progress_bar(recv_length, total_len) }) resStream.pipe(wtbs) wtbs.on('finish', () => {