UNPKG

diy-log

Version:

Various console log level prefixes with support for color symbols and tags.

73 lines (72 loc) 2.88 kB
// index.ts import colors from "picocolors"; import timestamp from "time-stamp"; function isUnicodeSupported() { if (process.platform !== "win32") { return process.env.TERM !== "linux"; } return Boolean(process.env.CI) || Boolean(process.env.WT_SESSION) || // Windows Terminal Boolean(process.env.TERMINUS_SUBLIME) || // Terminus (<0.2.27) process.env.ConEmuTask === "{cmd::Cmder}" || // ConEmu and cmder process.env.TERM_PROGRAM === "Terminus-Sublime" || process.env.TERM_PROGRAM === "vscode" || process.env.TERM === "xterm-256color" || process.env.TERM === "alacritty" || process.env.TERMINAL_EMULATOR === "JetBrains-JediTerm"; } var symbols = function() { const main = { info: colors.blue("\u2139"), done: colors.green("\u2714"), success: colors.green("\u2714"), warn: colors.yellow("\u26A0"), error: colors.red("\u2716") }; const fallbacks = { info: colors.blue("i"), done: colors.green("\u221A"), success: colors.green("\u221A"), warn: colors.yellow("\u203C"), error: colors.red("\xD7") }; return isUnicodeSupported() ? main : fallbacks; }(); function logFn(fn, args, prefix) { if (prefix) { process.stdout.write(prefix + " "); } fn.apply(console, args); } var log = (...args) => logFn(console.log, args); var time = (...args) => { const times = timestamp("HH:mm:ss"); logFn(console.log, args, "[" + colors.gray(times) + "]"); }; var info = (...args) => logFn(console.info, args, symbols.info); var error = (...args) => logFn(console.error, args, symbols.error); var done = (...args) => logFn(console.log, args, symbols.done); var warn = (...args) => logFn(console.warn, args, symbols.warn); function tagFn(message, label, fn, bgColorFn, colorFn) { const labelStr = bgColorFn.call(colors, colors.black(` ${label} `)); fn.call(console, labelStr, typeof colorFn === "function" ? colorFn.call(colors, message) : message); } var tag = (message, type = "info", label) => { var _a; label = label != null ? label : type.toLocaleUpperCase(); return (_a = tag[type]) == null ? void 0 : _a.call(void 0, message, label); }; tag.info = (message, label) => tagFn(message, label ? label : "info".toLocaleUpperCase(), console.info, colors.bgBlue); tag.done = tag.success = (message, label) => tagFn(message, label ? label : "done".toLocaleUpperCase(), console.log, colors.bgGreen); tag.warn = (message, label) => tagFn(message, label ? label : "warn".toLocaleUpperCase(), console.warn, colors.bgYellow, colors.yellow); tag.error = (message, label) => tagFn(message, label ? label : "error".toLocaleUpperCase(), console.error, colors.bgRed, colors.red); var diy_log_default = { log, time, info, error, done, warn, symbols, colors, tag, timestamp }; export { colors, diy_log_default as default, done, error, info, log, done as success, symbols, tag, time, timestamp, warn };