UNPKG

acebase-core

Version:

Shared AceBase core components, no need to install manually

153 lines 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Colorize = exports.SetColorsEnabled = exports.ColorsSupported = exports.ColorStyle = void 0; const process_1 = require("./process"); // See from https://en.wikipedia.org/wiki/ANSI_escape_code const FontCode = { bold: 1, dim: 2, italic: 3, underline: 4, inverse: 7, hidden: 8, strikethrough: 94, }; const ColorCode = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, grey: 90, // Bright colors: brightRed: 91, // TODO, other bright colors }; const BgColorCode = { bgBlack: 40, bgRed: 41, bgGreen: 42, bgYellow: 43, bgBlue: 44, bgMagenta: 45, bgCyan: 46, bgWhite: 47, bgGrey: 100, bgBrightRed: 101, // TODO, other bright colors }; const ResetCode = { all: 0, color: 39, background: 49, bold: 22, dim: 22, italic: 23, underline: 24, inverse: 27, hidden: 28, strikethrough: 29, }; var ColorStyle; (function (ColorStyle) { ColorStyle["reset"] = "reset"; ColorStyle["bold"] = "bold"; ColorStyle["dim"] = "dim"; ColorStyle["italic"] = "italic"; ColorStyle["underline"] = "underline"; ColorStyle["inverse"] = "inverse"; ColorStyle["hidden"] = "hidden"; ColorStyle["strikethrough"] = "strikethrough"; ColorStyle["black"] = "black"; ColorStyle["red"] = "red"; ColorStyle["green"] = "green"; ColorStyle["yellow"] = "yellow"; ColorStyle["blue"] = "blue"; ColorStyle["magenta"] = "magenta"; ColorStyle["cyan"] = "cyan"; ColorStyle["grey"] = "grey"; ColorStyle["bgBlack"] = "bgBlack"; ColorStyle["bgRed"] = "bgRed"; ColorStyle["bgGreen"] = "bgGreen"; ColorStyle["bgYellow"] = "bgYellow"; ColorStyle["bgBlue"] = "bgBlue"; ColorStyle["bgMagenta"] = "bgMagenta"; ColorStyle["bgCyan"] = "bgCyan"; ColorStyle["bgWhite"] = "bgWhite"; ColorStyle["bgGrey"] = "bgGrey"; })(ColorStyle = exports.ColorStyle || (exports.ColorStyle = {})); function ColorsSupported() { // Checks for basic color support if (typeof process_1.default === 'undefined' || !process_1.default.stdout || !process_1.default.env || !process_1.default.platform || process_1.default.platform === 'browser') { return false; } if (process_1.default.platform === 'win32') { return true; } const env = process_1.default.env; if (env.COLORTERM) { return true; } if (env.TERM === 'dumb') { return false; } if (env.CI || env.TEAMCITY_VERSION) { return !!env.TRAVIS; } if (['iTerm.app', 'HyperTerm', 'Hyper', 'MacTerm', 'Apple_Terminal', 'vscode'].includes(env.TERM_PROGRAM)) { return true; } if (/^xterm-256|^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(env.TERM)) { return true; } return false; } exports.ColorsSupported = ColorsSupported; let _enabled = ColorsSupported(); function SetColorsEnabled(enabled) { _enabled = ColorsSupported() && enabled; } exports.SetColorsEnabled = SetColorsEnabled; function Colorize(str, style) { if (!_enabled) { return str; } const openCodes = [], closeCodes = []; const addStyle = (style) => { if (style === ColorStyle.reset) { openCodes.push(ResetCode.all); } else if (style in FontCode) { openCodes.push(FontCode[style]); closeCodes.push(ResetCode[style]); } else if (style in ColorCode) { openCodes.push(ColorCode[style]); closeCodes.push(ResetCode.color); } else if (style in BgColorCode) { openCodes.push(BgColorCode[style]); closeCodes.push(ResetCode.background); } }; if (style instanceof Array) { style.forEach(addStyle); } else { addStyle(style); } // const open = '\u001b[' + openCodes.join(';') + 'm'; // const close = '\u001b[' + closeCodes.join(';') + 'm'; const open = openCodes.map(code => '\u001b[' + code + 'm').join(''); const close = closeCodes.map(code => '\u001b[' + code + 'm').join(''); // return open + str + close; return str.split('\n').map(line => open + line + close).join('\n'); } exports.Colorize = Colorize; String.prototype.colorize = function (style) { return Colorize(this, style); }; //# sourceMappingURL=simple-colors.js.map