UNPKG

chalk-console

Version:

console text colorfully, and success, info, warn, or danger colorfully

82 lines (61 loc) 1.67 kB
/* * @Author: xsmallbird * @Description: * @Date: 2019-05-27 12:11:14 * @Last Modified by: xsmallbird */ const chalk = require('chalk') const moment = require('moment') const gray = chalk.gray // formatting time, default [HH24:MI:SS] const time = function () { return '[' + moment().format('HH:mm:ss') + ']' } // support color const COLORS = [ 'red', 'gray', 'blue', 'cyan', 'white', 'green', 'yellow' ] const STATES = { error : 'red', info : 'cyan', warn : 'yellow', success : 'green' } // build color stream COLORS.forEach(function (color) { exports[color] = (function (color) { return function (content, flag) { console.log((flag ? chalk.gray(time()) + ' ' : '') + chalk[color](content)) } })(color) }) // build state stream Object.keys(STATES).forEach(function (state) { exports[state] = (function (state) { return function (content, options) { var color, what = '?' options = Object.assign({}, { time: true, // need to show time label: state.toUpperCase(), // content's label all: false, // whether color print all text color: STATES[state] // print's color }, options) color = options.color what = (options.time ? gray(time()) + ' ' : '') + (chalk[color]('[' + options.label + ']') + ' ') + (options.all ? chalk[color](content) : content) console.log(what) } })(state) }) exports.log = console.log exports.dir = console.dir exports.time = console.time exports.assert = console.assert exports.trace = console.trace