symfony-style-console
Version:
Use the style and utilities of the Symfony Console in Node.js
183 lines (182 loc) • 6.46 kB
JavaScript
import { arrContains } from '../Helper/Helper';
/**
* Formatter style class for defining styles.
*
*
* @author Florian Reuschel <florian@loilo.de>
*
* Port to TypeScript
*
*/
let OutputFormatterStyle = /** @class */ (() => {
class OutputFormatterStyle {
/**
* Initializes output formatter style.
*
* @param foreground The style foreground color name
* @param background The style background color name
* @param options The style options
*/
constructor(foreground = null, background = null, 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
*/
setForeground(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
*/
setBackground(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
*/
setOption(option) {
if (!OutputFormatterStyle.availableOptions[option]) {
throw new ReferenceError(`Invalid option specified: "${option}". \
Expected one of (${Object.keys(OutputFormatterStyle.availableOptions).join(', ')})`);
}
if (!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
*/
unsetOption(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(setOption => setOption !== OutputFormatterStyle.availableOptions[option]);
}
/**
* Sets multiple style options at once.
*
* @param The style options to enable
*/
setOptions(options) {
this.options = [];
for (const option of options) {
this.setOption(option);
}
}
/**
* Applies the style to a given text.
*
* @param The text to style
* @return The formatted text
*/
apply(text) {
const setCodes = [];
const 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 (const option of this.options) {
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;
})();
export default OutputFormatterStyle;