UNPKG

js-log-lib

Version:

浏览器和 node 共用的 log 样式,行为控制工具,可以自定义输出样式,其核心原理是采用了 ANSI 指令以及 css 属性,更多用法可以参照 ANSI 的详细介绍

335 lines (323 loc) 12.7 kB
import { emptyObject, isEmptyObject, getType, toKebabCase } from 'utils-lib-js'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var ANSIParams = { colorMode: { trueColor: "2", compatibility: "5", }, colorType: { foreground: "38", background: "48" }, color: { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, bgBlack: 40, bgRed: 41, bgGreen: 42, bgYellow: 43, bgBlue: 44, bgMagenta: 45, bgCyan: 46, bgWhite: 47, brightBlack: 90, brightRed: 91, brightGreen: 92, brightYellow: 93, brightBlue: 94, brightMagenta: 95, brightCyan: 96, brightWhite: 97, bgBrightBlack: 100, bgBrightRed: 101, bgBrightGreen: 102, bgBrightYellow: 103, bgBrightBlue: 104, bgBrightMagenta: 105, bgBrightCyan: 106, bgBrightWhite: 107, }, textStyle: { reset: 0, bold: 1, dim: 2, italic: 3, underline: 4, inverse: 7, hidden: 8, strikethrough: 9, noBold: 21, noItalic: 23, noUnderline: 24, noBlink: 25, noInverse: 27, noHidden: 28, noStrikethrough: 29, }, cursor: { savePosition: "s", restorePosition: "u", reportPosition: "6n", toStart: "H", toLineStart: "E", toLineEnd: "F", eraseDisplay: "2J", eraseLine: "K", direct: { custom: "H", up: "A", down: "B", right: "C", left: "D", } }, scroll: { up: "S", down: "T" } }; var ANSI = (function () { function ANSI() { var _this = this; this.moveTo = function (opts) { var _a; var _b = opts !== null && opts !== void 0 ? opts : {}, direct = _b.direct, position = _b.position; if (!direct || !position) return ""; var _p = ""; if (direct === "custom" && typeof position === "object") { var col = position.col, row = position.row; _p = "".concat(row, ";").concat(col); } else if (typeof position !== "object") { _p = "".concat(position); } if (_p) return "".concat(_p).concat((_a = ANSIParams.cursor.direct[direct]) !== null && _a !== void 0 ? _a : ""); return ""; }; this.scrollTo = function (row) { if (!row) return ""; var direct = ANSIParams.scroll.down; if (row < 0) { direct = ANSIParams.scroll.up; } return "".concat(Math.abs(row)).concat(direct); }; this.getCursor = function (cursor) { var _a; if (cursor) return "".concat((_a = ANSIParams.cursor[cursor]) !== null && _a !== void 0 ? _a : ""); else return ""; }; this.getColorMode = function (mode) { var _a; return "".concat((_a = ANSIParams.colorMode[mode]) !== null && _a !== void 0 ? _a : "2"); }; this.getColorType = function (type) { var _a; return "".concat((_a = ANSIParams.colorType[type]) !== null && _a !== void 0 ? _a : "38"); }; this.getRGB = function (options) { var _a = options.type, type = _a === void 0 ? "foreground" : _a, _b = options.mode, mode = _b === void 0 ? "trueColor" : _b, _c = options.red, red = _c === void 0 ? 0 : _c, _d = options.green, green = _d === void 0 ? 0 : _d, _e = options.blue, blue = _e === void 0 ? 0 : _e, _f = options.color, color = _f === void 0 ? 0 : _f; var cType = _this.getColorType(type), cMode = _this.getColorMode(mode); if (cMode === ANSIParams.colorMode.compatibility) return "".concat(cType, ";").concat(cMode, ";").concat(color); return "".concat(cType, ";").concat(cMode, ";").concat(red, ";").concat(green, ";").concat(blue); }; this.getColor = function (color) { var _a; if (color) return "".concat((_a = ANSIParams.color[color]) !== null && _a !== void 0 ? _a : ""); else return ""; }; this.getTextStyle = function (textStyle) { var _a; if (textStyle) return "".concat((_a = ANSIParams.textStyle[textStyle]) !== null && _a !== void 0 ? _a : ""); else return ""; }; this.getANSI = function (params, isStyle) { if (isStyle === void 0) { isStyle = true; } return params ? "\u001B[".concat(params).concat(isStyle ? "m" : "") : ""; }; this.reset = function () { return _this.getANSI("0"); }; } return ANSI; }()); var defaultOpts = { reset: true, type: "log", split: true }; var JSLog = (function (_super) { __extends(JSLog, _super); function JSLog(globalOpts) { var _this = _super.call(this) || this; _this.stylePlaceholder = "%c"; _this.isNode = typeof process !== "undefined"; _this.globalOpts = defaultOpts; _this.log = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (args.length <= 0) return _this; var _a = _this.createQueue(args), logQueue = _a.logQueue, styleQueue = _a.styleQueue; _this.logFn(logQueue, styleQueue); return _this; }; _this.createQueue = function (args) { var styleQueue = [], logQueue = []; var _a = _this, isNode = _a.isNode, stylePlaceholder = _a.stylePlaceholder, globalOpts = _a.globalOpts; args.forEach(function (it) { _this.checkOptions(it); var _a = it.text, text = _a === void 0 ? "" : _a; var _opts = emptyObject(__assign(__assign({}, globalOpts), it)); var reset = _opts.reset, _b = _opts.style, style = _b === void 0 ? {} : _b; var hasStyle = !isEmptyObject(style); logQueue.push("".concat(_this.formate(_opts)).concat(hasStyle ? stylePlaceholder : "").concat(text).concat(reset ? _this.reset() : "")); if (hasStyle) { var _style = _this.formatStyle(style); _style && styleQueue.push(_style); } }); if (isNode && globalOpts.split) { return { logQueue: logQueue, styleQueue: styleQueue }; } return { logQueue: logQueue.join(""), styleQueue: styleQueue }; }; _this.checkOptions(globalOpts); _this.mixins(globalOpts); return _this; } JSLog.prototype.mixins = function (opts) { this.globalOpts = emptyObject(__assign(__assign({}, this.globalOpts), opts)); return this.globalOpts; }; JSLog.prototype.clear = function () { this.globalOpts = emptyObject(defaultOpts); }; JSLog.prototype.formate = function (opts) { var color = opts.color, textStyle = opts.textStyle, cursor = opts.cursor, move = opts.move, scroll = opts.scroll; var params = this.getANSI("".concat(this.formatCursor(cursor)).concat(this.moveTo(move)).concat(this.scrollTo(scroll)), false) + this.getANSI("".concat(this.formatColor(color)).concat(this.formatText(textStyle))); return params; }; JSLog.prototype.formatCursor = function (cursor) { if (typeof cursor === "string") { return this.getCursor(cursor); } return ""; }; JSLog.prototype.formatText = function (textStyle) { var _this = this; if (typeof textStyle === "string") { return this.getTextStyle(textStyle); } else if (typeof textStyle === "object") { var __temp_1 = ""; textStyle.forEach(function (it) { return __temp_1 += _this.getTextStyle(it); }); return __temp_1; } return ""; }; JSLog.prototype.formatColor = function (color) { var _this = this; if (typeof color === "string") { return this.getColor(color); } else if (typeof color === "object") { if (getType(color) === "array") { var __temp_2 = ""; color.forEach(function (it) { return __temp_2 += _this.formatColor(it); }); return __temp_2; } else { return this.getRGB(color); } } return ""; }; JSLog.prototype.formatStyle = function (styles) { var styleStr = ""; Reflect.ownKeys(styles).forEach(function (k) { if (typeof k === "string") styleStr += "".concat(toKebabCase(k), ":").concat(styles[k], ";"); }); return styleStr; }; JSLog.prototype.logFn = function (logQueue, styleQueue) { var globalOpts = this.globalOpts; var type = globalOpts.type; if (typeof logQueue === "string") { return console[type].apply(console, __spreadArray([logQueue], styleQueue, false)); } return console[type].apply(console, __spreadArray(__spreadArray([], logQueue, false), styleQueue, false)); }; JSLog.prototype.checkOptions = function (opts) { if (opts === void 0) { opts = {}; } var isNode = this.isNode; var cursor = opts.cursor, move = opts.move, scroll = opts.scroll, style = opts.style, color = opts.color, textStyle = opts.textStyle; if (isNode && style) throw Error("node环境下无法使用style相关属性"); if (!isNode && (cursor || move || scroll)) throw Error("window环境下无法使用光标相关属性"); if (!isNode && style && (color || textStyle)) console.warn("请注意:样式可能会冲突"); }; return JSLog; }(ANSI)); export { JSLog, JSLog as default };