/** * XColor Tools Module ... * a module for handle colorify text contents in node js ... * * Maintainer * * Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) */ // //#region Module Imports ... const XValueTools = require("./x-value.tools"); //#endregion // //#region Constants ... /** * these are available style which can applied to an string ... */ const AVAILABLE_STYLES = { // Bold: "\x1b[1m", Dim: "\x1b[2m", Underlined: "\x1b[4m", Blink: "\x1b[5m", ReverseFandB: "\x1b[7m", Hidden: "\x1b[8m", // // Commonly used for reset all Styles ... Reset: "\x1b[0m" }; /** * these are available foreground colors which can applied to an string ... */ const AVAILABLE_FOREGROUND_COLORS = { Default: "\x1b[39m", Black: "\x1b[30m", Red: "\x1b[31m", Green: "\x1b[32m", Yellow: "\x1b[33m", Blue: "\x1b[34m", Magenta: "\x1b[35m", Cyan: "\x1b[36m", LightGray: "\x1b[37m", DarkGray: "\x1b[90m", LightRed: "\x1b[91m", LightGreen: "\x1b[92m", LightYellow: "\x1b[93m", LightBlue: "\x1b[94m", LightMagenta: "\x1b[95m", LightCyan: "\x1b[96m", White: "\x1b[97m", }; /** * these are available background colors which can applied to an string ... */ const AVAILABLE_BACKGROUND_COLORS = { Default: "\x1b[49m", Black: "\x1b[40m", Red: "\x1b[41m", Green: "\x1b[42m", Yellow: "\x1b[43m", Blue: "\x1b[44m", Magenta: "\x1b[45m", Cyan: "\x1b[46m", LightGray: "\x1b[47m", DarkGray: "\x1b[100m", LightRed: "\x1b[101m", LightGreen: "\x1b[102m", LightYellow: "\x1b[103m", LightBlue: "\x1b[104m", LightMagenta: "\x1b[105m", LightCyan: "\x1b[106m", White: "\x1b[107m", }; /** * these are available style names, which exports from module and * users can use them ... */ const STYLE_NAMES = { Bold: "Bold", Dim: "Dim", Underlined: "Underlined", Blink: "Blink", ReverseFandB: "ReverseFandB", Hidden: "Hidden", Reset: "Reset", }; /** * these are available color names, which exports from module and * users can use them ... */ const COLOR_NAMES = { Default: "Default", Black: "Black", Red: "Red", Green: "Green", Yellow: "Yellow", Blue: "Blue", Magenta: "Magenta", Cyan: "Cyan", LightGray: "LightGray", DarkGray: "DarkGray", LightRed: "LightRed", LightGreen: "LightGreen", LightYellow: "LightYellow", LightBlue: "LightBlue", LightMagenta: "LightMagenta", LightCyan: "LightCyan", White: "White", }; //#endregion // //#region Actions ... /** * apply specified style and color on a content ... * * @param {string} content specified content for styling ... * @param {string} color specific color name for using to styling ... * @param {string} style soecufic style name to use ... * @param {boolean} toForeground apply specified color as foreground ... * @param {boolean} toBackground apply specified color as background ... * @returns {string} styled content ... */ function apply( content, color, style, toForeground = true, toBackground = false ) { // let result = content; // // Validate Arg ... if (!XValueTools.isValidArg(content)) { return result; } // // Detect and Validate Style and Apply it ... let eStyle = AVAILABLE_STYLES[style]; if (XValueTools.isValidArg(eStyle)) { result = `${eStyle}${result}${AVAILABLE_STYLES.Reset}`; } // // Detect and Validate Foreground Color and Apply it ... let eFColor = AVAILABLE_FOREGROUND_COLORS[color]; if ( !!toForeground && XValueTools.isValidArg(eFColor) ) { result = `${eFColor}${result}${AVAILABLE_STYLES.Reset}`; } // // Detect and Validate Background Color and Apply it ... let eBColor = AVAILABLE_BACKGROUND_COLORS[color]; if ( !!toBackground && XValueTools.isValidArg(eBColor) ) { result = `${eBColor}${result}${AVAILABLE_STYLES.Reset}`; } } /** * apply specific style on a content ... * * @param {string} content specific content which going to styled ... * @param {string} style a member of STYLE_NAMES which specified that which style going to applied to content ... * @returns {string} styled content ... */ function applyStyle(content, style) { // // Validate Arg ... if (!XValueTools.isValidArg(content)) { return content; } // let eStyle = AVAILABLE_STYLES[style]; if (eStyle === undefined) { return content; } // return `${eStyle}${content}${AVAILABLE_STYLES.Reset}`; } /** * apply specific foreground color on a content ... * * @param {string} content specific content which going to colorified ... * @param {string} color a member of COLOR_NAMES which specified that which color going to applied to content ... * @returns {string} colorified content ... */ function applyForegroundColor(content, color) { // // Validate Arg ... if (!XValueTools.isValidArg(content)) { return content; } // let eColor = AVAILABLE_FOREGROUND_COLORS[color]; if (eColor === undefined) { return content; } // return `${eColor}${content}${AVAILABLE_STYLES.Reset}`; } /** * apply specific background color on a content ... * * @param {string} content specific content which going to colorified ... * @param {string} color a member of COLOR_NAMES which specified that which color going to applied to content ... * @returns {string} colorified content ... */ function applyBackgroundColor(content, color) { // // Validate Arg ... if (!XValueTools.isValidArg(content)) { return content; } // let eColor = AVAILABLE_BACKGROUND_COLORS[color]; if (eColor === undefined) { return content; } // return `${eColor}${content}${AVAILABLE_STYLES.Reset}`; } /** * generate style and color applier expression ... * * @param {string} color specific color name for using to styling ... * @param {string} style soecufic style name to use ... * @param {boolean} reset close applier string by reset styles ... * @param {boolean} toForeground apply specified color as foreground ... * @param {boolean} toBackground apply specified color as background ... * @returns {string} style and color applier string ... */ function getApplier( style = "", color = "", reset = false, toForeground = true, toBackground = false ) { // let result = ""; // // Detect and Validate Style and Apply it ... let eStyle = AVAILABLE_STYLES[style]; if (XValueTools.isValidArg(eStyle)) { result = `${eStyle}`; } // // Detect and Validate Foreground Color and Apply it ... let eFColor = AVAILABLE_FOREGROUND_COLORS[color]; if ( !!toForeground && XValueTools.isValidArg(eFColor) ) { result = `${eFColor}`; } // // Detect and Validate Background Color and Apply it ... let eBColor = AVAILABLE_BACKGROUND_COLORS[color]; if ( !!toBackground && XValueTools.isValidArg(eBColor) ) { result = `${eBColor}`; } // if ( !!reset && result.length > 0 ) { result = `${result}${AVAILABLE_STYLES.Reset}`; } // return result; } /** * generate style applier expression ... * * @param {string} style soecufic style name to use ... * @param {boolean} reset close applier string by reset styles ... * @returns {string} style applier string ... */ function getStyleApplier( style = "", reset = false ) { // let result = ""; // // Detect and Validate Style and Apply it ... let eStyle = AVAILABLE_STYLES[style]; if (XValueTools.isValidArg(eStyle)) { result = `${eStyle}`; } // if ( !!reset && result.length > 0 ) { result = `${result}${AVAILABLE_STYLES.Reset}`; } // return result; } /** * generate color applier expression ... * * @param {string} color specific color name for using to styling ... * @param {boolean} reset close applier string by reset styles ... * @param {boolean} toForeground apply specified color as foreground ... * @param {boolean} toBackground apply specified color as background ... * @returns {string} color applier string ... */ function getColorApplier( color = "", reset = false, toForeground = true, toBackground = false ) { // let result = ""; // // Detect and Validate Foreground Color and Apply it ... let eFColor = AVAILABLE_FOREGROUND_COLORS[color]; if ( !!toForeground && XValueTools.isValidArg(eFColor) ) { result = `${eFColor}`; } // // Detect and Validate Background Color and Apply it ... let eBColor = AVAILABLE_BACKGROUND_COLORS[color]; if ( !!toBackground && XValueTools.isValidArg(eBColor) ) { result = `${eBColor}`; } // if ( !!reset && result.length > 0 ) { result = `${result}${AVAILABLE_STYLES.Reset}`; } // return result; } /** * colorified specific content ... * * @param {string} content specified content for styling ... * @param {string} color specific color name for using to styling ... * @param {boolean} toForeground apply specified color as foreground ... * @param {boolean} toBackground apply specified color as background ... * @returns {string} */ function colorifyContent( content = "", color = "", toForeground = true, toBackground = false ) { // let result = content; // if (!XValueTools.isValidArg(content)) { return result; } // // Finde Colors ... // // Detect and Validate Foreground Color and Apply it ... let eFColor = AVAILABLE_FOREGROUND_COLORS[color]; if ( !!toForeground && XValueTools.isValidArg(eFColor) ) { result = `${eFColor}${result}`; } // // Detect and Validate Background Color and Apply it ... let eBColor = AVAILABLE_BACKGROUND_COLORS[color]; if ( !!toBackground && XValueTools.isValidArg(eBColor) ) { result = `${eBColor}${result}`; } // if ( result.length > 0 && ( XValueTools.isValidArg(eFColor) || XValueTools.isValidArg(eBColor) ) ) { result = `${result}${AVAILABLE_STYLES.Reset}`; } // return result; } /** * apply style on specific content ... * * @param {string} content specified content for styling ... * @param {string} style soecufic style name to use ... * @returns {string} */ function stylifiyContent( content = "", style = "", ) { // let result = content; // if (!XValueTools.isValidArg(content)) { return result; } // // Detect and Validate Style and Apply it ... let eStyle = AVAILABLE_STYLES[style]; if (XValueTools.isValidArg(eStyle)) { result = `${eStyle}${result}`; } // if ( result.length > 0 && XValueTools.isValidArg(eStyle) ) { result = `${result}${AVAILABLE_STYLES.Reset}`; } // return result; } //#endregion // //#region Module Exports ... module.exports = { // STYLE_NAMES, COLOR_NAMES, // apply, applyStyle, getApplier, getColorApplier, getStyleApplier, colorifyContent, stylifiyContent, applyForegroundColor, applyBackgroundColor, } //#endregion