UNPKG

symfony-style-console

Version:

Use the style and utilities of the Symfony Console in Node.js

186 lines (185 loc) 6.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Helper_1 = require("../Helper/Helper"); /** * Formatter style class for defining styles. * * * @author Florian Reuschel <florian@loilo.de> * * Port to TypeScript * */ var OutputFormatterStyle = /** @class */ (function () { /** * Initializes output formatter style. * * @param foreground The style foreground color name * @param background The style background color name * @param options The style options */ function OutputFormatterStyle(foreground, background, options) { if (foreground === void 0) { foreground = null; } if (background === void 0) { background = null; } if (options === void 0) { options = []; } /** * The enabled style options escape sequences. */ this.options = []; if (null !== foreground) { this.setForeground(foreground); } if (null !== background) { this.setBackground(background); } if (options.length) { this.setOptions(options); } } /** * Sets style foreground color. * * @param color The color name * * @throws `ReferenceError` if the color name is not available */ OutputFormatterStyle.prototype.setForeground = function (color) { if (color === void 0) { color = null; } if (null === color) { this.foreground = null; return; } if (!OutputFormatterStyle.availableForegroundColors[color]) { throw new ReferenceError("Invalid foreground color specified: \"" + color + "\". Expected one of (" + Object.keys(OutputFormatterStyle.availableForegroundColors).join(', ') + ")"); } this.foreground = OutputFormatterStyle.availableForegroundColors[color]; }; /** * Sets style background color. * * @param color The color name * * @throws `ReferenceError` if the color name is not available */ OutputFormatterStyle.prototype.setBackground = function (color) { if (color === void 0) { color = null; } if (null === color) { this.background = null; return; } if (!OutputFormatterStyle.availableBackgroundColors[color]) { throw new ReferenceError("Invalid background color specified: \"" + color + "\". Expected one of (" + Object.keys(OutputFormatterStyle.availableBackgroundColors).join(', ') + ")"); } this.background = OutputFormatterStyle.availableBackgroundColors[color]; }; /** * Sets some specific style option. * * @param option The option name * * @throws `ReferenceError` if the option name is not available */ OutputFormatterStyle.prototype.setOption = function (option) { if (!OutputFormatterStyle.availableOptions[option]) { throw new ReferenceError("Invalid option specified: \"" + option + "\". Expected one of (" + Object.keys(OutputFormatterStyle.availableOptions).join(', ') + ")"); } if (!Helper_1.arrContains(this.options, OutputFormatterStyle.availableOptions[option])) { this.options.push(OutputFormatterStyle.availableOptions[option]); } }; /** * Unsets some specific style option. * * @param string option The option name * * @throws `ReferenceError` if the option name is not available */ OutputFormatterStyle.prototype.unsetOption = function (option) { if (!OutputFormatterStyle.availableOptions[option]) { throw new ReferenceError("Invalid option specified: \"" + option + "\". Expected one of (" + Object.keys(OutputFormatterStyle.availableOptions).join(', ') + ")"); } this.options = this.options.filter(function (setOption) { return setOption !== OutputFormatterStyle.availableOptions[option]; }); }; /** * Sets multiple style options at once. * * @param The style options to enable */ OutputFormatterStyle.prototype.setOptions = function (options) { this.options = []; for (var _i = 0, options_1 = options; _i < options_1.length; _i++) { var option = options_1[_i]; this.setOption(option); } }; /** * Applies the style to a given text. * * @param The text to style * @return The formatted text */ OutputFormatterStyle.prototype.apply = function (text) { var setCodes = []; var unsetCodes = []; if (null != this.foreground) { setCodes.push(this.foreground.set); unsetCodes.push(this.foreground.unset); } if (null != this.background) { setCodes.push(this.background.set); unsetCodes.push(this.background.unset); } if (this.options.length) { for (var _i = 0, _a = this.options; _i < _a.length; _i++) { var option = _a[_i]; setCodes.push(option.set); unsetCodes.push(option.unset); } } if (!setCodes.length) { return text; } return "\u001B[" + setCodes.join(';') + "m" + text + "\u001B[" + unsetCodes.join(';') + "m"; }; /** * The ANSI escape sequences for available foreground colors. */ OutputFormatterStyle.availableForegroundColors = { black: { set: 30, unset: 39 }, red: { set: 31, unset: 39 }, green: { set: 32, unset: 39 }, yellow: { set: 33, unset: 39 }, blue: { set: 34, unset: 39 }, magenta: { set: 35, unset: 39 }, cyan: { set: 36, unset: 39 }, white: { set: 37, unset: 39 }, default: { set: 39, unset: 39 } }; /** * The ANSI escape sequences for available background colors. */ OutputFormatterStyle.availableBackgroundColors = { black: { set: 40, unset: 49 }, red: { set: 41, unset: 49 }, green: { set: 42, unset: 49 }, yellow: { set: 43, unset: 49 }, blue: { set: 44, unset: 49 }, magenta: { set: 45, unset: 49 }, cyan: { set: 46, unset: 49 }, white: { set: 47, unset: 49 }, default: { set: 49, unset: 49 } }; /** * The ANSI escape sequences for available style options. */ OutputFormatterStyle.availableOptions = { bold: { set: 1, unset: 22 }, underscore: { set: 4, unset: 24 }, blink: { set: 5, unset: 25 }, reverse: { set: 7, unset: 27 }, dim: { set: 2, unset: 22 }, conceal: { set: 8, unset: 28 } }; return OutputFormatterStyle; }()); exports.default = OutputFormatterStyle;