Comparing version 1.1.1 to 1.1.2
const log = require('../index')() | ||
// 在实例化之前,注册一个消息 | ||
log.register('request', { | ||
downloading: { | ||
name: 'flip', // 使用 flip 动画 | ||
interval: 50, // 更快一些 | ||
color: 'gray', // 灰色! | ||
text: data => `${data.frame} downloading data from a remote server` | ||
hello: { | ||
text: '( ̄︶ ̄)↗ Hello World!', | ||
// 开启保留 | ||
save: true, | ||
}, | ||
downloading: data => `${data.frame} downloading data from a remote server`, | ||
completed: '√ download completed', | ||
@@ -15,6 +14,7 @@ }) | ||
// 使用 | ||
log.request.downloading() | ||
setTimeout(() => log.request.completed(), 2000) | ||
// 状态更换后,保留这条消息 | ||
log.request.hello() | ||
setTimeout(() => log.request.downloading(), 500) | ||
// 这一条也会被保留 | ||
// 因为状态会一直被持续 | ||
setTimeout(() => log.request.completed(), 2000) |
37
index.js
@@ -26,3 +26,2 @@ const cliSpinners = require('cli-spinners') | ||
this._nextSave = null | ||
} | ||
@@ -34,13 +33,25 @@ | ||
_interval () { | ||
const {args, text, name = this.name, interval = this.interval, color = this.color} = this.options | ||
const {args, text, name = this.name, interval = this.interval, color = this.color} = this.options | ||
const frames = cliSpinners[name].frames | ||
const _ = () => this.log(text({ | ||
frame: chalk[color](frames[this.index++ % frames.length]), | ||
args | ||
})) | ||
let frame = null | ||
const _ = () => { | ||
frame = chalk[color](frames[this.index++ % frames.length]) | ||
this.log(text(data)) | ||
} | ||
// 先马上显示 | ||
const data = { | ||
args, | ||
} | ||
// 性能优化 | ||
// 只有 data.frame 被使用时,才会触发循环定时器 | ||
Object.defineProperty(data, 'frame', { | ||
get: () => { | ||
if (!this.timer) this.timer = setInterval(_, interval) | ||
return frame | ||
} | ||
}) | ||
// 立即显示 | ||
_() | ||
this.timer = setInterval(_, interval) | ||
}, | ||
@@ -50,3 +61,7 @@ | ||
start (options, ...args) { | ||
if (typeof options === 'string' || options.text === 'string') return this.log(options) | ||
if (typeof options === 'string') return this.log(options) | ||
if (typeof options.text === 'string') return this.log(options.text) | ||
if (typeof options === 'function') { | ||
@@ -93,3 +108,3 @@ this.options = { | ||
} | ||
this._nextSave = options.clear | ||
this._nextSave = options.save | ||
this.start(options, ...args) | ||
@@ -96,0 +111,0 @@ }.bind(this) |
{ | ||
"name": "@ntbl/log", | ||
"version": "1.1.1", | ||
"description": "一个内置进度动画的命令行文本输入工具。", | ||
"version": "1.1.2", | ||
"description": "一个内置 loading 动画的命令行文本输入工具。", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "author": { |
# log | ||
一个内置进度动画的命令行文本输出工具。 | ||
一个内置 loading 动画的命令行文本输出工具。 | ||
@@ -59,3 +59,3 @@ [![GitHub](https://img.shields.io/badge/GitHub-yeshimei-green.svg)](https://github.com/yeshimei/ntbl-log.git) [![npm](https://img.shields.io/npm/v/@ntbl/log.svg)](https://www.npmjs.com/package/@ntbl/log) [![MIT](https://img.shields.io/npm/l/express.svg)](https://github.com/yeshimei/ntbl-log.git) | ||
log 内置了所有 [cli-spinners](https://github.com/sindresorhus/cli-spinners) 命令行进度动画。 | ||
log 内置了所有 [cli-spinners](https://github.com/sindresorhus/cli-spinners) 动画。 | ||
@@ -62,0 +62,0 @@ ```js |
217649
17
179